Skip to content

Add TesterTrait::assertCommandIsSuccessful() helper #41851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testLintFilesFromBundleDirectory()
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testLintCorrectFile()
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);

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

Expand Down Expand Up @@ -88,7 +88,7 @@ public function testLintFilesFromBundleDirectory()
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function testRunOnlyWarnsOnUnregistrableCommand()
$tester->run(['command' => 'fine']);
$output = $tester->getDisplay();

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

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

$this->assertStringContainsString(trim('[WARNING] Some commands could not be registered:'), trim($display[1]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testClearPrivatePool()
$tester = $this->createCommandTester();
$tester->execute(['pools' => ['cache.private_pool']], ['decorated' => false]);

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

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

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

$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success');
$tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
$this->assertStringContainsString('Calling cache clearer: cache.app_clearer', $tester->getDisplay());
$this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testListPools()
$tester = $this->createCommandTester(['cache.app', 'cache.system']);
$tester->execute([]);

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

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

private function createCommandTester(array $poolNames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function testGetDeprecation()
$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"doctrine/persistence": "^1.3|^2.0",
"symfony/asset": "^5.3|^6.0",
"symfony/browser-kit": "^5.4|^6.0",
"symfony/console": "^5.2|^6.0",
"symfony/console": "^5.4|^6.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the conflict line should be updated too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"symfony/css-selector": "^4.4|^5.0|^6.0",
"symfony/dom-crawler": "^4.4|^5.0|^6.0",
"symfony/dotenv": "^5.1|^6.0",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add `TesterTrait::assertCommandIsSuccessful()` to test command

5.3
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tester\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Console\Command\Command;

final class CommandIsSuccessful extends Constraint
{
/**
* {@inheritdoc}
*/
public function toString(): string
{
return 'is successful';
}

/**
* {@inheritdoc}
*/
protected function matches($other): bool
{
return Command::SUCCESS === $other;
}

/**
* {@inheritdoc}
*/
protected function failureDescription($other): string
{
return 'the command '.$this->toString();
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Console/Tester/TesterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Component\Console\Tester;

use PHPUnit\Framework\Assert;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;

/**
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
Expand Down Expand Up @@ -110,6 +112,11 @@ public function getStatusCode()
return $this->statusCode;
}

public function assertCommandIsSuccessful(string $message = ''): void
{
Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message);
}

/**
* Sets the user inputs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public function testSetInputs()
$tester->setInputs(['I1', 'I2', 'I3']);
$tester->run(['command' => 'foo']);

$this->assertSame(0, $tester->getStatusCode());
$tester->assertCommandIsSuccessful();
$this->assertEquals('Q1Q2Q3', $tester->getDisplay(true));
}

public function testGetStatusCode()
{
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
$this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
}

public function testErrorOutput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function testGetDisplayWithoutCallingExecuteBefore()

public function testGetStatusCode()
{
$this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
$this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
}

public function testGetStatusCodeWithoutCallingExecuteBefore()
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testCommandWithInputs()
$tester->setInputs(['Bobby', 'Fine', 'France']);
$tester->execute([]);

$this->assertEquals(0, $tester->getStatusCode());
$tester->assertCommandIsSuccessful();
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
}

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

$this->assertEquals(0, $tester->getStatusCode());
$tester->assertCommandIsSuccessful();
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public function testSymfonyStyleCommandWithInputs()
$tester->setInputs(['Bobby', 'Fine', 'France']);
$tester->execute([]);

$this->assertEquals(0, $tester->getStatusCode());
$tester->assertCommandIsSuccessful();
}

public function testErrorOutput()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Tester\Constraint;

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;

final class CommandIsSuccessfulTest extends TestCase
{
public function testConstraint()
{
$constraint = new CommandIsSuccessful();

$this->assertTrue($constraint->evaluate(Command::SUCCESS, '', true));
$this->assertFalse($constraint->evaluate(Command::FAILURE, '', true));
$this->assertFalse($constraint->evaluate(Command::INVALID, '', true));

try {
$constraint->evaluate(Command::FAILURE);
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('Failed asserting that the command is successful.', TestFailure::exceptionToString($e));

return;
}

$this->fail();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testDebugDateTimeType()
$tester = $this->createCommandTester();
$tester->execute(['class' => 'DateTime'], ['decorated' => false, 'interactive' => false]);

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
"symfony/expression-language": "^4.4|^5.0|^6.0",
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/console": "^5.4|^6.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the conflict line should also be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"symfony/http-foundation": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0",
"symfony/intl": "^4.4|^5.0|^6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testBasicRun()
'--limit' => 1,
]);

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

Expand Down Expand Up @@ -96,7 +96,7 @@ public function testRunWithBusOption()
'--limit' => 1,
]);

$this->assertSame(0, $tester->getStatusCode());
$tester->assertCommandIsSuccessful();
$this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay());
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Messenger/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"require-dev": {
"psr/cache": "^1.0|^2.0|^3.0",
"symfony/console": "^4.4|^5.0|^6.0",
"symfony/console": "^5.4|^6.0",
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
"symfony/event-dispatcher": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testLintCorrectFile()
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);

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

Expand All @@ -51,7 +51,7 @@ public function testLintCorrectFiles()
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);

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

Expand Down Expand Up @@ -101,7 +101,7 @@ public function testLintTargetLanguageIsCaseInsensitive()

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

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

Expand All @@ -112,7 +112,7 @@ public function testLintSucceedsWhenLocaleInFileAndInTargetLanguageNameUsesDashe

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

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

Expand Down
Loading