testExecuteWithCustomizedExecuteActionMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Workflow;
3
4
use Fwlib\Workflow\ModelInterface;
5
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
6
use Fwlib\Workflow\AbstractManager;
7
8
/**
9
 * @copyright   Copyright 2014-2015 Fwolf
10
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
11
 */
12
class AbstractManagerTest extends PHPUnitTestCase
13
{
14
    protected function buildMock($uuid = '')
15
    {
16
        $workflow = $this->getMockBuilder(
17
            'FwlibTest\Workflow\AbstractManagerDummy'
18
        )
19
        ->setMethods([])
20
        ->setConstructorArgs([$uuid])
21
        ->getMockForAbstractClass();
22
23
        return $workflow;
24
    }
25
26
27
    public function testAccessors()
28
    {
29
        $workflow = $this->buildMock('uuid dummy');
30
31
        $this->assertEquals('uuid dummy', $workflow->getUuid());
32
        $this->assertNotEmpty($workflow->getCurrentNodeTitle());
33
        $this->assertNotEmpty($workflow->getModelClass());
34
        $this->assertInstanceOf(
35
            ModelInterface::class,
36
            $workflow->getModel()
37
        );
38
39
        // Get title for current result code
40
        $this->assertEquals('Not Ended', $workflow->getResultCodeTitle());
41
        // Get title with given result code
42
        $this->assertEquals(
43
            'Approved',
44
            $workflow->getResultCodeTitle($workflow::RESULT_CODE_APPROVED)
45
        );
46
47
        $workflow->setNodeActionTitle('start', 'edit', 'Edit Title Modified');
48
        $nodes = $this->reflectionGet($workflow, 'nodes');
49
        $this->assertEquals(
50
            'Edit Title Modified',
51
            $nodes['start']['actions']['edit']['title']
52
        );
53
54
        $workflowModel = $workflow->getModel();
55
        $workflow->setModel($workflowModel);
56
        $this->assertInstanceOf(
57
            ModelInterface::class,
58
            $workflow->getModel()
59
        );
60
    }
61
62
63
    public function testDisableEnableAction()
64
    {
65
        $workflow = $this->buildMock();
66
67
        $actionsOld = $workflow->getAvailableActions();
68
69
        $workflow->disableAction('notExist');
70
        $this->assertEqualArray(
71
            $actionsOld,
72
            $workflow->getAvailableActions()
73
        );
74
75
        $workflow->disableAction('submit');
76
        $actionsNew = $workflow->getAvailableActions();
77
        $this->assertEquals(count($actionsOld), count($actionsNew) + 1);
78
        $this->assertArrayNotHasKey('submit', $actionsNew);
79
80
        $workflow->enableAction('submit');
81
        $actionsNew = $workflow->getAvailableActions();
82
        // Actions order has been changed assertEqualArray() will fail
83
        $this->assertEquals($actionsOld, $actionsNew);
84
    }
85
86
87
    public function testDisableEnableActions()
88
    {
89
        $workflow = $this->buildMock();
90
91
        $actionsOld = $workflow->getAvailableActions();
92
93
        $workflow->disableActions(['notExist', 'submit']);
94
        $actionsNew = $workflow->getAvailableActions();
95
96
        $this->assertEquals(count($actionsOld), count($actionsNew) + 1);
97
        $this->assertArrayNotHasKey('submit', $actionsNew);
98
99
        $workflow->enableActions(['notExist', 'submit']);
100
        $actionsNew = $workflow->getAvailableActions();
101
        $this->assertEquals($actionsOld, $actionsNew);
102
    }
103
104
105
    public function testExecute()
106
    {
107
        $workflow = $this->buildMock();
108
109
        $contentData = ['key' => 'dummy'];
110
        $_POST = $contentData;
111
112
        $workflow->updateContents($_POST)->execute('submit');
113
114
        // Node is moved
115
        $this->assertEquals('end', $workflow->getCurrentNode());
116
117
        // ResultCode is approved
118
        $this->assertTrue($workflow->isApproved());
119
        $this->assertEquals('Approved', $workflow->getResultCodeTitle());
120
121
        // Content data is set
122
        $this->assertEquals('dummy', $workflow->getContent('key'));
123
        $this->assertEqualArray($contentData, $workflow->getContents());
124
125
        // Rollback
126
        $workflow->execute('rollback');
127
        $this->assertEquals('start', $workflow->getCurrentNode());
128
        $this->assertFalse($workflow->isApproved());
129
    }
130
131
132
    public function testExecuteWithCustomizedExecuteActionMethod()
133
    {
134
        $workflow = $this->buildMock('uuid');
135
136
        $this->assertFalse($workflow->isEnded());
137
138
        $workflow->execute('customizedAction');
139
140
        // Check the customized method is executed
141
        $this->assertEquals('changed', $workflow->getTitle());
142
143
        $this->assertTrue($workflow->isEnded());
144
        $this->assertEquals(
145
            $workflow::RESULT_CODE_REJECTED,
146
            $workflow->getResultCode()
147
        );
148
149
        // Uuid is generated
150
        $this->assertNotEmpty($workflow->getUuid());
151
    }
152
153
154
    /**
155
     * @expectedException \Fwlib\Workflow\Exception\InvalidActionException
156
     */
157
    public function testExecuteWithInvalidAction()
158
    {
159
        $workflow = $this->buildMock();
160
161
        $workflow->execute('actionNotExists');
162
    }
163
164
165
    /**
166
     * @expectedException \Fwlib\Workflow\Exception\InvalidActionException
167
     */
168
    public function testExecuteWithInvalidActionByReSubmit()
169
    {
170
        $workflow = $this->buildMock();
171
172
        $workflow->execute('submit');
173
        $workflow->execute('submit');
174
    }
175
176
177
    /**
178
     * @expectedException \Exception
179
     * @expectedExceptionMessage Invalid or not allowed action
180
     */
181
    public function testExecuteWithNotAvailableAction()
182
    {
183
        $workflow = $this->buildMock('dummyUuid');
184
185
        $workflow->execute('notAvailableAction');
186
    }
187
188
189
    /**
190
     * @expectedException \Exception
191
     * @expectedExceptionMessage invalid action
192
     */
193
    public function testGetActionTitle()
194
    {
195
        $workflow = $this->buildMock();
196
197
        $this->assertEquals('Submit', $workflow->getActionTitle('submit'));
198
199
        $workflow->getActionTitle('not exists action');
200
    }
201
202
203
    public function testGetAvailableAction()
204
    {
205
        $workflow = $this->buildMock();
206
207
        $availableAction = $workflow->getAvailableActions();
208
209
        $this->assertEqualArray(
210
            ['edit', 'submit', 'customizedAction'],
211
            array_keys($availableAction)
212
        );
213
214
215
        $workflow->execute('submit');
216
217
        // Simulate an end node without actions
218
        $nodes = $this->reflectionGet($workflow, 'nodes');
219
        unset($nodes['end']['actions']);
220
        $this->reflectionSet($workflow, 'nodes', $nodes);
221
222
        $this->assertEmpty($workflow->getAvailableActions());
223
    }
224
225
226
    public function testGetNotAvailableActions()
227
    {
228
        $workflow = $this->buildMock('dummyUuid');
229
230
        $workflow->getAvailableActions();
231
232
        $this->assertArrayHasKey(
233
            'notAvailableAction',
234
            $workflow->getNotAvailableActions()
235
        );
236
    }
237
238
239
    public function testGetWorkflowTitle()
240
    {
241
        $workflow = $this->buildMock();
242
243
        $this->assertEquals(
244
            'Workflow Title Dummy',
245
            $workflow->getWorkflowTitle()
246
        );
247
    }
248
249
250
    public function testLimitActions()
251
    {
252
        $workflow = $this->buildMock();
253
254
        $actionsOld = $workflow->getAvailableActions();
0 ignored issues
show
Unused Code introduced by
$actionsOld is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
255
256
        $workflow->limitActions(['notExist', 'edit', 'submit']);
257
        $actionsNew = $workflow->getAvailableActions();
258
259
        $this->assertEquals(2, count($actionsNew));
260
        $this->assertArrayHasKey('submit', $actionsNew);
261
262
        $workflow->enableAction('customizedAction');
263
        $actionsNew = $workflow->getAvailableActions();
264
        $this->assertArrayHasKey('customizedAction', $actionsNew);
265
    }
266
267
268
    /**
269
     * @expectedException \Exception
270
     * @expectedExceptionMessage end twice
271
     */
272
    public function testMoveToEndTwice()
273
    {
274
        $workflow = $this->buildMock();
275
276
        $workflow->execute('submit');
277
278
        // Normally if we execute 'submit' again, will fail because there has
279
        // no 'submit' in node 'end', so to simulate concurrence execute, we
280
        // use reflection to call move() directly.
281
        $this->reflectionCall($workflow, 'move', ['rollback', 'end', 'end']);
282
    }
283
}
284