1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Global Trading Technologies Ltd workflow-extension-bundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* (c) fduch <[email protected]> |
9
|
|
|
* @date 09.08.16 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\Tests\Schedule; |
13
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Command\ExecuteActionCommand; |
17
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository\ScheduledJobRepository; |
18
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob; |
19
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ActionScheduler; |
20
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ValueObject\ScheduledAction; |
21
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\WorkflowContext; |
22
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
23
|
|
|
use Psr\Log\LoggerInterface; |
24
|
|
|
use Symfony\Component\Workflow\Workflow; |
25
|
|
|
|
26
|
|
|
class ActionSchedulerTest extends \PHPUnit_Framework_TestCase |
27
|
|
|
{ |
28
|
|
|
public function tearDown() |
29
|
|
|
{ |
30
|
|
|
parent::tearDown(); |
31
|
|
|
Carbon::setTestNow(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @dataProvider scheduledActionProvider |
36
|
|
|
*/ |
37
|
|
|
public function testSchedulerSchedulesCorrectJob(ScheduledAction $action, $alreadyScheduled) |
38
|
|
|
{ |
39
|
|
|
/** @var WorkflowContext $workflowContext */ |
40
|
|
|
list($workflowContext, $repository, $em) = $this->setupSchedulerContext(); |
41
|
|
|
|
42
|
|
|
Carbon::setTestNow(Carbon::now()); |
43
|
|
|
$now = Carbon::now(); |
44
|
|
|
|
45
|
|
|
$executeJobAfter = clone $now; |
46
|
|
|
$executeJobAfter->add($action->getOffset()); |
47
|
|
|
|
48
|
|
|
if ($action->isReschedulable()) { |
49
|
|
|
if ($alreadyScheduled) { |
50
|
|
|
// expecting rescheduling |
51
|
|
|
$actionJob = new Job('command', ['--some' => 'args']); |
52
|
|
|
$scheduledJob = new ScheduledJob($actionJob); |
53
|
|
|
|
54
|
|
|
$expectedActionJob = clone $actionJob; |
55
|
|
|
$expectedActionJob->setExecuteAfter($executeJobAfter); |
56
|
|
|
|
57
|
|
|
$repository->expects(self::once())->method('findScheduledJobToReschedule')->willReturn($scheduledJob); |
58
|
|
|
$em->expects(self::once())->method('persist')->with(self::equalTo($expectedActionJob)); |
59
|
|
View Code Duplication |
} else { |
|
|
|
|
60
|
|
|
// expecting the new one |
61
|
|
|
$repository->expects(self::once())->method('findScheduledJobToReschedule')->willReturn(null); |
62
|
|
|
$this->configureEmToExpectNewJobCreation($em, $action, $workflowContext, $executeJobAfter); |
63
|
|
|
} |
64
|
|
View Code Duplication |
} else { |
|
|
|
|
65
|
|
|
// non-reschedulable action - expecting creation of the new job |
66
|
|
|
$repository->expects(self::never())->method('findScheduledJobToReschedule')->willReturn(null); |
67
|
|
|
$this->configureEmToExpectNewJobCreation($em, $action, $workflowContext, $executeJobAfter); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$scheduler = new ActionScheduler($em, $this->getMock(LoggerInterface::class)); |
71
|
|
|
$scheduler->scheduleAction($workflowContext, $action); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function scheduledActionProvider() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
[new ScheduledAction('a1', [], 'PT1S'), true], |
78
|
|
|
[new ScheduledAction('a1', [], 'PT1S'), false], |
79
|
|
|
[new ScheduledAction('a1', [], 'PT1S', true), true], |
80
|
|
|
[new ScheduledAction('a1', [], 'PT1S', true), false] |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function configureEmToExpectNewJobCreation(ObjectManager $em, ScheduledAction $action, WorkflowContext $workflowContext, \DateTime $executeJobAfter) |
85
|
|
|
{ |
86
|
|
|
$expectedActionJob = new Job( |
87
|
|
|
ExecuteActionCommand::COMMAND_NAME, |
88
|
|
|
[ |
89
|
|
|
'--action=' . $action->getName(), |
90
|
|
|
'--arguments=' . json_encode($action->getArguments()), |
91
|
|
|
'--workflow=' . $workflowContext->getWorkflow()->getName(), |
92
|
|
|
'--subjectClass=' . get_class($workflowContext->getSubject()), |
93
|
|
|
'--subjectId=' . $workflowContext->getSubjectId() |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$expectedActionJob->setExecuteAfter($executeJobAfter); |
98
|
|
|
|
99
|
|
|
/** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */ |
100
|
|
|
$em->expects(self::exactly(2))->method('persist')->withConsecutive( |
|
|
|
|
101
|
|
|
self::equalTo($expectedActionJob), |
102
|
|
|
self::equalTo(new ScheduledJob($expectedActionJob, $action->isReschedulable())) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
private function setupSchedulerContext() |
110
|
|
|
{ |
111
|
|
|
$subject = new \StdClass(); |
112
|
|
|
$subjectId = '1'; |
113
|
|
|
|
114
|
|
|
$workflow = $this->getMockBuilder(Workflow::class)->disableOriginalConstructor()->getMock(); |
115
|
|
|
$workflow->expects(self::any())->method('getName')->willReturn('w1'); |
116
|
|
|
|
117
|
|
|
$workflowContext = new WorkflowContext($workflow, $subject, $subjectId); |
118
|
|
|
|
119
|
|
|
$repository = $this->getMockBuilder(ScheduledJobRepository::class)->disableOriginalConstructor()->getMock(); |
120
|
|
|
|
121
|
|
|
$em = $this->getMockBuilder(ObjectManager::class)->disableOriginalConstructor()->getMock(); |
122
|
|
|
$em->expects(self::once())->method('getRepository')->with(ScheduledJob::class)->willReturn($repository); |
123
|
|
|
|
124
|
|
|
return [$workflowContext, $repository, $em]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.