Completed
Push — master ( e32eeb...714cda )
by
unknown
89:50 queued 46:29
created

WorkflowBundle/Tests/Unit/Model/TransitionTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Oro\Bundle\WorkflowBundle\Tests\Unit\Model;
4
5
use Oro\Bundle\WorkflowBundle\Model\Transition;
6
7
class TransitionTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @dataProvider propertiesDataProvider
11
     * @param string $property
12
     * @param mixed $value
13
     */
14
    public function testGettersAndSetters($property, $value)
15
    {
16
        $ucProp = ucfirst($property);
17
        $setter = 'set' . $ucProp;
18
        $obj = new Transition();
19
        $getter = 'get' . $ucProp;
20
        if (!method_exists($obj, $getter)) {
21
            $getter = 'is' . $ucProp;
22
        }
23
        $this->assertInstanceOf(
24
            'Oro\Bundle\WorkflowBundle\Model\Transition',
25
            call_user_func_array(array($obj, $setter), array($value))
26
        );
27
        $this->assertEquals($value, call_user_func_array(array($obj, $getter), array()));
28
    }
29
30
    public function propertiesDataProvider()
31
    {
32
        return array(
33
            'name' => array('name', 'test'),
34
            'label' => array('label', 'test'),
35
            'message' => array('message', 'test'),
36
            'hidden' => array('hidden', true),
37
            'start' => array('start', true),
38
            'unavailableHidden' => array('unavailableHidden', true),
39
            'stepTo' => array('stepTo', $this->getStepMock('testStep')),
40
            'frontendOptions' => array('frontendOptions', array('key' => 'value')),
41
            'form_type' => array('formType', 'custom_workflow_transition'),
42
            'display_type' => array('displayType', 'page'),
43
            'form_options' => array('formOptions', array('one', 'two')),
44
            'page_template' => array('pageTemplate', 'Workflow:Test:page_template.html.twig'),
45
            'dialog_template' => array('dialogTemplate', 'Workflow:Test:dialog_template.html.twig'),
46
            'pre_condition' => array(
47
                'preCondition',
48
                $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface')
49
            ),
50
            'condition' => array(
51
                'condition',
52
                $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface')
53
            ),
54
            'postAction' => array(
55
                'postAction',
56
                $this->getMock('Oro\Component\Action\Action\ActionInterface')
57
            )
58
        );
59
    }
60
61
    public function testHidden()
62
    {
63
        $transition = new Transition();
64
        $this->assertFalse($transition->isHidden());
65
        $this->assertInstanceOf(
66
            'Oro\Bundle\WorkflowBundle\Model\Transition',
67
            $transition->setHidden(true)
68
        );
69
        $this->assertTrue($transition->isHidden());
70
        $this->assertInstanceOf(
71
            'Oro\Bundle\WorkflowBundle\Model\Transition',
72
            $transition->setHidden(false)
73
        );
74
        $this->assertFalse($transition->isHidden());
75
    }
76
77
    /**
78
     * @dataProvider isAllowedDataProvider
79
     * @param bool $isAllowed
80
     * @param bool $expected
81
     */
82 View Code Duplication
    public function testIsAllowed($isAllowed, $expected)
83
    {
84
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
85
            ->disableOriginalConstructor()
86
            ->getMock();
87
88
        $obj = new Transition();
89
90
        if (null !== $isAllowed) {
91
            $condition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
92
            $condition->expects($this->once())
93
                ->method('evaluate')
94
                ->with($workflowItem)
95
                ->will($this->returnValue($isAllowed));
96
            $obj->setCondition($condition);
97
        }
98
99
        $this->assertEquals($expected, $obj->isAllowed($workflowItem));
100
    }
101
102
    public function isAllowedDataProvider()
103
    {
104
        return array(
105
            'allowed' => array(
106
                'isAllowed' => true,
107
                'expected'  => true
108
            ),
109
            'not allowed' => array(
110
                'isAllowed' => false,
111
                'expected'  => false,
112
            ),
113
            'no condition' => array(
114
                'isAllowed' => null,
115
                'expected'  => true,
116
            ),
117
        );
118
    }
119
120
    /**
121
     * @dataProvider isAllowedDataProvider
122
     * @param bool $isAllowed
123
     * @param bool $expected
124
     */
125 View Code Duplication
    public function testIsAvailableWithForm($isAllowed, $expected)
126
    {
127
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
128
            ->disableOriginalConstructor()
129
            ->getMock();
130
131
        $obj = new Transition();
132
        $obj->setFormOptions(array('key' => 'value'));
133
134
        if (null !== $isAllowed) {
135
            $condition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
136
            $condition->expects($this->once())
137
                ->method('evaluate')
138
                ->with($workflowItem)
139
                ->will($this->returnValue($isAllowed));
140
            $obj->setPreCondition($condition);
141
        }
142
143
        $this->assertEquals($expected, $obj->isAvailable($workflowItem));
144
    }
145
146
    /**
147
     * @dataProvider isAvailableDataProvider
148
     * @param bool $isAllowed
149
     * @param bool $isAvailable
150
     * @param bool $expected
151
     */
152
    public function testIsAvailableWithoutForm($isAllowed, $isAvailable, $expected)
153
    {
154
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
155
            ->disableOriginalConstructor()
156
            ->getMock();
157
158
        $obj = new Transition();
159
160
        if (null !== $isAvailable) {
161
            $preCondition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
162
            $preCondition->expects($this->any())
163
                ->method('evaluate')
164
                ->with($workflowItem)
165
                ->will($this->returnValue($isAvailable));
166
            $obj->setPreCondition($preCondition);
167
        }
168
        if (null !== $isAllowed) {
169
            $condition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
170
            $condition->expects($this->any())
171
                ->method('evaluate')
172
                ->with($workflowItem)
173
                ->will($this->returnValue($isAllowed));
174
            $obj->setCondition($condition);
175
        }
176
177
        $this->assertEquals($expected, $obj->isAvailable($workflowItem));
178
    }
179
180
    public function isAvailableDataProvider()
181
    {
182
        return array(
183
            'allowed' => array(
184
                'isAllowed' => true,
185
                'isAvailable' => true,
186
                'expected'  => true
187
            ),
188
            'not allowed #1' => array(
189
                'isAllowed' => false,
190
                'isAvailable' => true,
191
                'expected'  => false,
192
            ),
193
            'not allowed #2' => array(
194
                'isAllowed' => true,
195
                'isAvailable' => false,
196
                'expected'  => false,
197
            ),
198
            'not allowed #3' => array(
199
                'isAllowed' => false,
200
                'isAvailable' => false,
201
                'expected'  => false,
202
            ),
203
            'no conditions' => array(
204
                'isAllowed' => null,
205
                'isAvailable' => null,
206
                'expected'  => true,
207
            ),
208
        );
209
    }
210
211
    /**
212
     * @dataProvider transitDisallowedDataProvider
213
     * @param bool $preConditionAllowed
214
     * @param bool $conditionAllowed
215
     * @expectedException \Oro\Bundle\WorkflowBundle\Exception\ForbiddenTransitionException
216
     * @expectedExceptionMessage Transition "test" is not allowed.
217
     */
218
    public function testTransitNotAllowed($preConditionAllowed, $conditionAllowed)
219
    {
220
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
221
            ->disableOriginalConstructor()
222
            ->getMock();
223
        $workflowItem->expects($this->never())
224
            ->method('setCurrentStep');
225
226
        $preCondition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
227
        $preCondition->expects($this->any())
228
            ->method('evaluate')
229
            ->with($workflowItem)
230
            ->will($this->returnValue($preConditionAllowed));
231
232
        $condition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
233
        $condition->expects($this->any())
234
            ->method('evaluate')
235
            ->with($workflowItem)
236
            ->will($this->returnValue($conditionAllowed));
237
238
        $postAction = $this->getMock('Oro\Component\Action\Action\ActionInterface');
239
        $postAction->expects($this->never())
240
            ->method('execute');
241
242
        $obj = new Transition();
243
        $obj->setName('test');
244
        $obj->setPreCondition($preCondition);
245
        $obj->setCondition($condition);
246
        $obj->setPostAction($postAction);
247
        $obj->transit($workflowItem);
248
    }
249
250 View Code Duplication
    public function transitDisallowedDataProvider()
251
    {
252
        return array(
253
            array(false, false),
254
            array(false, true),
255
            array(true, false)
256
        );
257
    }
258
259
    /**
260
     * @dataProvider transitDataProvider
261
     * @param boolean $isFinal
262
     * @param boolean $hasAllowedTransition
263
     */
264
    public function testTransit($isFinal, $hasAllowedTransition)
265
    {
266
        $currentStepEntity =  $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowStep')
267
            ->disableOriginalConstructor()
268
            ->getMock();
269
270
        $step = $this->getStepMock('currentStep', $isFinal, $hasAllowedTransition, $currentStepEntity);
271
272
        $workflowDefinition = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition')
273
            ->disableOriginalConstructor()
274
            ->getMock();
275
        $workflowDefinition->expects($this->once())
276
            ->method('getStepByName')
277
            ->with($step->getName())
278
            ->will($this->returnValue($currentStepEntity));
279
280
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
281
            ->disableOriginalConstructor()
282
            ->getMock();
283
        $workflowItem->expects($this->once())
284
            ->method('getDefinition')
285
            ->will($this->returnValue($workflowDefinition));
286
        $workflowItem->expects($this->once())
287
            ->method('setCurrentStep')
288
            ->with($currentStepEntity);
289
290
        $preCondition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
291
        $preCondition->expects($this->once())
292
            ->method('evaluate')
293
            ->with($workflowItem)
294
            ->will($this->returnValue(true));
295
296
        $condition = $this->getMock('Oro\Component\ConfigExpression\ExpressionInterface');
297
        $condition->expects($this->once())
298
            ->method('evaluate')
299
            ->with($workflowItem)
300
            ->will($this->returnValue(true));
301
302
        $postAction = $this->getMock('Oro\Component\Action\Action\ActionInterface');
303
        $postAction->expects($this->once())
304
            ->method('execute')
305
            ->with($workflowItem);
306
307
        $obj = new Transition();
308
        $obj->setPreCondition($preCondition);
309
        $obj->setCondition($condition);
310
        $obj->setPostAction($postAction);
311
        $obj->setStepTo($step);
312
        $obj->transit($workflowItem);
313
    }
314
315
    /**
316
     * @return array
317
     */
318 View Code Duplication
    public function transitDataProvider()
319
    {
320
        return array(
321
            array(true, true),
322
            array(true, false),
323
            array(false, false)
324
        );
325
    }
326
327
    protected function getStepMock($name, $isFinal = false, $hasAllowedTransitions = true, $stepEntity = null)
0 ignored issues
show
The parameter $stepEntity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
328
    {
329
        $step = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\Step')
330
            ->disableOriginalConstructor()
331
            ->getMock();
332
        $step->expects($this->any())
333
            ->method('getName')
334
            ->will($this->returnValue($name));
335
        $step->expects($this->any())
336
            ->method('isFinal')
337
            ->will($this->returnValue($isFinal));
338
        $step->expects($this->any())
339
            ->method('hasAllowedTransitions')
340
            ->will($this->returnValue($hasAllowedTransitions));
341
        return $step;
342
    }
343
344
    public function testStart()
345
    {
346
        $obj = new Transition();
347
        $this->assertFalse($obj->isStart());
348
        $obj->setStart(true);
349
        $this->assertTrue($obj->isStart());
350
    }
351
352
    public function testGetSetFrontendOption()
353
    {
354
        $obj = new Transition();
355
356
        $this->assertEquals(array(), $obj->getFrontendOptions());
357
358
        $frontendOptions = array('class' => 'foo', 'icon' => 'bar');
359
        $obj->setFrontendOptions($frontendOptions);
360
        $this->assertEquals($frontendOptions, $obj->getFrontendOptions());
361
    }
362
363
    public function testHasForm()
364
    {
365
        $obj = new Transition();
366
367
        $this->assertFalse($obj->hasForm()); // by default transition has form
368
369
        $obj->setFormOptions(array('key' => 'value'));
370
        $this->assertFalse($obj->hasForm());
371
372
        $obj->setFormOptions(array('attribute_fields' => array()));
373
        $this->assertFalse($obj->hasForm());
374
375
        $obj->setFormOptions(array('attribute_fields' => array('key' => 'value')));
376
        $this->assertTrue($obj->hasForm());
377
    }
378
}
379