Skip to content

Commit

Permalink
v3 (#16)
Browse files Browse the repository at this point in the history
* PHP 8.1, Symfony 6.1, dropped annotation support

* Fix extension class base
  • Loading branch information
Padam87 authored Jul 30, 2022
1 parent c61a4e5 commit a5f5ff4
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 226 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 1 addition & 8 deletions Annotation/Job.php → Attribute/Job.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php

namespace Padam87\CronBundle\Annotation;
namespace Padam87\CronBundle\Attribute;

use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

/**
* @Annotation
* @NamedArgumentConstructor
* @Target("CLASS")
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class Job
{
Expand Down
4 changes: 1 addition & 3 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Padam87\CronBundle\Command;

use Doctrine\Common\Annotations\AnnotationReader;
use Padam87\CronBundle\Util\Helper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -34,8 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

protected function dump(InputInterface $input): string
{
$reader = new AnnotationReader();
$helper = new Helper($this->getApplication(), $reader);
$helper = new Helper($this->getApplication());

$tab = $helper->createTab($input, $this->getConfiguration());

Expand Down
2 changes: 0 additions & 2 deletions Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Padam87CronExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 4 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```

Expand Down
151 changes: 7 additions & 144 deletions Tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [
Expand All @@ -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);

Expand All @@ -161,7 +45,6 @@ public function should_register_single_job_attribute()

/**
* @test
* @requires PHP 8.0
*/
public function should_register_multiple_job_attributes()
{
Expand All @@ -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);

Expand All @@ -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()
{
Expand All @@ -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);

Expand All @@ -215,7 +90,6 @@ public function should_register_job_attributes_on_multiple_commands()

/**
* @test
* @requires PHP 8.0
*/
public function should_ignore_irrelevant_attributes()
{
Expand All @@ -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);

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources/Command/IrrelevantAttributeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources/Command/OneAttributeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions Tests/Resources/Command/ProcessTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Padam87\CronBundle\Tests\Resources\Command;

use Padam87\CronBundle\Attribute\Job;
use Symfony\Component\Console\Command\Command;

#[Job(logFile: 'myjob.log')]
class ProcessTestCommand extends Command
{
protected static $defaultName = 'my:job';
}
2 changes: 1 addition & 1 deletion Tests/Resources/Command/TwoAttributesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading

0 comments on commit a5f5ff4

Please sign in to comment.