From a5f5ff43d754c9aab1807cefff5e33368c9139cc Mon Sep 17 00:00:00 2001 From: Adam Prager Date: Sat, 30 Jul 2022 22:50:39 +0200 Subject: [PATCH] v3 (#16) * PHP 8.1, Symfony 6.1, dropped annotation support * Fix extension class base --- .github/workflows/ci.yaml | 4 +- {Annotation => Attribute}/Job.php | 9 +- Command/DumpCommand.php | 4 +- Command/ImportCommand.php | 2 - DependencyInjection/Padam87CronExtension.php | 2 +- README.md | 32 +--- Tests/HelperTest.php | 151 +----------------- .../Command/IrrelevantAttributeCommand.php | 2 +- .../Resources/Command/OneAttributeCommand.php | 2 +- .../Resources/Command/ProcessTestCommand.php | 12 ++ .../Command/TwoAttributesCommand.php | 2 +- UPGRADE-3.0.md | 5 + Util/Helper.php | 31 +--- Util/Tab.php | 2 +- Util/VariableBag.php | 2 +- composer.json | 11 +- 16 files changed, 47 insertions(+), 226 deletions(-) rename {Annotation => Attribute}/Job.php (82%) create mode 100644 Tests/Resources/Command/ProcessTestCommand.php create mode 100644 UPGRADE-3.0.md diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0c0747f..d3cc6c1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,13 +16,11 @@ jobs: strategy: matrix: php-version: - - "7.4" - - "8.0" - "8.1" dependencies: - "highest" include: - - php-version: "7.4" + - php-version: "8.1" dependencies: "lowest" steps: diff --git a/Annotation/Job.php b/Attribute/Job.php similarity index 82% rename from Annotation/Job.php rename to Attribute/Job.php index 5078907..0d773bc 100644 --- a/Annotation/Job.php +++ b/Attribute/Job.php @@ -1,14 +1,7 @@ getApplication(), $reader); + $helper = new Helper($this->getApplication()); $tab = $helper->createTab($input, $this->getConfiguration()); diff --git a/Command/ImportCommand.php b/Command/ImportCommand.php index 553a8c8..348373e 100644 --- a/Command/ImportCommand.php +++ b/Command/ImportCommand.php @@ -2,8 +2,6 @@ namespace Padam87\CronBundle\Command; -use Doctrine\Common\Annotations\AnnotationReader; -use Padam87\CronBundle\Util\Helper; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; diff --git a/DependencyInjection/Padam87CronExtension.php b/DependencyInjection/Padam87CronExtension.php index e0d3582..89eb41b 100644 --- a/DependencyInjection/Padam87CronExtension.php +++ b/DependencyInjection/Padam87CronExtension.php @@ -4,7 +4,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; class Padam87CronExtension extends Extension diff --git a/README.md b/README.md index a1a0551..0b8e8a5 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,9 @@ padam87_cron: any_other_variable_you_might_need: 'some_value' ``` -## Usage +## Usage (v3) + +**Please note that v2 versions of this bundle still support annotations.** ### Commands @@ -28,48 +30,22 @@ padam87_cron: ### Basic -_Using attributes (requires PHP 8.0 or higher)_ ```php #[Job(minute: '5', hour: '0')] class MyCommand extends Command ``` -_Using annotations_ -```php -/** - * @Cron\Job(minute="5", hour="0") - */ -class MyCommand extends Command -``` - ### Groups -_Using attributes (requires PHP 8.0 or higher)_ ```php #[Job(minute: '5',hour: '0', group: 'master')] class MyCommand extends Command ``` -_Using annotations_ -```php -/** - * @Cron\Job(minute="5", hour="0", group="master") - */ -class MyCommand extends Command -``` - ### Output file -_Using attributes (requires PHP 8.0 or higher)_ -```php -#[Job(minute: '5', hour: '0', logFile: 'my-command.log')] -class MyCommand extends Command -``` -_Using annotations_ ```php -/** - * @Cron\Job(minute="5", hour="0", logFile="my-command.log") - */ +#[Job(minute: '5', hour: '0', logFile: 'my-command.log')] class MyCommand extends Command ``` diff --git a/Tests/HelperTest.php b/Tests/HelperTest.php index 5664593..6a74005 100644 --- a/Tests/HelperTest.php +++ b/Tests/HelperTest.php @@ -2,15 +2,13 @@ namespace Padam87\CronBundle\Tests; -use Doctrine\Common\Annotations\AnnotationReader; -use Padam87\CronBundle\Annotation\Job; use Padam87\CronBundle\Tests\Resources\Command\IrrelevantAttributeCommand; use Padam87\CronBundle\Tests\Resources\Command\OneAttributeCommand; +use Padam87\CronBundle\Tests\Resources\Command\ProcessTestCommand; use Padam87\CronBundle\Tests\Resources\Command\TwoAttributesCommand; use Padam87\CronBundle\Util\Helper; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Application; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; class HelperTest extends TestCase @@ -27,117 +25,6 @@ private function getConfig(): array /** * @test */ - public function should_register_single_job_annotaion() - { - $commands = [ - $this->createMock(Command::class), - ]; - - $application = $this->createMock(Application::class); - $application->expects($this->once())->method('all')->willReturn($commands); - - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ - new Job(), - ]); - - $helper = new Helper($application, $annotationReader); - - $input = $this->createMock(InputInterface::class); - - $tab = $helper->createTab($input, $this->getConfig()); - - $this->assertCount(1, $tab->getJobs()); - } - - /** - * @test - */ - public function should_register_multiple_job_annotaions() - { - $commands = [ - $this->createMock(Command::class), - ]; - - $application = $this->createMock(Application::class); - $application->expects($this->once())->method('all')->willReturn($commands); - - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ - new Job(), - new Job(), - ]); - - $helper = new Helper($application, $annotationReader); - - $input = $this->createMock(InputInterface::class); - - $tab = $helper->createTab($input, $this->getConfig()); - - $this->assertCount(2, $tab->getJobs()); - } - - /** - * @test - */ - public function should_register_job_annotaions_on_multiple_commands() - { - $commands = [ - $this->createMock(Command::class), - $this->createMock(Command::class), - $this->createMock(Command::class), - ]; - - $application = $this->createMock(Application::class); - $application->expects($this->once())->method('all')->willReturn($commands); - - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->exactly(3))->method('getClassAnnotations')->willReturnOnConsecutiveCalls( - [new Job(), new Job()], - [new Job()], - [], - ); - - $helper = new Helper($application, $annotationReader); - - $input = $this->createMock(InputInterface::class); - - $tab = $helper->createTab($input, $this->getConfig()); - - $this->assertCount(3, $tab->getJobs()); - } - - /** - * @test - */ - public function should_ignore_irrelevant_annotations() - { - $commands = [ - $this->createMock(Command::class) - ]; - - $application = $this->createMock(Application::class); - $application->expects($this->once())->method('all')->willReturn($commands); - - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ - new Job(), - new \stdClass(), - ]); - - $helper = new Helper($application, $annotationReader); - - $input = $this->createMock(InputInterface::class); - - $tab = $helper->createTab($input, $this->getConfig()); - - $this->assertCount(1, $tab->getJobs()); - } - - /** - * @test - * @requires PHP 8.0 - */ public function should_register_single_job_attribute() { $commands = [ @@ -147,10 +34,7 @@ public function should_register_single_job_attribute() $application = $this->createMock(Application::class); $application->expects($this->once())->method('all')->willReturn($commands); - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->never())->method('getClassAnnotations'); - - $helper = new Helper($application, $annotationReader); + $helper = new Helper($application); $input = $this->createMock(InputInterface::class); @@ -161,7 +45,6 @@ public function should_register_single_job_attribute() /** * @test - * @requires PHP 8.0 */ public function should_register_multiple_job_attributes() { @@ -172,10 +55,7 @@ public function should_register_multiple_job_attributes() $application = $this->createMock(Application::class); $application->expects($this->once())->method('all')->willReturn($commands); - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->never())->method('getClassAnnotations'); - - $helper = new Helper($application, $annotationReader); + $helper = new Helper($application); $input = $this->createMock(InputInterface::class); @@ -186,7 +66,6 @@ public function should_register_multiple_job_attributes() /** * @test - * @requires PHP 8.0 */ public function should_register_job_attributes_on_multiple_commands() { @@ -200,11 +79,7 @@ public function should_register_job_attributes_on_multiple_commands() $application = $this->createMock(Application::class); $application->expects($this->once())->method('all')->willReturn($commands); - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->once())->method('getClassAnnotations') - ->with(new \ReflectionClass($stdClass))->willReturn([]); - - $helper = new Helper($application, $annotationReader); + $helper = new Helper($application); $input = $this->createMock(InputInterface::class); @@ -215,7 +90,6 @@ public function should_register_job_attributes_on_multiple_commands() /** * @test - * @requires PHP 8.0 */ public function should_ignore_irrelevant_attributes() { @@ -226,10 +100,7 @@ public function should_ignore_irrelevant_attributes() $application = $this->createMock(Application::class); $application->expects($this->once())->method('all')->willReturn($commands); - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->never())->method('getClassAnnotations'); - - $helper = new Helper($application, $annotationReader); + $helper = new Helper($application); $input = $this->createMock(InputInterface::class); @@ -243,20 +114,12 @@ public function should_ignore_irrelevant_attributes() */ public function should_process_jobs() { - $command = $this->createMock(Command::class); - $command->expects($this->once())->method('getName')->willReturn('my:job'); - - $commands = [$command]; + $commands = [new ProcessTestCommand()]; $application = $this->createMock(Application::class); $application->expects($this->once())->method('all')->willReturn($commands); - $annotationReader = $this->createMock(AnnotationReader::class); - $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ - new Job('*', '*', '*', '*', '*', null, 'myjob.log'), - ]); - - $helper = new Helper($application, $annotationReader); + $helper = new Helper($application); $input = $this->createMock(InputInterface::class); diff --git a/Tests/Resources/Command/IrrelevantAttributeCommand.php b/Tests/Resources/Command/IrrelevantAttributeCommand.php index 13a44a0..4958a33 100644 --- a/Tests/Resources/Command/IrrelevantAttributeCommand.php +++ b/Tests/Resources/Command/IrrelevantAttributeCommand.php @@ -2,7 +2,7 @@ namespace Padam87\CronBundle\Tests\Resources\Command; -use Padam87\CronBundle\Annotation\Job; +use Padam87\CronBundle\Attribute\Job; use Symfony\Component\Console\Command\Command; #[Job] diff --git a/Tests/Resources/Command/OneAttributeCommand.php b/Tests/Resources/Command/OneAttributeCommand.php index 09fc008..c633164 100644 --- a/Tests/Resources/Command/OneAttributeCommand.php +++ b/Tests/Resources/Command/OneAttributeCommand.php @@ -2,7 +2,7 @@ namespace Padam87\CronBundle\Tests\Resources\Command; -use Padam87\CronBundle\Annotation\Job; +use Padam87\CronBundle\Attribute\Job; use Symfony\Component\Console\Command\Command; #[Job] diff --git a/Tests/Resources/Command/ProcessTestCommand.php b/Tests/Resources/Command/ProcessTestCommand.php new file mode 100644 index 0000000..2a69d37 --- /dev/null +++ b/Tests/Resources/Command/ProcessTestCommand.php @@ -0,0 +1,12 @@ +application = $application; - $this->annotationReader = $annotationReader; } public function createTab(InputInterface $input, ?array $config = null): Tab @@ -31,25 +25,10 @@ public function createTab(InputInterface $input, ?array $config = null): Tab $reflectionClass = new \ReflectionClass($commandInstance); - if (PHP_MAJOR_VERSION >= 8) { - $attributes = $reflectionClass->getAttributes(Job::class); - - if (count($attributes) > 0) { - foreach ($attributes as $attribute) { - $this->processJob(new Job(...$attribute->getArguments()), $input, $config, $commandInstance, $tab); - } - - // Don't process annotations - continue; - } - } - - $jobs = $this->annotationReader->getClassAnnotations($reflectionClass); + $attributes = $reflectionClass->getAttributes(Job::class); - foreach ($jobs as $job) { - if ($job instanceof Job) { - $this->processJob($job, $input, $config, $commandInstance, $tab); - } + foreach ($attributes as $attribute) { + $this->processJob(new Job(...$attribute->getArguments()), $input, $config, $commandInstance, $tab); } } diff --git a/Util/Tab.php b/Util/Tab.php index 4f76ac2..ec518fd 100644 --- a/Util/Tab.php +++ b/Util/Tab.php @@ -2,7 +2,7 @@ namespace Padam87\CronBundle\Util; -use Padam87\CronBundle\Annotation\Job; +use Padam87\CronBundle\Attribute\Job; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\BufferedOutput; diff --git a/Util/VariableBag.php b/Util/VariableBag.php index 500b84c..89c92dc 100644 --- a/Util/VariableBag.php +++ b/Util/VariableBag.php @@ -2,7 +2,7 @@ namespace Padam87\CronBundle\Util; -use Padam87\CronBundle\Annotation\Job; +use Padam87\CronBundle\Attribute\Job; class VariableBag implements \ArrayAccess { diff --git a/composer.json b/composer.json index 46109e5..f15aba3 100644 --- a/composer.json +++ b/composer.json @@ -19,12 +19,11 @@ "psr-4": {"Padam87\\CronBundle\\": ""} }, "require": { - "php": "^7.4|^8.0", - "doctrine/annotations": "^1.12", - "symfony/console": "^5.1|^6.0", - "symfony/process": "^5.1|^6.0", - "symfony/config": "^5.1|^6.0", - "symfony/dependency-injection": "^5.1|^6.0" + "php": "^8.1", + "symfony/console": "^6.1", + "symfony/process": "^6.1", + "symfony/config": "^6.1", + "symfony/dependency-injection": "^6.1" }, "require-dev": { "phpunit/phpunit": "^9.5"