Skip to content

Commit

Permalink
feat: castor 0.15 upgrade and tests cleanup (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
COil committed May 18, 2024
1 parent 9f3841b commit 1f98867
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/symfony.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bin/coverage-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
167 changes: 91 additions & 76 deletions castor.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
<?php

// Until the 1.x Castor version the API may be unstable
// it script was tested with Castor 0.10.0
// it script was tested with Castor 0.15.0

declare(strict_types=1);

use Castor\Attribute\AsTask;
use Symfony\Component\Console\Command\Command;

use function Castor\get_command;
use function Castor\exit_code;
use function Castor\io;
use function Castor\run;
use function Castor\task;

// use function Castor\parallel;

/**
* Don't display the description when using a parent command context.
*/
function title(string $title, ?Command $command = null): void
function title(Command $command, bool $silent = false): void
{
io()->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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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(
Expand All @@ -203,16 +217,16 @@ 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();
}

#[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();
Expand All @@ -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);
}
5 changes: 0 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
</testsuite>
</testsuites>

<coverage>
<report>
<clover outputFile="var/coverage/clover.xml"/>
</report>
</coverage>
<!-- Run `composer require symfony/panther` before enabling this extension -->
<!--
<extensions>
Expand Down

0 comments on commit 1f98867

Please sign in to comment.