/
WorkflowExtensionTest.php
133 lines (110 loc) · 4.66 KB
/
WorkflowExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig\Tests\Extension;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
class WorkflowExtensionTest extends TestCase
{
private $extension;
private $t1;
protected function setUp()
{
if (!class_exists(Workflow::class)) {
$this->markTestSkipped('The Workflow component is needed to run tests for this extension.');
}
$places = ['ordered', 'waiting_for_payment', 'processed'];
$transitions = [
$this->t1 = new Transition('t1', 'ordered', 'waiting_for_payment'),
new Transition('t2', 'waiting_for_payment', 'processed'),
];
$metadataStore = null;
if (class_exists(InMemoryMetadataStore::class)) {
$transitionsMetadata = new \SplObjectStorage();
$transitionsMetadata->attach($this->t1, ['title' => 't1 title']);
$metadataStore = new InMemoryMetadataStore(
['title' => 'workflow title'],
['orderer' => ['title' => 'ordered title']],
$transitionsMetadata
);
}
$definition = new Definition($places, $transitions, null, $metadataStore);
$workflow = new Workflow($definition, new MethodMarkingStore());
$registry = new Registry();
$addWorkflow = method_exists($registry, 'addWorkflow') ? 'addWorkflow' : 'add';
$supportStrategy = class_exists(InstanceOfSupportStrategy::class)
? new InstanceOfSupportStrategy(Subject::class)
: new ClassInstanceSupportStrategy(Subject::class);
$registry->$addWorkflow($workflow, $supportStrategy);
$this->extension = new WorkflowExtension($registry);
}
public function testCanTransition()
{
$subject = new Subject();
$this->assertTrue($this->extension->canTransition($subject, 't1'));
$this->assertFalse($this->extension->canTransition($subject, 't2'));
}
public function testGetEnabledTransitions()
{
$subject = new Subject();
$transitions = $this->extension->getEnabledTransitions($subject);
$this->assertCount(1, $transitions);
$this->assertInstanceOf(Transition::class, $transitions[0]);
$this->assertSame('t1', $transitions[0]->getName());
}
public function testHasMarkedPlace()
{
$subject = new Subject(['ordered' => 1, 'waiting_for_payment' => 1]);
$this->assertTrue($this->extension->hasMarkedPlace($subject, 'ordered'));
$this->assertTrue($this->extension->hasMarkedPlace($subject, 'waiting_for_payment'));
$this->assertFalse($this->extension->hasMarkedPlace($subject, 'processed'));
}
public function testGetMarkedPlaces()
{
$subject = new Subject(['ordered' => 1, 'waiting_for_payment' => 1]);
$this->assertSame(['ordered', 'waiting_for_payment'], $this->extension->getMarkedPlaces($subject));
$this->assertSame($subject->getMarking(), $this->extension->getMarkedPlaces($subject, false));
}
public function testGetMetadata()
{
if (!class_exists(InMemoryMetadataStore::class)) {
$this->markTestSkipped('This test requires symfony/workflow:4.1.');
}
$subject = new Subject();
$this->assertSame('workflow title', $this->extension->getMetadata($subject, 'title'));
$this->assertSame('ordered title', $this->extension->getMetadata($subject, 'title', 'orderer'));
$this->assertSame('t1 title', $this->extension->getMetadata($subject, 'title', $this->t1));
$this->assertNull($this->extension->getMetadata($subject, 'not found'));
$this->assertNull($this->extension->getMetadata($subject, 'not found', $this->t1));
}
}
final class Subject
{
private $marking;
public function __construct($marking = null)
{
$this->marking = $marking;
}
public function getMarking()
{
return $this->marking;
}
public function setMarking($marking)
{
$this->marking = $marking;
}
}