Skip to content

Commit

Permalink
Add symfony 5 compatibility (php-task#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Jun 16, 2020
1 parent 488a998 commit 39e8ae3
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 47 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ matrix:
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-dist --no-interaction"
- php: 7.1
- php: 7.4
env:
- COMPOSER_FLAGS="--prefer-lowest --prefer-dist --no-interaction"
- php: 7.4
env:
- CODE_COVERAGE=true

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"php": "^5.6 || ^7.0",
"doctrine/common": "^2.6",
"ramsey/uuid": "^3.1",
"symfony/event-dispatcher": "^2.6 || ^3.4 || ^4.0",
"mtdowling/cron-expression": "^1.1"
"symfony/event-dispatcher": "^2.6 || ^3.4 || ^4.0 || ^5.0",
"dragonmantank/cron-expression": "^1.1 || ^2.0 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"mikey179/vfsStream": "^1.6",
"phpunit/phpunit": "^5.7.27",
"mikey179/vfsstream": "^1.6.7",
"psr/log": "^1.0"
},
"autoload": {
Expand Down
25 changes: 25 additions & 0 deletions src/Task/Event/BaseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Task\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

// Clean up when sf 3.4 support is removed
if (is_subclass_of(EventDispatcherInterface::class, ContractsEventDispatcherInterface::class)) {
/**
* @internal
*/
abstract class BaseEvent extends ContractsEvent
{
}
} else {
/**
* @internal
*/
abstract class BaseEvent extends Event
{
}
}
3 changes: 1 addition & 2 deletions src/Task/Event/TaskEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@

namespace Task\Event;

use Symfony\Component\EventDispatcher\Event;
use Task\TaskInterface;

/**
* Task Events are triggered by the Scheduler during scheduling and run process.
*/
class TaskEvent extends Event
class TaskEvent extends BaseEvent
{
/**
* @var TaskInterface
Expand Down
30 changes: 17 additions & 13 deletions src/Task/Runner/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Task\Runner;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Task\Event\Events;
use Task\Event\TaskExecutionEvent;
use Task\Execution\TaskExecutionInterface;
Expand Down Expand Up @@ -45,12 +46,6 @@ class TaskRunner implements TaskRunnerInterface
*/
private $eventDispatcher;

/**
* @param TaskExecutionRepositoryInterface $taskExecutionRepository
* @param ExecutionFinderInterface $executionFinder
* @param ExecutorInterface $executor
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
TaskExecutionRepositoryInterface $taskExecutionRepository,
ExecutionFinderInterface $executionFinder,
Expand Down Expand Up @@ -113,7 +108,7 @@ private function run(TaskExecutionInterface $execution)
*/
private function handle(TaskExecutionInterface $execution)
{
$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_BEFORE,
new TaskExecutionEvent($execution->getTask(), $execution)
);
Expand All @@ -132,7 +127,7 @@ private function handle(TaskExecutionInterface $execution)
$execution->reset();
$execution->incrementAttempts();

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_RETRIED,
new TaskExecutionEvent($execution->getTask(), $execution)
);
Expand All @@ -141,7 +136,7 @@ private function handle(TaskExecutionInterface $execution)

throw new ExitException();
} finally {
$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_AFTER,
new TaskExecutionEvent($execution->getTask(), $execution)
);
Expand All @@ -164,7 +159,7 @@ private function hasPassed(TaskExecutionInterface $execution, $result)
$execution->setStatus(TaskStatus::COMPLETED);
$execution->setResult($result);

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_PASSED,
new TaskExecutionEvent($execution->getTask(), $execution)
);
Expand All @@ -176,7 +171,7 @@ private function hasPassed(TaskExecutionInterface $execution, $result)
* The given task failed the run.
*
* @param TaskExecutionInterface $execution
* @param \Exception $exception
* @param \Throwable $exception
*
* @return TaskExecutionInterface
*/
Expand All @@ -188,7 +183,7 @@ private function hasFailed(TaskExecutionInterface $execution, \Exception $except
$execution->setException($exception->__toString());
$execution->setStatus(TaskStatus::FAILED);

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_FAILED,
new TaskExecutionEvent($execution->getTask(), $execution)
);
Expand All @@ -213,11 +208,20 @@ private function finalize(TaskExecutionInterface $execution, $start)
$execution->setDuration(microtime(true) - $start);
}

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_FINISHED,
new TaskExecutionEvent($execution->getTask(), $execution)
);

$this->taskExecutionRepository->save($execution);
}

private function dispatch($eventName, $event)
{
if (class_exists(LegacyEventDispatcherProxy::class)) {
return $this->eventDispatcher->dispatch($event, $eventName);
} else {
return $this->eventDispatcher->dispatch($eventName, $event);
}
}
}
20 changes: 12 additions & 8 deletions src/Task/Scheduler/TaskScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Task\Scheduler;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Task\Builder\TaskBuilderFactoryInterface;
use Task\Event\Events;
use Task\Event\TaskEvent;
Expand Down Expand Up @@ -46,12 +47,6 @@ class TaskScheduler implements TaskSchedulerInterface
*/
private $eventDispatcher;

/**
* @param TaskBuilderFactoryInterface $factory
* @param TaskRepositoryInterface $taskRepository
* @param TaskExecutionRepositoryInterface $taskExecutionRepository
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
TaskBuilderFactoryInterface $factory,
TaskRepositoryInterface $taskRepository,
Expand All @@ -77,7 +72,7 @@ public function createTask($handlerClass, $workload = null)
*/
public function addTask(TaskInterface $task)
{
$this->eventDispatcher->dispatch(Events::TASK_CREATE, new TaskEvent($task));
$this->dispatch(Events::TASK_CREATE, new TaskEvent($task));

$this->taskRepository->save($task);
$this->scheduleTask($task);
Expand Down Expand Up @@ -115,11 +110,20 @@ protected function scheduleTask(TaskInterface $task)
$execution = $this->taskExecutionRepository->create($task, $scheduleTime);
$execution->setStatus(TaskStatus::PLANNED);

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_EXECUTION_CREATE,
new TaskExecutionEvent($task, $execution)
);

$this->taskExecutionRepository->save($execution);
}

private function dispatch($eventName, $event)
{
if (class_exists(LegacyEventDispatcherProxy::class)) {
return $this->eventDispatcher->dispatch($event, $eventName);
} else {
return $this->eventDispatcher->dispatch($eventName, $event);
}
}
}
29 changes: 21 additions & 8 deletions tests/Unit/Runner/TaskRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Task\Event\Events;
use Task\Event\TaskExecutionEvent;
use Task\Execution\TaskExecution;
Expand Down Expand Up @@ -195,39 +196,42 @@ public function testRunTasksRetryMaximumAttemptsReached()

private function initializeDispatcher($eventDispatcher, $execution, $event = Events::TASK_PASSED)
{
$eventDispatcher->dispatch(
$testCase = $this;
$this->dispatch(
Events::TASK_BEFORE,
Argument::that(
function (TaskExecutionEvent $event) use ($execution) {
return $event->getTaskExecution() === $execution;
}
)
)->will(
function () use ($eventDispatcher, $execution, $event) {
$eventDispatcher->dispatch(
function () use ($testCase, $eventDispatcher, $execution, $event) {
$testCase->dispatch(
Events::TASK_AFTER,
Argument::that(
function (TaskExecutionEvent $event) use ($execution) {
return $event->getTaskExecution() === $execution;
}
)
)->shouldBeCalled();
$eventDispatcher->dispatch(
);
$testCase->dispatch(
$event,
Argument::that(
function (TaskExecutionEvent $event) use ($execution) {
return $event->getTaskExecution() === $execution;
}
)
)->shouldBeCalled();
$eventDispatcher->dispatch(
);
$testCase->dispatch(
Events::TASK_FINISHED,
Argument::that(
function (TaskExecutionEvent $event) use ($execution) {
return $event->getTaskExecution() === $execution;
}
)
)->shouldBeCalled();
);

return new \stdClass();
}
);
}
Expand All @@ -251,6 +255,15 @@ private function createTaskExecution(

return $execution;
}

private function dispatch($eventName, $event)
{
if (class_exists(LegacyEventDispatcherProxy::class)) {
return $this->eventDispatcher->dispatch($event, $eventName)->shouldBeCalled()->willReturnArgument(0);
} else {
return $this->eventDispatcher->dispatch($eventName, $event)->shouldBeCalled()->willReturnArgument(0);
}
}
}

class TestHandler implements TaskHandlerInterface
Expand Down
44 changes: 32 additions & 12 deletions tests/Unit/Scheduler/TaskSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cron\CronExpression;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Task\Builder\TaskBuilderFactoryInterface;
use Task\Builder\TaskBuilderInterface;
use Task\Event\Events;
Expand Down Expand Up @@ -94,22 +95,31 @@ public function testAddTask()

$execution = $this->prophesize(TaskExecutionInterface::class);

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_CREATE,
Argument::that(
function (TaskEvent $event) use ($task) {
return $event->getTask() === $task->reveal();
function ($event) use ($task) {
if ($event instanceof TaskEvent) {
return $event->getTask() === $task->reveal();
}

return false;
}
)
)->shouldBeCalled();
$this->eventDispatcher->dispatch(
);

$this->dispatch(
Events::TASK_EXECUTION_CREATE,
Argument::that(
function (TaskExecutionEvent $event) use ($task, $execution) {
return $event->getTask() === $task->reveal() && $event->getTaskExecution() === $execution->reveal();
function ($event) use ($task, $execution) {
if ($event instanceof TaskExecutionEvent) {
return $event->getTask() === $task->reveal() && $event->getTaskExecution() === $execution->reveal();
}

return false;
}
)
)->shouldBeCalled();
);

$this->taskRepository->save($task->reveal())->shouldBeCalled();

Expand Down Expand Up @@ -154,22 +164,23 @@ public function testScheduleTasks()
$this->taskExecutionRepository->create($tasks[2], $date)->willReturn($execution2->reveal());
$this->taskExecutionRepository->save($execution2)->shouldBeCalled();

$this->eventDispatcher->dispatch(
$this->dispatch(
Events::TASK_EXECUTION_CREATE,
Argument::that(
function (TaskExecutionEvent $event) use ($tasks, $execution1) {
return $event->getTask() === $tasks[1] && $event->getTaskExecution() === $execution1->reveal();
}
)
)->shouldBeCalled();
$this->eventDispatcher->dispatch(
);

$this->dispatch(
Events::TASK_EXECUTION_CREATE,
Argument::that(
function (TaskExecutionEvent $event) use ($tasks, $execution2) {
return $event->getTask() === $tasks[2] && $event->getTaskExecution() === $execution2->reveal();
}
)
)->shouldBeCalled();
);

$this->taskScheduler->scheduleTasks();
}
Expand All @@ -182,6 +193,15 @@ private function createTask($interval, $firstExecution = null)

return $task->reveal();
}

private function dispatch($eventName, $event)
{
if (class_exists(LegacyEventDispatcherProxy::class)) {
return $this->eventDispatcher->dispatch($event, $eventName)->shouldBeCalled()->willReturnArgument(0);
} else {
return $this->eventDispatcher->dispatch($eventName, $event)->shouldBeCalled()->willReturnArgument(0);
}
}
}

class TestHandler implements TaskHandlerInterface
Expand Down

0 comments on commit 39e8ae3

Please sign in to comment.