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

WorkflowTest::testStart()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 46
rs 8.9411
cc 2
eloc 35
nc 2
nop 2
1
<?php
2
3
namespace Oro\Bundle\WorkflowBundle\Tests\Unit\Model;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
use Oro\Bundle\ActionBundle\Model\Attribute;
8
use Oro\Bundle\ActionBundle\Model\AttributeManager;
9
use Oro\Bundle\WorkflowBundle\Acl\AclManager;
10
use Oro\Bundle\WorkflowBundle\Entity\WorkflowStep;
11
use Oro\Bundle\WorkflowBundle\Model\EntityConnector;
12
use Oro\Bundle\WorkflowBundle\Model\TransitionManager;
13
use Oro\Bundle\WorkflowBundle\Model\Workflow;
14
use Oro\Bundle\WorkflowBundle\Model\Step;
15
use Oro\Bundle\WorkflowBundle\Model\Transition;
16
use Oro\Bundle\WorkflowBundle\Entity\WorkflowItem;
17
use Oro\Bundle\WorkflowBundle\Entity\WorkflowTransitionRecord;
18
use Oro\Bundle\WorkflowBundle\Exception\InvalidTransitionException;
19
20
/**
21
 * @SuppressWarnings(PHPMD.TooManyMethods)
22
 */
23
class WorkflowTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @dataProvider propertiesDataProvider
27
     * @param string $property
28
     * @param mixed $value
29
     */
30 View Code Duplication
    public function testGettersAndSetters($property, $value)
31
    {
32
        $getter = 'get' . ucfirst($property);
33
        $setter = 'set' . ucfirst($property);
34
        $workflow = $this->createWorkflow();
35
        $this->assertInstanceOf(
36
            'Oro\Bundle\WorkflowBundle\Model\Workflow',
37
            call_user_func_array(array($workflow, $setter), array($value))
38
        );
39
        $this->assertEquals($value, call_user_func_array(array($workflow, $getter), array()));
40
    }
41
42
    public function propertiesDataProvider()
43
    {
44
        return array(
45
            'name' => array('name', 'test'),
46
            'label' => array('label', 'test'),
47
            'definition' => array('definition', $this->getMock('Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition'))
48
        );
49
    }
50
51
    /**
52
     * @expectedException \InvalidArgumentException
53
     * @expectedExceptionMessage Expected transition argument type is string or Transition
54
     */
55
    public function testIsTransitionAllowedArgumentException()
56
    {
57
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
61
        $workflow = $this->createWorkflow();
62
        $workflow->isTransitionAllowed($workflowItem, 1);
63
    }
64
65
    /**
66
     * @expectedException \InvalidArgumentException
67
     * @expectedExceptionMessage Expected transition argument type is string or Transition
68
     */
69
    public function testTransitAllowedArgumentException()
70
    {
71
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
72
            ->disableOriginalConstructor()
73
            ->getMock();
74
75
        $workflow = $this->createWorkflow();
76
        $workflow->transit($workflowItem, 1);
77
    }
78
79
    // @codingStandardsIgnoreStart
80
    /**
81
     * @expectedException \Oro\Bundle\WorkflowBundle\Exception\InvalidTransitionException
82
     * @expectedExceptionMessage Step "test_step" of workflow "test_workflow" doesn't have allowed transition "test_transition".
83
     */
84
    // @codingStandardsIgnoreEnd
85
    public function testIsTransitionAllowedStepHasNoAllowedTransitionException()
86
    {
87
        $workflowStep = new WorkflowStep();
88
        $workflowStep->setName('test_step');
89
90
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
91
            ->disableOriginalConstructor()
92
            ->getMock();
93
        $workflowItem->expects($this->any())
94
            ->method('getCurrentStep')
95
            ->will($this->returnValue($workflowStep));
96
        $workflowItem->expects($this->once())
97
            ->method('getWorkflowName')
98
            ->will($this->returnValue('test_workflow'));
99
100
        $step = $this->getStepMock($workflowStep->getName());
101
        $step->expects($this->any())
102
            ->method('isAllowedTransition')
103
            ->with('test_transition')
104
            ->will($this->returnValue(false));
105
106
        $transition = $this->getTransitionMock('test_transition', false);
107
108
        $workflow = $this->createWorkflow('test_workflow');
109
        $workflow->getTransitionManager()->setTransitions(array($transition));
110
        $workflow->getStepManager()->setSteps(array($step));
111
112
        $workflow->isTransitionAllowed($workflowItem, 'test_transition', null, true);
113
    }
114
115
    /**
116
     * @dataProvider isTransitionAllowedDataProvider
117
     */
118
    public function testIsTransitionAllowed(
119
        $expectedResult,
120
        $transitionExist,
121
        $transitionAllowed,
122
        $isTransitionStart,
123
        $hasCurrentStep,
124
        $stepAllowTransition,
125
        $fireExceptions = true
126
    ) {
127
        $workflowStep = new WorkflowStep();
128
        $workflowStep->setName('test_step');
129
130
        $entity = new \DateTime();
131
132
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
133
            ->disableOriginalConstructor()
134
            ->getMock();
135
        $workflowItem->expects($this->any())
136
            ->method('getWorkflowName')
137
            ->will($this->returnValue('test_workflow'));
138
        $workflowItem->expects($this->any())
139
            ->method('getCurrentStep')
140
            ->will($this->returnValue($hasCurrentStep ? $workflowStep : null));
141
        $workflowItem->expects($this->any())
142
            ->method('getEntity')
143
            ->will($this->returnValue($entity));
144
145
        $step = $this->getStepMock('test_step');
146
        $step->expects($this->any())
147
            ->method('isAllowedTransition')
148
            ->with('test_transition')
149
            ->will($this->returnValue($stepAllowTransition));
150
151
        $errors = new ArrayCollection();
152
153
        $transition = $this->getTransitionMock('test_transition', $isTransitionStart);
154
        $transition->expects($this->any())
155
            ->method('isAllowed')
156
            ->with($workflowItem, $errors)
157
            ->will($this->returnValue($transitionAllowed));
158
159
        $workflow = $this->createWorkflow('test_workflow');
160
        if ($transitionExist) {
161
            $workflow->getTransitionManager()->setTransitions(array($transition));
162
        }
163
        $workflow->getStepManager()->setSteps(array($step));
164
165
        if ($expectedResult instanceof \Exception) {
166
            $this->setExpectedException(get_class($expectedResult), $expectedResult->getMessage());
167
        }
168
169
        $actualResult = $workflow->isTransitionAllowed($workflowItem, 'test_transition', $errors, $fireExceptions);
170
171
        if (is_bool($expectedResult)) {
172
            $this->assertEquals($actualResult, $expectedResult);
173
        }
174
    }
175
176
    /**
177
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
178
     *
179
     * @return array
180
     */
181
    public function isTransitionAllowedDataProvider()
182
    {
183
        return array(
184
            'not_allowed_transition' => array(
185
                'expectedResult' => false,
186
                'transitionExist' => true,
187
                'transitionAllowed' => false,
188
                'isTransitionStart' => true,
189
                'hasCurrentStep' => true,
190
                'stepAllowTransition' => true,
191
            ),
192
            'allowed_transition' => array(
193
                'expectedResult' => true,
194
                'transitionExist' => true,
195
                'transitionAllowed' => true,
196
                'isTransitionStart' => false,
197
                'hasCurrentStep' => true,
198
                'stepAllowTransition' => true,
199
            ),
200
            'not_allowed_start_transition' => array(
201
                'expectedResult' => false,
202
                'transitionExist' => true,
203
                'transitionAllowed' => false,
204
                'isTransitionStart' => false,
205
                'hasCurrentStep' => true,
206
                'stepAllowTransition' => true,
207
            ),
208
            'allowed_start_transition' => array(
209
                'expectedResult' => true,
210
                'transitionExist' => true,
211
                'transitionAllowed' => true,
212
                'isTransitionStart' => true,
213
                'hasCurrentStep' => true,
214
                'stepAllowTransition' => true,
215
            ),
216
            'unknown_transition_fire_exception' => array(
217
                'expectedException' => InvalidTransitionException::unknownTransition('test_transition'),
218
                'transitionExist' => false,
219
                'transitionAllowed' => true,
220
                'isTransitionStart' => false,
221
                'hasCurrentStep' => true,
222
                'stepAllowTransition' => true,
223
            ),
224
            'unknown_transition_no_exception' => array(
225
                'expectedResult' => false,
226
                'transitionExist' => false,
227
                'transitionAllowed' => true,
228
                'isTransitionStart' => false,
229
                'hasCurrentStep' => true,
230
                'stepAllowTransition' => true,
231
                'fireException' => false
232
            ),
233
            'not_start_transition_fire_exception' => array(
234
                'expectedException' => InvalidTransitionException::notStartTransition(
235
                    'test_workflow',
236
                    'test_transition'
237
                ),
238
                'transitionExist' => true,
239
                'transitionAllowed' => true,
240
                'isTransitionStart' => false,
241
                'hasCurrentStep' => false,
242
                'stepAllowTransition' => true,
243
            ),
244
            'not_start_transition_no_exception' => array(
245
                'expectedResult' => false,
246
                'transitionExist' => true,
247
                'transitionAllowed' => true,
248
                'isTransitionStart' => false,
249
                'hasCurrentStep' => false,
250
                'stepAllowTransition' => true,
251
                'fireException' => false
252
            ),
253
            'step_not_allow_transition_fire_exception' => array(
254
                'expectedException' => InvalidTransitionException::stepHasNoAllowedTransition(
255
                    'test_workflow',
256
                    'test_step',
257
                    'test_transition'
258
                ),
259
                'transitionExist' => true,
260
                'transitionAllowed' => true,
261
                'isTransitionStart' => false,
262
                'hasCurrentStep' => true,
263
                'stepAllowTransition' => false,
264
            ),
265
            'step_not_allow_transition_no_exception' => array(
266
                'expectedResult' => false,
267
                'transitionExist' => true,
268
                'transitionAllowed' => true,
269
                'isTransitionStart' => false,
270
                'hasCurrentStep' => true,
271
                'stepAllowTransition' => false,
272
                'fireException' => false
273
            ),
274
        );
275
    }
276
277
    /**
278
     * @expectedException \Oro\Bundle\WorkflowBundle\Exception\InvalidTransitionException
279
     * @expectedTransitionMessage Step "stepOne" of workflow "test" doesn't have allowed transition "transition".
280
     */
281
    public function testTransitNotAllowedTransition()
282
    {
283
        $workflowStep = new WorkflowStep();
284
        $workflowStep->setName('stepOne');
285
286
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
287
            ->disableOriginalConstructor()
288
            ->getMock();
289
        $workflowItem->expects($this->any())
290
            ->method('getCurrentStep')
291
            ->will($this->returnValue($workflowStep));
292
293
        $step = $this->getStepMock($workflowStep->getName());
294
        $step->expects($this->once())
295
            ->method('isAllowedTransition')
296
            ->with('transition')
297
            ->will($this->returnValue(false));
298
299
        $transition = $this->getTransitionMock('transition');
300
301
        $workflow = $this->createWorkflow('test');
302
        $workflow->getTransitionManager()->setTransitions(array($transition));
303
        $workflow->getStepManager()->setSteps(array($step));
304
        $workflow->transit($workflowItem, 'transition');
305
    }
306
307
    public function testTransit()
308
    {
309
        $workflowStepOne = new WorkflowStep();
310
        $workflowStepOne->setName('stepOne');
311
312
        $workflowStepTwo = new WorkflowStep();
313
        $workflowStepTwo->setName('stepTwo');
314
315
        $entity = new \DateTime();
316
317
        $workflowDefinition = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition')
318
            ->disableOriginalConstructor()
319
            ->getMock();
320
        $workflowDefinition->expects($this->once())
321
            ->method('getStepByName')
322
            ->with($workflowStepTwo->getName())
323
            ->will($this->returnValue($workflowStepTwo));
324
325
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
326
            ->disableOriginalConstructor()
327
            ->getMock();
328
        $workflowItem->expects($this->any())
329
            ->method('getEntity')
330
            ->will($this->returnValue($entity));
331
        $workflowItem->expects($this->any())
332
            ->method('getCurrentStep')
333
            ->will($this->returnValue($workflowStepOne));
334
        $workflowItem->expects($this->once())
335
            ->method('addTransitionRecord')
336
            ->with($this->isInstanceOf('Oro\Bundle\WorkflowBundle\Entity\WorkflowTransitionRecord'))
337
            ->will(
338
                $this->returnCallback(
339
                    function (WorkflowTransitionRecord $transitionRecord) {
340
                        self::assertEquals('transition', $transitionRecord->getTransitionName());
341
                        self::assertEquals('stepOne', $transitionRecord->getStepFrom()->getName());
342
                        self::assertEquals('stepTwo', $transitionRecord->getStepTo()->getName());
343
                    }
344
                )
345
            );
346
347
        $stepOne = $this->getStepMock($workflowStepOne->getName());
348
        $stepOne->expects($this->once())
349
            ->method('isAllowedTransition')
350
            ->with('transition')
351
            ->will($this->returnValue(true));
352
353
        $stepTwo = $this->getStepMock('stepTwo');
354
355
        $transition = $this->getTransitionMock('transition', false, $stepTwo);
356
        $transition->expects($this->once())
357
            ->method('transit')
358
            ->with($workflowItem);
359
360
        $entityConnector = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\EntityConnector')
361
            ->disableOriginalConstructor()
362
            ->getMock();
363
        $entityConnector->expects($this->once())
364
            ->method('setWorkflowItem')
365
            ->with($entity, $workflowItem);
366
        $entityConnector->expects($this->once())
367
            ->method('setWorkflowStep')
368
            ->with($entity, $workflowStepOne);
369
370
        $aclManager = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Acl\AclManager')
371
            ->disableOriginalConstructor()
372
            ->getMock();
373
        $aclManager->expects($this->once())
374
            ->method('updateAclIdentities')
375
            ->with($workflowItem);
376
377
        $workflow = $this->createWorkflow(null, $entityConnector, $aclManager);
378
        $workflow->setDefinition($workflowDefinition);
379
        $workflow->getTransitionManager()->setTransitions(array($transition));
380
        $workflow->getStepManager()->setSteps(array($stepOne, $stepTwo));
381
        $workflow->transit($workflowItem, 'transition');
382
    }
383
384
    /**
385
     * @expectedException \Oro\Bundle\WorkflowBundle\Exception\WorkflowException
386
     * @expectedExceptionMessage Workflow "test" does not have step entity "stepTwo"
387
     */
388
    public function testTransitException()
389
    {
390
        $workflowStepOne = new WorkflowStep();
391
        $workflowStepOne->setName('stepOne');
392
393
        $workflowDefinition = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition')
394
            ->disableOriginalConstructor()
395
            ->getMock();
396
397
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
398
            ->disableOriginalConstructor()
399
            ->getMock();
400
        $workflowItem->expects($this->any())
401
            ->method('getCurrentStep')
402
            ->will($this->returnValue($workflowStepOne));
403
404
        $stepOne = $this->getStepMock($workflowStepOne->getName());
405
        $stepOne->expects($this->once())
406
            ->method('isAllowedTransition')
407
            ->with('transition')
408
            ->will($this->returnValue(true));
409
410
        $stepTwo = $this->getStepMock('stepTwo');
411
412
        $transition = $this->getTransitionMock('transition', false, $stepTwo);
413
414
        $workflow = $this->createWorkflow('test');
415
        $workflow->setDefinition($workflowDefinition);
416
        $workflow->getTransitionManager()->setTransitions(array($transition));
417
        $workflow->getStepManager()->setSteps(array($stepOne));
418
        $workflow->transit($workflowItem, 'transition');
419
    }
420
421
    /**
422
     * @dataProvider startDataProvider
423
     * @param array $data
424
     * @param string $transitionName
425
     */
426
    public function testStart($data, $transitionName)
427
    {
428
        if (!$transitionName) {
429
            $expectedTransitionName = TransitionManager::DEFAULT_START_TRANSITION_NAME;
430
        } else {
431
            $expectedTransitionName = $transitionName;
432
        }
433
434
        $workflowStep = new WorkflowStep();
435
        $workflowStep->setName('step_name');
436
        $step = $this->getStepMock($workflowStep->getName());
437
438
        $transition = $this->getTransitionMock($expectedTransitionName, true, $step);
439
        $transition->expects($this->once())
440
            ->method('transit')
441
            ->with($this->isInstanceOf('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem'))
442
            ->will(
443
                $this->returnCallback(
444
                    function (WorkflowItem $workflowItem) use ($workflowStep) {
445
                        $workflowItem->setCurrentStep($workflowStep);
446
                    }
447
                )
448
            );
449
450
        $workflowDefinition = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition')
451
            ->disableOriginalConstructor()
452
            ->getMock();
453
        $workflowDefinition->expects($this->once())
454
            ->method('getStepByName')
455
            ->with($workflowStep->getName())
456
            ->will($this->returnValue($workflowStep));
457
458
        $entity = new \DateTime();
459
        $entityAttribute = new Attribute();
460
        $entityAttribute->setName('entity');
461
462
        $workflow = $this->createWorkflow();
463
        $workflow->setDefinition($workflowDefinition);
464
        $workflow->getTransitionManager()->setTransitions(array($transition));
465
        $workflow->getAttributeManager()->setAttributes(array($entityAttribute));
466
        $workflow->getAttributeManager()->setEntityAttributeName($entityAttribute->getName());
467
        $item = $workflow->start($entity, $data, $transitionName);
468
        $this->assertInstanceOf('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem', $item);
469
        $this->assertEquals($entity, $item->getEntity());
470
        $this->assertEquals(array_merge($data, array('entity' => $entity)), $item->getData()->getValues());
471
    }
472
473 View Code Duplication
    public function startDataProvider()
474
    {
475
        return array(
476
            array(array(), null),
477
            array(array('test' => 'test'), 'test')
478
        );
479
    }
480
481 View Code Duplication
    protected function getStepMock($name)
482
    {
483
        $step = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\Step')
484
            ->disableOriginalConstructor()
485
            ->getMock();
486
        $step->expects($this->any())
487
            ->method('getName')
488
            ->will($this->returnValue($name));
489
490
        return $step;
491
    }
492
493 View Code Duplication
    protected function getTransitionMock($name, $isStart = false, $step = null)
494
    {
495
        $transition = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\Transition')
496
            ->disableOriginalConstructor()
497
            ->getMock();
498
        $transition->expects($this->any())
499
            ->method('getName')
500
            ->will($this->returnValue($name));
501
        if ($isStart) {
502
            $transition->expects($this->any())
503
                ->method('isStart')
504
                ->will($this->returnValue($isStart));
505
        }
506
        if ($step) {
507
            $transition->expects($this->any())
508
                ->method('getStepTo')
509
                ->will($this->returnValue($step));
510
        }
511
512
        return $transition;
513
    }
514
515
    public function testGetAllowedTransitions()
516
    {
517
        $firstTransition = new Transition();
518
        $firstTransition->setName('first_transition');
519
520
        $secondTransition = new Transition();
521
        $secondTransition->setName('second_transition');
522
523
        $workflowStep = new WorkflowStep();
524
        $workflowStep->setName('test_step');
525
526
        $step = new Step();
527
        $step->setName($workflowStep->getName());
528
        $step->setAllowedTransitions(array($secondTransition->getName()));
529
530
        $workflow = $this->createWorkflow();
531
        $workflow->getStepManager()->setSteps(array($step));
532
        $workflow->getTransitionManager()->setTransitions(array($firstTransition, $secondTransition));
533
534
        $workflowItem = new WorkflowItem();
535
        $workflowItem->setCurrentStep($workflowStep);
536
537
        $actualTransitions = $workflow->getTransitionsByWorkflowItem($workflowItem);
538
        $this->assertEquals(array($secondTransition), $actualTransitions->getValues());
539
    }
540
541
    /**
542
     * @expectedException \Oro\Bundle\WorkflowBundle\Exception\UnknownStepException
543
     * @expectedExceptionMessage Step "unknown_step" not found
544
     */
545
    public function testGetAllowedTransitionsUnknownStepException()
546
    {
547
        $workflowStep = new WorkflowStep();
548
        $workflowStep->setName('unknown_step');
549
550
        $workflowItem = new WorkflowItem();
551
        $workflowItem->setCurrentStep($workflowStep);
552
553
        $workflow = $this->createWorkflow();
554
        $workflow->getTransitionsByWorkflowItem($workflowItem);
555
    }
556
557
    public function testIsTransitionAvailable()
558
    {
559
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
560
            ->disableOriginalConstructor()
561
            ->getMock();
562
        $errors = new ArrayCollection();
563
        $transitionName = 'test_transition';
564
        $transition = $this->getTransitionMock($transitionName);
565
        $transition->expects($this->once())
566
            ->method('isAvailable')
567
            ->with($workflowItem, $errors)
568
            ->will($this->returnValue(true));
569
        $transitionManager = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\TransitionManager')
570
            ->disableOriginalConstructor()
571
            ->getMock();
572
        $transitionManager->expects($this->once())
573
            ->method('extractTransition')
574
            ->with($transition)
575
            ->will($this->returnValue($transition));
576
        $workflow = $this->createWorkflow(null, null, null, null, $transitionManager);
577
578
        $this->assertTrue($workflow->isTransitionAvailable($workflowItem, $transition, $errors));
579
    }
580
581
    public function testIsStartTransitionAvailable()
582
    {
583
        $data = array();
584
        $errors = new ArrayCollection();
585
        $transitionName = 'test_transition';
586
587
        $workflowDefinition = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition')
588
            ->disableOriginalConstructor()
589
            ->getMock();
590
591
        $transition = $this->getTransitionMock($transitionName);
592
        $transition->expects($this->once())
593
            ->method('isAvailable')
594
            ->with($this->isInstanceOf('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem'), $errors)
595
            ->will($this->returnValue(true));
596
        $transitionManager = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\TransitionManager')
597
            ->disableOriginalConstructor()
598
            ->getMock();
599
        $transitionManager->expects($this->once())
600
            ->method('extractTransition')
601
            ->with($transition)
602
            ->will($this->returnValue($transition));
603
        $entity = new \DateTime();
604
        $entityAttribute = new Attribute();
605
        $entityAttribute->setName('entity');
606
607
        $workflow = $this->createWorkflow(null, null, null, null, $transitionManager);
608
        $workflow->setDefinition($workflowDefinition);
609
        $workflow->getAttributeManager()->setAttributes(array($entityAttribute));
610
        $workflow->getAttributeManager()->setEntityAttributeName($entityAttribute->getName());
611
612
        $this->assertTrue($workflow->isStartTransitionAvailable($transition, $entity, $data, $errors));
613
    }
614
615
    /**
616
     * @dataProvider passedStepsDataProvider
617
     * @param array $records
618
     * @param array $expected
619
     */
620
    public function testGetPassedStepsByWorkflowItem($records, $expected)
621
    {
622
        $workflowItem = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowItem')
623
            ->disableOriginalConstructor()
624
            ->getMock();
625
        $workflowItem->expects($this->once())
626
            ->method('getTransitionRecords')
627
            ->will($this->returnValue($records));
628
629
        $stepsOne = $this->getStepMock('step1');
630
        $stepsOne->expects($this->any())
631
            ->method('getOrder')
632
            ->will($this->returnValue(1));
633
        $stepsTwo = $this->getStepMock('step2');
634
        $stepsTwo->expects($this->any())
635
            ->method('getOrder')
636
            ->will($this->returnValue(2));
637
        $stepsThree = $this->getStepMock('step3');
638
        $stepsThree->expects($this->any())
639
            ->method('getOrder')
640
            ->will($this->returnValue(2));
641
642
        $workflow = $this->createWorkflow();
643
        $workflow->getStepManager()->setSteps(array($stepsOne, $stepsTwo, $stepsThree));
644
645
        $passedSteps = $workflow->getPassedStepsByWorkflowItem($workflowItem);
646
        $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $passedSteps);
647
        $actual = array();
648
        /** @var Step $step */
649
        foreach ($passedSteps as $step) {
650
            $actual[] = $step->getName();
651
        }
652
        $this->assertEquals($expected, $actual);
653
    }
654
655
    public function passedStepsDataProvider()
656
    {
657
        return array(
658
            array(
659
                array(
660
                    $this->getTransitionRecordMock('step1')
661
                ),
662
                array('step1')
663
            ),
664
            array(
665
                array(
666
                    $this->getTransitionRecordMock('step1'),
667
                    $this->getTransitionRecordMock('step2'),
668
                ),
669
                array('step1', 'step2')
670
            ),
671
            array(
672
                array(
673
                    $this->getTransitionRecordMock('step1'),
674
                    $this->getTransitionRecordMock('step2'),
675
                    $this->getTransitionRecordMock('step3'),
676
                    $this->getTransitionRecordMock('step1'),
677
                    $this->getTransitionRecordMock('step2'),
678
                ),
679
                array('step1', 'step2')
680
            ),
681
            array(
682
                array(
683
                    $this->getTransitionRecordMock('step1'),
684
                    $this->getTransitionRecordMock('step2'),
685
                    $this->getTransitionRecordMock('step3'),
686
                    $this->getTransitionRecordMock('step1'),
687
                    $this->getTransitionRecordMock('step3'),
688
                ),
689
                array('step1', 'step3')
690
            ),
691
            array(
692
                array(
693
                    $this->getTransitionRecordMock('step1'),
694
                    $this->getTransitionRecordMock('step2'),
695
                    $this->getTransitionRecordMock('step3'),
696
                    $this->getTransitionRecordMock('step1'),
697
                    $this->getTransitionRecordMock('step2'),
698
                    $this->getTransitionRecordMock('step1'),
699
                    $this->getTransitionRecordMock('step2'),
700
                    $this->getTransitionRecordMock('step1'),
701
                    $this->getTransitionRecordMock('step3'),
702
                ),
703
                array('step1', 'step3')
704
            ),
705
            array(
706
                array(
707
                    $this->getTransitionRecordMock('step1'),
708
                    $this->getTransitionRecordMock('step2'),
709
                    $this->getTransitionRecordMock('step3'),
710
                    $this->getTransitionRecordMock('step1'),
711
                    $this->getTransitionRecordMock('step2'),
712
                    $this->getTransitionRecordMock('step1'),
713
                    $this->getTransitionRecordMock('step2'),
714
                    $this->getTransitionRecordMock('step1'),
715
                    $this->getTransitionRecordMock('step1'),
716
                    $this->getTransitionRecordMock('step3'),
717
                    $this->getTransitionRecordMock('step3'),
718
                ),
719
                array('step1', 'step3')
720
            ),
721
            array(
722
                array(
723
                    $this->getTransitionRecordMock('step1'),
724
                    $this->getTransitionRecordMock('step3'),
725
                    $this->getTransitionRecordMock('step2'),
726
                ),
727
                array('step1', 'step3', 'step2')
728
            ),
729
        );
730
    }
731
732
    protected function getTransitionRecordMock($stepToName)
733
    {
734
        $workflowStep = new WorkflowStep();
735
        $workflowStep->setName($stepToName);
736
737
        $record = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Entity\WorkflowTransitionRecord')
738
            ->disableOriginalConstructor()
739
            ->getMock();
740
        $record->expects($this->any())
741
            ->method('getStepTo')
742
            ->will($this->returnValue($workflowStep));
743
        return $record;
744
    }
745
746
    /**
747
     * @param null|string $workflowName
748
     * @return Workflow
749
     */
750
751
    /**
752
     * @param string $workflowName
753
     * @param EntityConnector $entityConnector
754
     * @param AclManager $aclManager
755
     * @param AttributeManager $attributeManager
756
     * @param TransitionManager $transitionManager
757
     * @return Workflow
758
     */
759
    protected function createWorkflow(
760
        $workflowName = null,
761
        $entityConnector = null,
762
        $aclManager = null,
763
        $attributeManager = null,
764
        $transitionManager = null
765
    ) {
766
        if (!$entityConnector) {
767
            $entityConnector = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Model\EntityConnector')
768
                ->disableOriginalConstructor()
769
                ->getMock();
770
        }
771
772
        if (!$aclManager) {
773
            $aclManager = $this->getMockBuilder('Oro\Bundle\WorkflowBundle\Acl\AclManager')
774
                ->disableOriginalConstructor()
775
                ->getMock();
776
        }
777
778
        $workflow = new Workflow($entityConnector, $aclManager, null, $attributeManager, $transitionManager);
779
        $workflow->setName($workflowName);
780
        return $workflow;
781
    }
782
783
    public function testGetAttributesMapping()
784
    {
785
        $attributeOne = $this->getMockBuilder('Oro\Bundle\ActionBundle\Model\Attribute')
786
            ->getMock();
787
        $attributeOne->expects($this->once())
788
            ->method('getPropertyPath');
789
        $attributeOne->expects($this->never())
790
            ->method('getName');
791
        $attributeTwo = $this->getMockBuilder('Oro\Bundle\ActionBundle\Model\Attribute')
792
            ->getMock();
793
        $attributeTwo->expects($this->atLeastOnce())
794
            ->method('getPropertyPath')
795
            ->will($this->returnValue('path'));
796
        $attributeTwo->expects($this->once())
797
            ->method('getName')
798
            ->will($this->returnValue('name'));
799
800
        $attributes = array($attributeOne, $attributeTwo);
801
        $attributeManager = $this->getMockBuilder('Oro\Bundle\ActionBundle\Model\AttributeManager')
802
            ->disableOriginalConstructor()
803
            ->getMock();
804
        $attributeManager->expects($this->once())
805
            ->method('getAttributes')
806
            ->will($this->returnValue($attributes));
807
        $workflow = $this->createWorkflow(null, null, null, $attributeManager);
808
        $expected = array('name' => 'path');
809
        $this->assertEquals($expected, $workflow->getAttributesMapping());
810
    }
811
}
812