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
|
|
|
|
15
|
|
|
use Carbon\Carbon; |
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
17
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository\ScheduledJobRepository; |
18
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob; |
19
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ScheduledTransition; |
20
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\TransitionScheduler; |
21
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\SubjectManipulator; |
22
|
|
|
use JMS\JobQueueBundle\Entity\Job; |
23
|
|
|
use Psr\Log\LoggerInterface; |
24
|
|
|
|
25
|
|
|
class TransitionSchedulerTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
public function tearDown() |
28
|
|
|
{ |
29
|
|
|
parent::tearDown(); |
30
|
|
|
Carbon::setTestNow(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInternalExceptionsDoNotBlockExecution() |
34
|
|
|
{ |
35
|
|
|
list($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em) = $this->setupSchedulerContext(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$transition1 = new ScheduledTransition('t1', 'PT1S'); |
38
|
|
|
$transition2 = new ScheduledTransition('t2', 'PT2S'); |
39
|
|
|
|
40
|
|
|
$logger = $this->getMock(LoggerInterface::class); |
41
|
|
|
$logger->expects(self::exactly(2))->method('error'); |
42
|
|
|
|
43
|
|
|
/** @var $repository \PHPUnit_Framework_MockObject_MockObject */ |
44
|
|
|
$repository |
45
|
|
|
->expects(self::at(0)) |
46
|
|
|
->method('findScheduledJobToReschedule') |
47
|
|
|
->willThrowException(new \Exception()) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
$repository |
51
|
|
|
->expects(self::at(1)) |
52
|
|
|
->method('findScheduledJobToReschedule') |
53
|
|
|
->willReturn(null) |
54
|
|
|
; |
55
|
|
|
/** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */ |
56
|
|
|
$em->expects(self::once())->method('persist')->willThrowException(new \Exception()); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$scheduler = new TransitionScheduler($em, $subjectManipulator, $logger); |
|
|
|
|
59
|
|
|
$scheduler->scheduleTransitions($subject, $workflowName, [$transition1, $transition2]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testSchedulerSchedulesCorrectJob() |
63
|
|
|
{ |
64
|
|
|
list($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em) = $this->setupSchedulerContext(); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$transition = new ScheduledTransition('t1', 'PT1S'); |
67
|
|
|
|
68
|
|
|
$transitionTriggerJobArgs = [ |
69
|
|
|
'--transition=' . $transition->getTransitionName(), |
70
|
|
|
'--workflow=' . $workflowName, |
71
|
|
|
'--subjectClass=' . $subjectClass, |
72
|
|
|
'--subjectId=' . $subjectId, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
Carbon::setTestNow(Carbon::now()); |
76
|
|
|
$now = Carbon::now(); |
77
|
|
|
$executeJobAfter = $now->add($transition->getOffset()); |
78
|
|
|
|
79
|
|
|
/** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */ |
80
|
|
|
$em->expects(self::exactly(2))->method('persist')->withConsecutive( |
|
|
|
|
81
|
|
|
[self::callback( |
82
|
|
|
function (Job $job) use ($transitionTriggerJobArgs, $executeJobAfter) { |
83
|
|
|
return |
84
|
|
|
$job->getArgs() == $transitionTriggerJobArgs && |
85
|
|
|
$job->getExecuteAfter() == $executeJobAfter; |
86
|
|
|
} |
87
|
|
|
)], |
88
|
|
|
[self::callback( |
89
|
|
|
function (ScheduledJob $job) { |
90
|
|
|
return |
91
|
|
|
$job->getReschedulable() == true && $job->getJob() instanceof Job; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
)] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$scheduler = new TransitionScheduler($em, $subjectManipulator, $this->getMock(LoggerInterface::class)); |
|
|
|
|
97
|
|
|
$scheduler->scheduleTransitions($subject, $workflowName, [$transition]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testSchedulerReschedulesCorrectJob() |
101
|
|
|
{ |
102
|
|
|
list($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em) = $this->setupSchedulerContext(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$transition = new ScheduledTransition('t1', 'PT1S'); |
105
|
|
|
|
106
|
|
|
Carbon::setTestNow(Carbon::now()); |
107
|
|
|
$now = Carbon::now(); |
108
|
|
|
|
109
|
|
|
$transitionTriggerJob = new Job('command'); |
110
|
|
|
$newExecuteJobAfter = $now->add($transition->getOffset()); |
111
|
|
|
|
112
|
|
|
$scheduledJob = new ScheduledJob($transitionTriggerJob); |
113
|
|
|
|
114
|
|
|
$repository |
115
|
|
|
->expects(self::once()) |
116
|
|
|
->method('findScheduledJobToReschedule') |
117
|
|
|
->willReturn($scheduledJob) |
118
|
|
|
; |
119
|
|
|
|
120
|
|
|
/** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */ |
121
|
|
|
$em->expects(self::once())->method('persist')->withConsecutive( |
|
|
|
|
122
|
|
|
[self::callback( |
123
|
|
|
function (Job $job) use ($newExecuteJobAfter) { |
124
|
|
|
return $job->getExecuteAfter() == $newExecuteJobAfter; |
125
|
|
|
} |
126
|
|
|
)] |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$scheduler = new TransitionScheduler($em, $subjectManipulator, $this->getMock(LoggerInterface::class)); |
|
|
|
|
130
|
|
|
$scheduler->scheduleTransitions($subject, $workflowName, [$transition]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
private function setupSchedulerContext() |
137
|
|
|
{ |
138
|
|
|
$subject = new \StdClass(); |
139
|
|
|
$subjectClass = get_class($subject); |
140
|
|
|
$subjectId = '1'; |
141
|
|
|
|
142
|
|
|
$workflowName = 'w1'; |
143
|
|
|
|
144
|
|
|
$subjectManipulator = $this->getMockBuilder(SubjectManipulator::class)->disableOriginalConstructor()->getMock(); |
145
|
|
|
$subjectManipulator->expects(self::once())->method('getSubjectId')->with($subject)->willReturn($subjectId); |
146
|
|
|
|
147
|
|
|
$repository = $this->getMockBuilder(ScheduledJobRepository::class)->disableOriginalConstructor()->getMock(); |
148
|
|
|
|
149
|
|
|
$em = $this->getMockBuilder(ObjectManager::class)->disableOriginalConstructor()->getMock(); |
150
|
|
|
$em->expects(self::once())->method('getRepository')->with(ScheduledJob::class)->willReturn($repository); |
151
|
|
|
|
152
|
|
|
return array($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.