Thanks to visit codestin.com
Credit goes to github.com

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

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 <[email protected]>
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 <[email protected]>
@@ -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
*

src/Symfony/Component/Console/Tests/Tester/ApplicationTesterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public function testSetInputs()
8282
$tester->setInputs(['I1', 'I2', 'I3']);
8383
$tester->run(['command' => 'foo']);
8484

85-
$this->assertSame(0, $tester->getStatusCode());
85+
$tester->assertCommandIsSuccessful();
8686
$this->assertEquals('Q1Q2Q3', $tester->getDisplay(true));
8787
}
8888

8989
public function testGetStatusCode()
9090
{
91-
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
91+
$this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
9292
}
9393

9494
public function testErrorOutput()

src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testGetDisplayWithoutCallingExecuteBefore()
7979

8080
public function testGetStatusCode()
8181
{
82-
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
82+
$this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
8383
}
8484

8585
public function testGetStatusCodeWithoutCallingExecuteBefore()
@@ -129,7 +129,7 @@ public function testCommandWithInputs()
129129
$tester->setInputs(['Bobby', 'Fine', 'France']);
130130
$tester->execute([]);
131131

132-
$this->assertEquals(0, $tester->getStatusCode());
132+
$tester->assertCommandIsSuccessful();
133133
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
134134
}
135135

@@ -154,7 +154,7 @@ public function testCommandWithDefaultInputs()
154154
$tester->setInputs(['', '', '']);
155155
$tester->execute([]);
156156

157-
$this->assertEquals(0, $tester->getStatusCode());
157+
$tester->assertCommandIsSuccessful();
158158
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
159159
}
160160

@@ -227,7 +227,7 @@ public function testSymfonyStyleCommandWithInputs()
227227
$tester->setInputs(['Bobby', 'Fine', 'France']);
228228
$tester->execute([]);
229229

230-
$this->assertEquals(0, $tester->getStatusCode());
230+
$tester->assertCommandIsSuccessful();
231231
}
232232

233233
public function testErrorOutput()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Tests\Tester\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\TestFailure;
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
19+
20+
final class CommandIsSuccessfulTest extends TestCase
21+
{
22+
public function testConstraint()
23+
{
24+
$constraint = new CommandIsSuccessful();
25+
26+
$this->assertTrue($constraint->evaluate(Command::SUCCESS, '', true));
27+
$this->assertFalse($constraint->evaluate(Command::FAILURE, '', true));
28+
$this->assertFalse($constraint->evaluate(Command::INVALID, '', true));
29+
30+
try {
31+
$constraint->evaluate(Command::FAILURE);
32+
} catch (ExpectationFailedException $e) {
33+
$this->assertStringContainsString('Failed asserting that the command is successful.', TestFailure::exceptionToString($e));
34+
35+
return;
36+
}
37+
38+
$this->fail();
39+
}
40+
}

src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testDebugDateTimeType()
7171
$tester = $this->createCommandTester();
7272
$tester->execute(['class' => 'DateTime'], ['decorated' => false, 'interactive' => false]);
7373

74-
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
74+
$tester->assertCommandIsSuccessful('Returns 0 in case of success');
7575
$this->assertStringContainsString('Symfony\Component\Form\Extension\Core\Type\DateTimeType (Block prefix: "datetime")', $tester->getDisplay());
7676
}
7777

@@ -123,7 +123,7 @@ public function testDebugAmbiguousFormTypeInteractive()
123123
$tester->setInputs([0]);
124124
$tester->execute(['class' => 'AmbiguousType'], ['decorated' => false, 'interactive' => true]);
125125

126-
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
126+
$tester->assertCommandIsSuccessful('Returns 0 in case of success');
127127
$output = $tester->getDisplay(true);
128128
$this->assertStringMatchesFormat(<<<TXT
129129

src/Symfony/Component/Form/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
3434
"symfony/expression-language": "^4.4|^5.0|^6.0",
3535
"symfony/config": "^4.4|^5.0|^6.0",
36-
"symfony/console": "^4.4|^5.0|^6.0",
36+
"symfony/console": "^5.4|^6.0",
3737
"symfony/http-foundation": "^4.4|^5.0|^6.0",
3838
"symfony/http-kernel": "^4.4|^5.0|^6.0",
3939
"symfony/intl": "^4.4|^5.0|^6.0",

src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testBasicRun()
6363
'--limit' => 1,
6464
]);
6565

66-
$this->assertSame(0, $tester->getStatusCode());
66+
$tester->assertCommandIsSuccessful();
6767
$this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay());
6868
}
6969

@@ -96,7 +96,7 @@ public function testRunWithBusOption()
9696
'--limit' => 1,
9797
]);
9898

99-
$this->assertSame(0, $tester->getStatusCode());
99+
$tester->assertCommandIsSuccessful();
100100
$this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay());
101101
}
102102
}

src/Symfony/Component/Messenger/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"require-dev": {
2828
"psr/cache": "^1.0|^2.0|^3.0",
29-
"symfony/console": "^4.4|^5.0|^6.0",
29+
"symfony/console": "^5.4|^6.0",
3030
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
3131
"symfony/event-dispatcher": "^4.4|^5.0|^6.0",
3232
"symfony/http-kernel": "^4.4|^5.0|^6.0",

src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testLintCorrectFile()
3636
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
3737
);
3838

39-
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
39+
$tester->assertCommandIsSuccessful('Returns 0 in case of success');
4040
$this->assertStringContainsString('OK', trim($tester->getDisplay()));
4141
}
4242

@@ -51,7 +51,7 @@ public function testLintCorrectFiles()
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', trim($tester->getDisplay()));
5656
}
5757

@@ -101,7 +101,7 @@ public function testLintTargetLanguageIsCaseInsensitive()
101101

102102
$tester->execute(['filename' => $filename], ['decorated' => false]);
103103

104-
$this->assertEquals(0, $tester->getStatusCode());
104+
$tester->assertCommandIsSuccessful();
105105
$this->assertStringContainsString('[OK] All 1 XLIFF files contain valid syntax.', trim($tester->getDisplay()));
106106
}
107107

@@ -112,7 +112,7 @@ public function testLintSucceedsWhenLocaleInFileAndInTargetLanguageNameUsesDashe
112112

113113
$tester->execute(['filename' => $filename], ['decorated' => false]);
114114

115-
$this->assertSame(0, $tester->getStatusCode());
115+
$tester->assertCommandIsSuccessful();
116116
$this->assertStringContainsString('[OK] All 1 XLIFF files contain valid syntax.', trim($tester->getDisplay()));
117117
}
118118

0 commit comments

Comments
 (0)