|
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 08.08.16 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Gtt\Bundle\WorkflowExtensionsBundle\Tests\Actions; |
|
13
|
|
|
|
|
14
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\WorkflowContext; |
|
15
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\WorkflowSubject\SubjectManipulator; |
|
16
|
|
|
use Gtt\Bundle\WorkflowExtensionsBundle\Actions\TransitionApplier; |
|
17
|
|
|
use PHPUnit_Framework_TestCase; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
use Symfony\Component\Workflow\Exception\LogicException; |
|
20
|
|
|
use Symfony\Component\Workflow\Workflow; |
|
21
|
|
|
|
|
22
|
|
|
class TransitionApplierTest extends PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @dataProvider exceptionAndLogLevelProvider |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testUnavailabilityToPerformTransitionIsReportedByLogger(\Exception $exception, $loggerLevel, $throw = true) |
|
28
|
|
|
{ |
|
29
|
|
|
$transitionName = "t1"; |
|
30
|
|
|
$subject = new \StdClass(); |
|
31
|
|
|
|
|
32
|
|
|
$workflow = $this->getMockBuilder(Workflow::class)->disableOriginalConstructor()->getMock(); |
|
33
|
|
|
$workflow->expects(self::once())->method('apply')->with($subject, $transitionName)->willThrowException($exception); |
|
34
|
|
|
if ($throw) { |
|
35
|
|
|
$this->setExpectedException(get_class($exception)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$workflowContext = new WorkflowContext($workflow, $subject, "id"); |
|
39
|
|
|
|
|
40
|
|
|
$logger = $this->getMock(LoggerInterface::class); |
|
41
|
|
|
$logger->expects(self::once())->method($loggerLevel); |
|
42
|
|
|
|
|
43
|
|
|
$applier = new TransitionApplier( |
|
44
|
|
|
$this->getMockBuilder(SubjectManipulator::class)->disableOriginalConstructor()->getMock(), |
|
45
|
|
|
$logger |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$applier->applyTransition($workflowContext, $transitionName); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function exceptionAndLogLevelProvider() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
// workflow logic exception should trigger info-level logging |
|
55
|
|
|
[new LogicException(), "info", false], |
|
56
|
|
|
[new \Exception(), "error"] |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @dataProvider transitionEnvironmentProvider |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testSeveralTransitionsHandledCorrectly(array $transitions = [], array $transitionsToBeTriedToApply = [], $cascade = false) |
|
64
|
|
|
{ |
|
65
|
|
|
$subject = new \StdClass(); |
|
66
|
|
|
$workflow = $this->getMockBuilder(Workflow::class)->disableOriginalConstructor()->getMock(); |
|
67
|
|
|
|
|
68
|
|
|
$callIndex = 0; |
|
69
|
|
|
$transitionsToApplyCount = 0; |
|
70
|
|
|
$workflow->expects(self::at($callIndex))->method('getName'); |
|
71
|
|
|
$callIndex++; |
|
72
|
|
|
foreach ($transitionsToBeTriedToApply as $transitionName => $applicationResult) { |
|
73
|
|
|
$invocationMocker = $workflow->expects(self::at($callIndex))->method('apply')->with($subject, $transitionName); |
|
74
|
|
|
$transitionsToApplyCount++; |
|
75
|
|
|
if (is_array($applicationResult)) { |
|
76
|
|
|
$throw = $applicationResult[1]; |
|
77
|
|
|
$exception = $applicationResult[0]; |
|
78
|
|
|
$invocationMocker->willThrowException($exception); |
|
79
|
|
|
if ($throw) { |
|
80
|
|
|
$this->setExpectedException(get_class($exception)); |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
$callIndex++; |
|
85
|
|
|
} |
|
86
|
|
|
$workflow->expects($this->exactly($transitionsToApplyCount))->method('apply'); |
|
87
|
|
|
|
|
88
|
|
|
$workflowContext = new WorkflowContext($workflow, $subject, "id"); |
|
89
|
|
|
|
|
90
|
|
|
$applier = new TransitionApplier( |
|
91
|
|
|
$this->getMockBuilder(SubjectManipulator::class)->disableOriginalConstructor()->getMock(), |
|
92
|
|
|
$this->getMock(LoggerInterface::class) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$applier->applyTransitions($workflowContext, $transitions, $cascade); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function transitionEnvironmentProvider() |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
[['t1', 't2'], ['t1' => true]], |
|
102
|
|
|
[['t1', 't2'], ['t1' => [new \Exception(), true], 't2' => true], true], |
|
103
|
|
|
[['t1', 't2'], ['t1' => [new \Exception(), true], 't2' => true], false], |
|
104
|
|
|
[['t1', 't2'], ['t1' => [new LogicException(), false], 't2' => true], true], |
|
105
|
|
|
[['t1', 't2'], ['t1' => [new LogicException(), false], 't2' => true], false], |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|