Skip to content

Commit cdcf696

Browse files
committed
feature #41851 Add TesterTrait::assertCommandIsSuccessful() helper (yoannrenard)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- Add TesterTrait::assertCommandIsSuccessful() helper | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR introduces a new helper TesterTrait::assertCommandIsSuccessful() that aims to help testing the result of a command. This is inspired by [BrowserKitAssertionsTrait::assertResponseIsSuccessful](https://github.com/symfony/symfony/blob/5.4/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php#L31) Commits ------- 6221527 Add TesterTrait::assertCommandIsSuccessful() helper
2 parents 883766e + 6221527 commit cdcf696

File tree

19 files changed

+126
-32
lines changed

19 files changed

+126
-32
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testLintFilesFromBundleDirectory()
5151
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
5252
);
5353

54-
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
54+
$tester->assertCommandIsSuccessful('Returns 0 in case of success');
5555
$this->assertStringContainsString('[OK] All 0 XLIFF files contain valid syntax', trim($tester->getDisplay()));
5656
}
5757

src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testLintCorrectFile()
4040
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
4141
);
4242

43-
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
43+
$tester->assertCommandIsSuccessful('Returns 0 in case of success');
4444
$this->assertStringContainsString('OK', trim($tester->getDisplay()));
4545
}
4646

@@ -88,7 +88,7 @@ public function testLintFilesFromBundleDirectory()
8888
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
8989
);
9090

91-
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
91+
$tester->assertCommandIsSuccessful('Returns 0 in case of success');
9292
$this->assertStringContainsString('[OK] All 0 YAML files contain valid syntax', trim($tester->getDisplay()));
9393
}
9494

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function testRunOnlyWarnsOnUnregistrableCommand()
147147
$tester->run(['command' => 'fine']);
148148
$output = $tester->getDisplay();
149149

150-
$this->assertSame(0, $tester->getStatusCode());
150+
$tester->assertCommandIsSuccessful();
151151
$this->assertStringContainsString('Some commands could not be registered:', $output);
152152
$this->assertStringContainsString('throwing', $output);
153153
$this->assertStringContainsString('fine', $output);
@@ -204,7 +204,7 @@ public function testRunOnlyWarnsOnUnregistrableCommandAtTheEnd()
204204
$tester = new ApplicationTester($application);
205205
$tester->run(['command' => 'list']);
206206

207-
$this->assertSame(0, $tester->getStatusCode());
207+
$tester->assertCommandIsSuccessful();
208208
$display = explode('List commands', $tester->getDisplay());
209209

210210
$this->assertStringContainsString(trim('[WARNING] Some commands could not be registered:'), trim($display[1]));

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testClearPrivatePool()
3333
$tester = $this->createCommandTester();
3434
$tester->execute(['pools' => ['cache.private_pool']], ['decorated' => false]);
3535

36-
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success');
36+
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
3737
$this->assertStringContainsString('Clearing cache pool: cache.private_pool', $tester->getDisplay());
3838
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
3939
}
@@ -43,7 +43,7 @@ public function testClearPublicPool()
4343
$tester = $this->createCommandTester();
4444
$tester->execute(['pools' => ['cache.public_pool']], ['decorated' => false]);
4545

46-
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success');
46+
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
4747
$this->assertStringContainsString('Clearing cache pool: cache.public_pool', $tester->getDisplay());
4848
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
4949
}
@@ -53,7 +53,7 @@ public function testClearPoolWithCustomClearer()
5353
$tester = $this->createCommandTester();
5454
$tester->execute(['pools' => ['cache.pool_with_clearer']], ['decorated' => false]);
5555

56-
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success');
56+
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
5757
$this->assertStringContainsString('Clearing cache pool: cache.pool_with_clearer', $tester->getDisplay());
5858
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
5959
}
@@ -63,7 +63,7 @@ public function testCallClearer()
6363
$tester = $this->createCommandTester();
6464
$tester->execute(['pools' => ['cache.app_clearer']], ['decorated' => false]);
6565

66-
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success');
66+
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
6767
$this->assertStringContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
6868
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
6969
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testListPools()
3030
$tester = $this->createCommandTester(['cache.app', 'cache.system']);
3131
$tester->execute([]);
3232

33-
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:list exits with 0 in case of success');
33+
$tester->assertCommandIsSuccessful('cache:pool:list exits with 0 in case of success');
3434
$this->assertStringContainsString('cache.app', $tester->getDisplay());
3535
$this->assertStringContainsString('cache.system', $tester->getDisplay());
3636
}
@@ -40,7 +40,7 @@ public function testEmptyList()
4040
$tester = $this->createCommandTester([]);
4141
$tester->execute([]);
4242

43-
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:list exits with 0 in case of success');
43+
$tester->assertCommandIsSuccessful('cache:pool:list exits with 0 in case of success');
4444
}
4545

4646
private function createCommandTester(array $poolNames)

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ContainerDebugCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function testGetDeprecation()
161161
$tester = new ApplicationTester($application);
162162
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
163163

164-
$this->assertSame(0, $tester->getStatusCode());
164+
$tester->assertCommandIsSuccessful();
165165
$this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
166166
$this->assertStringContainsString('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
167167
}
@@ -181,7 +181,7 @@ public function testGetDeprecationNone()
181181
$tester = new ApplicationTester($application);
182182
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
183183

184-
$this->assertSame(0, $tester->getStatusCode());
184+
$tester->assertCommandIsSuccessful();
185185
$this->assertStringContainsString('[OK] There are no deprecations in the logs!', $tester->getDisplay());
186186
}
187187

@@ -199,7 +199,7 @@ public function testGetDeprecationNoFile()
199199
$tester = new ApplicationTester($application);
200200
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
201201

202-
$this->assertSame(0, $tester->getStatusCode());
202+
$tester->assertCommandIsSuccessful();
203203
$this->assertStringContainsString('[WARNING] The deprecation file does not exist', $tester->getDisplay());
204204
}
205205

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"doctrine/persistence": "^1.3|^2.0",
3939
"symfony/asset": "^5.3|^6.0",
4040
"symfony/browser-kit": "^5.4|^6.0",
41-
"symfony/console": "^5.2|^6.0",
41+
"symfony/console": "^5.4|^6.0",
4242
"symfony/css-selector": "^4.4|^5.0|^6.0",
4343
"symfony/dom-crawler": "^4.4|^5.0|^6.0",
4444
"symfony/dotenv": "^5.1|^6.0",

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `TesterTrait::assertCommandIsSuccessful()` to test command
8+
49
5.3
510
---
611

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tester\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Console\Command\Command;
16+
17+
final class CommandIsSuccessful extends Constraint
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function toString(): string
23+
{
24+
return 'is successful';
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
protected function matches($other): bool
31+
{
32+
return Command::SUCCESS === $other;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected function failureDescription($other): string
39+
{
40+
return 'the command '.$this->toString();
41+
}
42+
}

src/Symfony/Component/Console/Tester/TesterTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Component\Console\Tester;
1313

14+
use PHPUnit\Framework\Assert;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Output\ConsoleOutput;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Output\StreamOutput;
19+
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
1820

1921
/**
2022
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
@@ -110,6 +112,11 @@ public function getStatusCode()
110112
return $this->statusCode;
111113
}
112114

115+
public function assertCommandIsSuccessful(string $message = ''): void
116+
{
117+
Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message);
118+
}
119+
113120
/**
114121
* Sets the user inputs.
115122
*

0 commit comments

Comments
 (0)