Skip to content

Commit a2de768

Browse files
committed
rewrite exception tests
1 parent da3ddb5 commit a2de768

File tree

6 files changed

+86
-68
lines changed

6 files changed

+86
-68
lines changed

CHANGE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# version 1.2.0_WIP
2+
- upgrade to Yii 2.0.13 dependency
3+
- **about Events** : Since Yii v2.0.14 it is possible to *specify event name as a wildcard pattern*
4+
```PHP
5+
$component->on('event.group.*', function ($event) {
6+
Yii::trace($event->name . ' is triggered.');
7+
});
8+
```
9+
This may cause a problem if you are using the `ExtendedEventSequence` model because it is making use of the wildcard characters to name events (for example : beforeEnterWorkflow(\*)). Consequently if you set an event handler for *beforeEnterWorkflow(MY_WORKFLOW)* event, the handler will be invoked twice : once for event \ `beforeEnterWorkflow(MY_WORKFLOW)` and once for event `beforeEnterWorkflow(*)`
10+
111
# version 1.1.0
212
- Add `ChangeStatusAction` (from https://github.com/raoul2000/yii2-workflow/pull/24)
313

tests/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ Check XDebug is installed :
5656
- run `php -i > info.txt`
5757
- copy/paste `info.txt` into [this form](https://xdebug.org/wizard.php) and check the result
5858
- if needed, follow *Installation instructions*.
59+
60+
### More ...
61+
62+
Change PATH to use PHP v7.2.5 :
63+
```
64+
PATH=$(echo $PATH | sed 's/php7.0.10/php7.2.5/g')
65+
```

tests/codeception/unit/workflow/base/StatusObjectTest.php

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
class StatusObjectTest extends TestCase
1616
{
1717
use\Codeception\Specify;
18-
18+
1919
protected function setUp()
2020
{
2121
parent::setUp();
2222
$this->src = new WorkflowFileSource();
2323
}
24-
24+
2525
public function testStatusCreationSuccess()
2626
{
2727
$this->specify('create a status instance', function ()
@@ -74,7 +74,7 @@ public function testUnknownMetadata()
7474
{
7575
$this->specify('status creation fails when no id is provided', function ()
7676
{
77-
$this->setExpectedException('raoul2000\workflow\base\WorkflowException');
77+
$this->expectException('raoul2000\workflow\base\WorkflowException');
7878

7979
$s = new Status([
8080
'id' => 'draft',
@@ -93,7 +93,7 @@ public function testMissingId()
9393
{
9494
$this->specify('status creation fails when no id is provided', function ()
9595
{
96-
$this->setExpectedException('yii\base\InvalidConfigException');
96+
$this->expectException('yii\base\InvalidConfigException');
9797

9898
new Status([
9999
'workflowId' => 'workflow1'
@@ -144,15 +144,15 @@ public function testNullWorkflowId()
144144
]);
145145
});
146146
}
147-
147+
148148
public function testCreateWithSourceSuccess()
149149
{
150150
$this->specify('create a status instance with source component', function ()
151151
{
152152
$src = Yii::createObject([
153153
'class' => WorkflowFileSource::className()
154154
]);
155-
155+
156156
$start = new Status([
157157
'id' => 'draft',
158158
'workflowId' => 'workflow1',
@@ -168,35 +168,31 @@ public function testCreateWithSourceFails1()
168168
$this->specify('create a status instance with an invalid source component', function ()
169169
{
170170
$src = new \stdClass();
171-
172-
$this->setExpectedException(
173-
'yii\base\InvalidConfigException',
174-
'The "source" property must implement interface raoul2000\workflow\source\IWorkflowSource'
175-
);
171+
172+
$this->expectException('yii\base\InvalidConfigException');
173+
$this->expectExceptionMessage('The "source" property must implement interface raoul2000\workflow\source\IWorkflowSource');
176174
$start = new Status([
177175
'id' => 'draft',
178176
'workflowId' => 'workflow1',
179177
'source' => $src
180178
]);
181-
});
179+
});
182180
}
183-
181+
184182
public function testCreateWithSourceFails2()
185183
{
186184
$this->specify('create a status instance with an invalid source component', function ()
187185
{
188-
$this->setExpectedException(
189-
'yii\base\InvalidConfigException',
190-
'The "source" property must implement interface raoul2000\workflow\source\IWorkflowSource'
191-
);
186+
$this->expectException('yii\base\InvalidConfigException');
187+
$this->expectExceptionMessage('The "source" property must implement interface raoul2000\workflow\source\IWorkflowSource');
192188
new Status([
193189
'id' => 'draft',
194190
'workflowId' => 'workflow1',
195191
'source' => ''
196192
]);
197193
});
198-
}
199-
194+
}
195+
200196
public function testStatusAccessorSuccess()
201197
{
202198
$this->src->addWorkflowDefinition('wid', [
@@ -212,71 +208,69 @@ public function testStatusAccessorSuccess()
212208
]);
213209
$w = $this->src->getWorkflow('wid');
214210
verify_that($w != null);
215-
211+
216212
$this->specify('transitions can be obtained through status',function() {
217-
213+
218214
$status = $this->src->getStatus('wid/A');
219-
215+
220216
expect_that($status != null);
221-
217+
222218
$tr = $status->getTransitions();
223-
219+
224220
expect_that(is_array($tr));
225221
expect(count($tr))->equals(2);
226-
222+
227223
$keys = array_keys($tr);
228-
224+
229225
expect($keys)->equals(['wid/B','wid/C']);
230226
expect_that( $tr['wid/B'] instanceof TransitionInterface);
231227
expect_that( $tr['wid/C'] instanceof TransitionInterface);
232228
});
233-
229+
234230
$this->specify('parent workflow can be obtained through status',function() {
235-
231+
236232
$status = $this->src->getStatus('wid/A');
237-
233+
238234
expect_that($status != null);
239-
235+
240236
$wrk = $status->getWorkflow();
241-
237+
242238
expect_that($wrk != null);
243239
verify_that( $wrk instanceof WorkflowInterface);
244240
verify($wrk->getId())->equals('wid');
245-
});
246-
}
247-
241+
});
242+
}
243+
248244
public function testStatusAccessorFails()
249245
{
250246
$st = new Status([
251247
'id' => 'draft',
252248
'workflowId' => 'workflow1'
253249
]);
254-
250+
255251
$this->specify('Failed to get transitions when no source is configured', function () use($st)
256252
{
257-
$this->setExpectedException(
258-
'raoul2000\workflow\base\WorkflowException',
259-
'no workflow source component available'
260-
);
253+
$this->expectException('raoul2000\workflow\base\WorkflowException');
254+
$this->expectExceptionMessage('no workflow source component available');
261255
$st->getTransitions();
262256
});
263-
257+
264258
$this->specify('Failed to get workflow object when no source is configured', function () use($st)
265259
{
266-
$this->setExpectedException(
267-
'raoul2000\workflow\base\WorkflowException',
268-
'no workflow source component available'
269-
);
260+
$this->expectException('raoul2000\workflow\base\WorkflowException');
261+
$this->expectExceptionMessage('no workflow source component available');
270262
$st->getWorkflow();
271-
});
272-
263+
});
264+
273265
$this->specify('Failed to call isInitialStatus when no source is configured', function () use($st)
274266
{
275-
$this->setExpectedException(
276-
'raoul2000\workflow\base\WorkflowException',
267+
$this->expectException(
268+
'raoul2000\workflow\base\WorkflowException'
269+
);
270+
$this->expectExceptionMessage(
277271
'no workflow source component available'
278272
);
279273
$st->getWorkflow();
280-
});
281-
}
274+
});
275+
}
282276
}

tests/codeception/unit/workflow/base/TransitionObjectTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ public function testEmptyStartStatusFails()
4242
{
4343
$this->specify('create transition with NULL start status fails', function ()
4444
{
45-
$this->setExpectedException(
46-
'yii\base\InvalidConfigException',
47-
'missing start status'
48-
);
45+
$this->expectException('yii\base\InvalidConfigException');
46+
$this->expectExceptionMessage('missing start status');
4947
new Transition([
5048
'start' => null,
5149
'end' => new Status([
@@ -60,8 +58,10 @@ public function testMissingStartStatusFails()
6058

6159
$this->specify('create transition with no start status provided fails ', function ()
6260
{
63-
$this->setExpectedException(
64-
'yii\base\InvalidConfigException',
61+
$this->expectException(
62+
'yii\base\InvalidConfigException'
63+
);
64+
$this->expectExceptionMessage(
6565
'missing start status'
6666
);
6767
new Transition([
@@ -77,8 +77,10 @@ public function testNotStatusStartStatusFails()
7777

7878
$this->specify('create transition with start status not Status instance fails ', function ()
7979
{
80-
$this->setExpectedException(
81-
'raoul2000\workflow\base\WorkflowException',
80+
$this->expectException(
81+
'raoul2000\workflow\base\WorkflowException'
82+
);
83+
$this->expectExceptionMessage(
8284
'Start status object must implement raoul2000\workflow\base\StatusInterface'
8385
);
8486
new Transition([
@@ -90,8 +92,10 @@ public function testMissingEndStatusFails()
9092
{
9193
$this->specify('create transition with no end status provided fails', function ()
9294
{
93-
$this->setExpectedException(
94-
'yii\base\InvalidConfigException',
95+
$this->expectException(
96+
'yii\base\InvalidConfigException'
97+
);
98+
$this->expectExceptionMessage(
9599
'missing end status'
96100
);
97101
new Transition([

tests/codeception/unit/workflow/behavior/EnterWorkflowTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function setup()
2828
'definitionLoader' => [
2929
'class' => 'raoul2000\workflow\source\file\PhpClassLoader',
3030
'namespace' => 'tests\codeception\unit\models'
31-
]
31+
]
3232
]);
3333
}
3434

@@ -68,7 +68,8 @@ public function testEnterWorkflowFails1()
6868
verify('current status is not set',$item->hasWorkflowStatus())->false();
6969
$item->sendToStatus('Item04Workflow/A');
7070
verify('current status is set',$item->hasWorkflowStatus())->true();
71-
$this->setExpectedException('raoul2000\workflow\base\WorkflowException', 'Model already in a workflow');
71+
$this->expectException('raoul2000\workflow\base\WorkflowException');
72+
$this->expectExceptionMessage('Model already in a workflow');
7273
$item->enterWorkflow();
7374
});
7475
}

tests/codeception/unit/workflow/events/ChangeStatusExtendedEventTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public function testChangeStatusEventOnSaveSuccess()
5454
$this->model->on(
5555
WorkflowEvent::beforeEnterStatus(),
5656
function($event) {
57-
codecept_debug("beforeEnterStatus");
5857
$this->eventsBefore[] = $event;
5958
}
6059
);
@@ -64,20 +63,23 @@ function($event) {
6463
$this->eventsAfter[] = $event;
6564
}
6665
);
67-
codecept_debug(count($this->eventsBefore));
6866
verify('event handler handlers have been called', count($this->eventsBefore) == 0 && count($this->eventsAfter) == 0)->true();
6967

7068
$this->model->enterWorkflow();
7169
verify('current status is set',$this->model->hasWorkflowStatus())->true();
72-
codecept_debug(count($this->eventsBefore));
73-
expect('event handler handlers have been called', count($this->eventsBefore) == 1 && count($this->eventsAfter) == 1)->true();
70+
71+
// NOTE: Since Yii v2.0.14 it is possible to specify event name as a wildcard pattern
72+
// This causes handler to be called twice because the ExtendedEventSequence includes events name
73+
// that contains the '*' character : beforeEnterWorkflow{*}, beforeEnterStatus{*}
74+
//
75+
expect('event handler handlers have been called', count($this->eventsBefore) == 2 && count($this->eventsAfter) == 2)->true();
7476

7577
$this->model->status = 'Item04Workflow/B';
7678
verify('save succeeds',$this->model->save())->true();
7779

7880
expect('model has changed to status B',$this->model->getWorkflowStatus()->getId())->equals('Item04Workflow/B');
79-
expect('beforeChangeStatus handler has been called',count($this->eventsBefore))->equals(2);
80-
expect('afterChangeStatus handler has been called',count($this->eventsAfter))->equals(2);
81+
expect('beforeChangeStatus handler has been called',count($this->eventsBefore))->equals(4);
82+
expect('afterChangeStatus handler has been called',count($this->eventsAfter))->equals(4);
8183
}
8284

8385
public function testChangeStatusEventOnSaveFails()

0 commit comments

Comments
 (0)