From 1f988679c8ca5c51fd7656e38c6abd79e1cde061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Vernet?= Date: Sat, 18 May 2024 10:29:27 +0200 Subject: [PATCH] feat: castor 0.15 upgrade and tests cleanup (#37) --- .github/workflows/symfony.yml | 2 +- Makefile | 2 +- README.md | 1 + bin/coverage-checker.php | 2 +- castor.php | 167 ++++++++++++++++++---------------- phpunit.xml.dist | 5 - 6 files changed, 95 insertions(+), 84 deletions(-) diff --git a/.github/workflows/symfony.yml b/.github/workflows/symfony.yml index f495956..48d60fb 100644 --- a/.github/workflows/symfony.yml +++ b/.github/workflows/symfony.yml @@ -54,7 +54,7 @@ jobs: # Code coverage - name: Generate the code coverage report and show the current global coverage run: | - php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage + php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage --coverage-clover=var/coverage/clover.xml php bin/coverage-checker.php var/coverage/clover.xml 100 # run all lint/CS checks (non critical) diff --git a/Makefile b/Makefile index 1eca349..354a6ce 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ test: ## Run all PHPUnit tests coverage: ## Generate the HTML PHPUnit code coverage report (stored in var/coverage) coverage: purge - @XDEBUG_MODE=coverage php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage + @XDEBUG_MODE=coverage php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage --coverage-clover=var/coverage/clover.xml @php bin/coverage-checker.php var/coverage/clover.xml 100 cov-report: var/coverage/index.html ## Open the PHPUnit code coverage report (var/coverage/index.html) diff --git a/README.md b/README.md index 902330d..63d6287 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ In both cases, your controller code has to be [modified accordingly](https://sym ## References 📚 +* [Automated Test Coverage Checks with Travis, PHPUnit for Github Pull Requests](https://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/) (ocramius.github.io)us * [Installing and using php-cs-fixer](https://www.strangebuzz.com/en/blog/installing-and-using-php-cs-fixer) (strangebuzz.com) * [Castor, a journey across the sea of task runners](https://jolicode.com/blog/castor-a-journey-across-the-sea-of-task-runners) (jolicode.com) * [Initializing your Symfony project with solid foundations](https://www.strangebuzz.com/en/blog/initializing-your-symfony-project-with-solid-foundations) (strangebuzz.com) diff --git a/bin/coverage-checker.php b/bin/coverage-checker.php index 4a079c7..c367de6 100644 --- a/bin/coverage-checker.php +++ b/bin/coverage-checker.php @@ -33,7 +33,7 @@ $coverage = round(($checkedElements / $totalElements) * 100, 2); if ($coverage < $percentage) { - echo ' > Code coverage: '.$coverage.'%, which is below the accepted '.$percentage.'% ❌'.PHP_EOL; + echo ' > Code coverage: '.$coverage.'%, which is below the accepted '.$percentage.'% ❌'.PHP_EOL.PHP_EOL; exit(1); } diff --git a/castor.php b/castor.php index 142528f..8512e85 100644 --- a/castor.php +++ b/castor.php @@ -1,30 +1,36 @@ title($title.($command !== null ? ': '.$command->getDescription() : '')); + if (!$silent) { + io()->title($command->getDescription()); + } } -function success(): void +function success(int $exitCode): int { - io()->success('Done!'); + if ($exitCode === 0) { + io()->success('Done!'); + } else { + io()->error(sprintf('Failure (exit code %d returned).', $exitCode)); + } + + return $exitCode; } function aborted(): void @@ -35,27 +41,25 @@ function aborted(): void #[AsTask(namespace: 'symfony', description: 'Serve the application with the Symfony binary', )] function start(): void { - title(__FUNCTION__, get_command()); + title(task()); run('symfony serve --daemon', quiet: false); - success(); } #[AsTask(namespace: 'symfony', description: 'Stop the web server')] function stop(): void { - title(__FUNCTION__, get_command()); + title(task()); run('symfony server:stop', quiet: false); - success(); } #[AsTask(namespace: 'symfony', description: 'Switch to the production environment')] function go_prod(): void { - title(__FUNCTION__, get_command()); - if (io()->confirm('Are you sure you want to switch to the production environment? This will overwrite your .env.local file', false)) { + title(task()); + if (io()->confirm('Are you sure you want to switch to the production environment? This will overwrite your .env.local file.', false)) { run('cp .env.local.dist .env.local', quiet: false); run('bin/console asset-map:compile', quiet: false); - success(); + success(0); return; } @@ -66,11 +70,11 @@ function go_prod(): void #[AsTask(namespace: 'symfony', description: 'Switch to the development environment')] function go_dev(): void { - title(__FUNCTION__, get_command()); - if (io()->confirm('Are you sure you want to switch to the development environment? This will delete your .env.local file', false)) { + title(task()); + if (io()->confirm('Are you sure you want to switch to the development environment? This will delete your .env.local file.', false)) { run('rm -f .env.local', quiet: false); run('rm -rf ./public/assets/*', quiet: false); - success(); + success(0); return; } @@ -81,115 +85,125 @@ function go_dev(): void #[AsTask(namespace: 'symfony', description: 'Purge all Symfony cache and logs')] function purge(): void { - title(__FUNCTION__, get_command()); - run('rm -rf ./var/cache/* ./var/logs/* ./var/coverage/*', quiet: false); - success(); + title(task()); + success(exit_code('rm -rf ./var/cache/* ./var/logs/* ./var/coverage/*', quiet: false)); } #[AsTask(name: 'all', namespace: 'test', description: 'Run all PHPUnit tests')] -function test(): void +function test(): int { - title(__FUNCTION__, get_command()); - run('vendor/bin/phpunit', quiet: false); + title(task()); + $ec = exit_code(__DIR__.'/vendor/bin/phpunit'); io()->writeln(''); - success(); + + return $ec; } #[AsTask(namespace: 'test', description: 'Generate the HTML PHPUnit code coverage report (stored in var/coverage)')] -function coverage(): void +function coverage(): int { - title(__FUNCTION__, get_command()); - run('php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage', + title(task(), task()?->getName() !== 'test:coverage'); + $ec = exit_code('php -d xdebug.enable=1 -d memory_limit=-1 vendor/bin/phpunit --coverage-html=var/coverage --coverage-clover=var/coverage/clover.xml', environment: [ 'XDEBUG_MODE' => 'coverage', ], quiet: false ); - run('php bin/coverage-checker.php var/coverage/clover.xml 100', quiet: false); - success(); + if ($ec !== 0) { + return $ec; + } + + return success(exit_code('php bin/coverage-checker.php var/coverage/clover.xml 100', quiet: false)); } #[AsTask(namespace: 'test', description: 'Open the PHPUnit code coverage report (var/coverage/index.html)')] function cov_report(): void { - title(__FUNCTION__, get_command()); - run('open var/coverage/index.html', quiet: true); - success(); + title(task()); + success(exit_code('open var/coverage/index.html', quiet: true)); } #[AsTask(namespace: 'cs', description: 'Run PHPStan')] -function stan(): void +function stan(): int { - title(__FUNCTION__, get_command()); - run('vendor/bin/phpstan analyse -c phpstan.neon --memory-limit 1G -vvv', quiet: false); - success(); + title(task(), task()?->getName() !== 'cs:stan'); + + return exit_code('vendor/bin/phpstan analyse -c phpstan.neon --memory-limit 1G -vvv', quiet: false); } #[AsTask(namespace: 'cs', description: 'Fix PHP files with php-cs-fixer (ignore PHP 8.2 warning)')] -function fix_php(): void +function fix_php(): int { - title(__FUNCTION__, get_command()); - run('vendor/bin/php-cs-fixer fix --allow-risky=yes', + title(task(), task()?->getName() !== 'cs:fix-php'); + $ec = exit_code('vendor/bin/php-cs-fixer fix --allow-risky=yes', environment: [ 'PHP_CS_FIXER_IGNORE_ENV' => 1, ], quiet: false ); - success(); + + return success($ec); } #[AsTask(namespace: 'cs', description: 'Lint PHP files with php-cs-fixer (report only)')] -function lint_php(): void +function lint_php(): int { - title(__FUNCTION__, get_command()); - run('vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run', + title(task(), task()?->getName() !== 'cs:lint-php'); + $ec = exit_code('vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run', environment: [ 'PHP_CS_FIXER_IGNORE_ENV' => 1, ], quiet: false ); - success(); + io()->newLine(); + + return success($ec); } #[AsTask(name: 'all', namespace: 'cs', description: 'Run all CS checks')] -function cs_all(): void +function cs_all(): int { - title(__FUNCTION__, get_command()); - fix_php(); - stan(); + title(task(), task()?->getName() !== 'cs:all'); + $ec1 = fix_php(); + $ec2 = stan(); + io()->newLine(); + + return success($ec1 + $ec2); } #[AsTask(name: 'container', namespace: 'lint', description: 'Lint the Symfony DI container')] -function lint_container(): void +function lint_container(): int { - title(__FUNCTION__, get_command()); - run('bin/console lint:container', quiet: false); - success(); + title(task(), task()?->getName() !== 'lint:container'); + + return exit_code('bin/console lint:container', quiet: false); } #[AsTask(name: 'twig', namespace: 'lint', description: 'Lint Twig files')] -function lint_twig(): void +function lint_twig(): int { - title(__FUNCTION__, get_command()); - run('bin/console lint:twig templates/', quiet: false); - success(); + title(task(), task()?->getName() !== 'lint:twig'); + + return exit_code('bin/console lint:twig templates/', quiet: false); } #[AsTask(name: 'yaml', namespace: 'lint', description: 'Lint Yaml files')] -function lint_yaml(): void +function lint_yaml(): int { - title(__FUNCTION__, get_command()); - run('bin/console lint:yaml --parse-tags config/', quiet: false); - success(); + title(task(), task()?->getName() !== 'lint:yaml'); + + return exit_code('bin/console lint:yaml --parse-tags config/', quiet: false); } #[AsTask(name: 'all', namespace: 'lint', description: 'Run all lints')] -function lint_all(): void +function lint_all(): int { - title(__FUNCTION__, get_command()); - lint_php(); - lint_container(); - lint_twig(); - lint_yaml(); + title(task(), task()?->getName() !== 'lint:all'); + $ec1 = lint_php(); + $ec2 = lint_container(); + $ec3 = lint_twig(); + $ec4 = lint_yaml(); + + return success($ec1 + $ec2 + $ec3 + $ec4); // if you want to speed up the process, you can run these commands in parallel // parallel( @@ -203,8 +217,8 @@ function lint_all(): void #[AsTask(name: 'all', namespace: 'ci', description: 'Run CI locally')] function ci(): void { - title(__FUNCTION__, get_command()); - test(); + title(task()); + coverage(); cs_all(); lint_all(); } @@ -212,7 +226,7 @@ function ci(): void #[AsTask(name: 'versions', namespace: 'helpers', description: 'Output current stack versions')] function versions(): void { - title(__FUNCTION__, get_command()); + title(task()); io()->note('PHP'); run('php -v', quiet: false); io()->newLine(); @@ -236,13 +250,14 @@ function versions(): void run('vendor/bin/php-cs-fixer --version', quiet: false); io()->newLine(); - success(); + success(0); } #[AsTask(name: 'check-requirements', namespace: 'helpers', description: 'Checks requirements for running Symfony')] -function check_requirements(): void +function check_requirements(): int { - title(__FUNCTION__, get_command()); - run('vendor/bin/requirements-checker', quiet: false); - success(); + $ec = exit_code('vendor/bin/requirements-checker'); + io()->newLine(); + + return success($ec); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c3edc78..c6edcb5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -26,11 +26,6 @@ - - - - -