Share
Embed
Testing Symfony/Console applications withย PHPUnit
Introduction to testing Symfony/Console applications with PHPUnit, presented at the Symfony Usergroup Berlin.
Raphael Stolt
November 08, 2023
Transcript
16 slides
-
The system under test The business logic of the SUT
( ) is the validation, creation, and modification of the .gitattributes file of any given software ๐ฆ. http://git.io/lean- package-validator -
Purpose of a .gitattributes file The main* purpose of a
.gitattributes file is to define which repository artifacts end up in a distributable package. A distributable package or Git archive is the one which is pulled into your project via package managers like Composer or pip . It helps package maintainers to reduce their ๐จ๐ footprint by preventing needless file transfers. -
Anatomy of a .gitattributes file Maybe located in the working
directory of a given Git repository. * text=auto eol=lf *.sqlite diff=sqlite3 .editorconfig export-ignore .gitattributes export-ignore .github/ export-ignore .gitignore export-ignore .php-cs-fixer.php export-ignore box.json.dist export-ignore CHANGELOG.md export-ignore example/ export-ignore LICENSE.md export-ignore phpstan.neon.dist export-ignore phpunit.xml.dist export-ignore README md export ignore -
Scenarios to test The produced exit code of the tool
under test is very important, as it's intended to be hooked into CI pipelines via . Over time additional features emerged like showing the diff between the expected and actual .gitattributes content; or the injection of glob pattern to utilise it for packages outside of the ๐ ecosystem. GitHub Actions -
Classic PHPUnit test setup $ cat phpunit.xml.dist <?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation= "https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> <coverage/> <testsuites> <testsuite name="Lean Package Validator"> <directory>tests/</directory> -
Classic PHPUnit test setup $ tree src/ tests/ src โโโ
Analyser.php โโโ Archive โ โโโ Validator.php โโโ Archive.php โโโ Commands โ โโโ InitCommand.php โ โโโ ValidateCommand.php โโโ Exceptions โ โโโ GitArchiveNotValidatedYet.php โ โโโ GitattributesCreationFailed.php โ โโโ GitHeadNotAvailable.php โ โโโ GitNotAvailable.php โ โโโ InvalidGlobPatternFile.php โ โ InvalidGlobPattern php -
Ways to approach testing a CLI 1. Trust your business
logic tests and neglect the CLI input port to it ๐ท -
Ways to approach testing a CLI 2. Utilising PHP's good
ol' exec โ #[Test] public function executableIsAvailable(): void { $command = 'php bin/lean-package-validator'; \exec($command, $output, $returnValue); $this->assertStringStartsWith( 'Lean package validator', $output[1], 'Expected application name not present.' ); -
Ways to approach testing a CLI 3. Utilising the Symfony/Process
component โ #[Test] public function executableIsAvailable(): void { $command = 'php bin/lean-package-validator'; $process = Process::formShellCommandline($command); $process->run(); $this->assertStringStartsWith( 'Lean package validator', $process->getOutput(), 'Expected application name not present.' ); -
Ways to approach testing a Symfony/Console based CLI 4. Utilising
Symfony/Console's CommandTester or ApplicationTester โ $ tree vendor/symfony/console/Tester vendor/symfony/console/Tester โโโ ApplicationTester.php โโโ CommandCompletionTester.php โโโ CommandTester.php โโโ Constraint โ โโโ CommandIsSuccessful.php โโโ TesterTrait.php 1 directory, 5 files -
Ways to approach testing a Symfony/Console based CLI 4. Utilising
Symfony/Console's CommandTester or ApplicationTester โ $ php ListCommandTesterPublicApi.php __construct(Command $command) execute(array $input, array $options = [])): int getDisplay(bool $normalize = false): string getErrorOutput(bool $normalize = false): string) getInput(): InputInterface getOutput(): OutputInterface getStatusCode(): int assertCommandIsSuccessful(): void setInputs(array $inputs): static -
InitCommandTest /** * Set up test environment. */ protected function
setUp(): void { $this->setUpTemporaryDirectory(); if (!\defined('WORKING_DIRECTORY')) { \define('WORKING_DIRECTORY', $this->temporaryDirectory); } $this->application = $this->getApplication(); } #[Test] public function verboseOutputIsAvailableWhenDesired(): void -
ValidateCommandTest #[Test] public function gitattributesFileWithNoExportIgnoresShowsExpected { $analyserMock = Mockery::mock( 'Stolt\LeanPackage\Analyser[getGlobalGitignorePatterns]'
); $analyserMock->shouldReceive('getGlobalGitignorePatterns') ->once() ->withAnyArgs() ->andReturn([]); $expectedDisplay = " The present .gitattributes file is considered invalid. Would expect the following .gitattributes file content: # Auto detect text files and perform LF normalization -
Executing the tests The lean-package-validator package has a dedicated Composer
script named lpv:test . $ composer lpv:test > phpunit PHPUnit 10.3.5 by Sebastian Bergmann and contributors. Runtime: PHP 8.2.12 Configuration: /user/stolt/work/oss/lean- ๐ฆ-validator/phpunit.xml ..................................................... 63 / 121 ................................................. 121 / 121 Time: 00:00.981, Memory: 14.00 MB OK (121 tests, 204 assertions) -
THE END Thank you and enjoy your green CLI integration
tests. Questions?