Skip to content

Commit 766f924

Browse files
committed
further tests implemented, one fix: when no transitions defined for certain state
1 parent 4872528 commit 766f924

2 files changed

Lines changed: 188 additions & 13 deletions

File tree

src/Machine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,6 @@ public function getAllowedTransitions(StateEnum $state = null): array
252252
$state = $this->getMachineState();
253253
}
254254

255-
return $this->allowedTransitions[(string)$state];
255+
return isset($this->allowedTransitions[(string)$state]) ? $this->allowedTransitions[(string)$state] : [];
256256
}
257257
}

tests/MachineTest.php

Lines changed: 187 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
namespace Coff\SMF\Test;
55

66

7+
use Coff\SMF\Assertion\AlwaysFalseAssertion;
8+
use Coff\SMF\Assertion\AlwaysTrueAssertion;
9+
use Coff\SMF\Assertion\CommonCallbackAssertion;
10+
use Coff\SMF\Assertion\DefaultCallbackAssertion;
711
use Coff\SMF\Exception\MachineException;
812
use Coff\SMF\Exception\TransitionException;
913
use Coff\SMF\Transition\Transition;
@@ -15,13 +19,22 @@ class MachineTest extends TestCase
1519
protected $machine;
1620

1721
/** @var SampleStateEnum */
18-
protected $x, $y;
22+
protected $x, $y, $z;
1923

2024
public function setUp()
2125
{
2226
$this->machine = new SampleMachine();
2327
$this->x = SampleStateEnum::ONE();
2428
$this->y = SampleStateEnum::TWO();
29+
$this->z = SampleStateEnum::THREE();
30+
}
31+
32+
public function test_setInitState()
33+
{
34+
$this->machine->setInitState($this->x);
35+
36+
$this->assertInstanceOf(SampleStateEnum::class, $this->machine->getInitState());
37+
$this->assertEquals($this->x, $this->machine->getInitState());
2538
}
2639

2740
/**
@@ -51,14 +64,6 @@ public function test_setInitState_doesnt_set_state_when_has_state()
5164
$this->assertEquals($this->y, $this->machine->getInitState());
5265
}
5366

54-
public function test_setInitState()
55-
{
56-
$this->machine->setInitState($this->x);
57-
58-
$this->assertInstanceOf(SampleStateEnum::class, $this->machine->getInitState());
59-
$this->assertEquals($this->x, $this->machine->getInitState());
60-
}
61-
6267
/**
6368
* @depends test_setInitState
6469
*/
@@ -81,7 +86,7 @@ public function test_isMachineState_one_state_not_same_object()
8186
}
8287

8388
/**
84-
* @throws \Coff\SMF\Exception\MachineException
89+
* @throws MachineException
8590
*/
8691
public function test_allowTransition()
8792
{
@@ -111,8 +116,6 @@ public function test_setMachineState_not_allowed_transition()
111116
*/
112117
public function test_setMachineState_calls_onTransition()
113118
{
114-
115-
116119
$this->machine = $this->createPartialMock(SampleMachine::class, ['onTransition']);
117120

118121
$this->machine->allowTransition($this->x, $this->y);
@@ -128,5 +131,177 @@ public function test_setMachineState_calls_onTransition()
128131
$this->machine->setMachineState($this->y);
129132
}
130133

134+
/**
135+
* @depends test_setInitState
136+
*/
137+
public function test_isTransitionAllowed()
138+
{
139+
$this->machine->setInitState($this->x);
140+
$this->machine->allowTransition($this->x, $this->y);
141+
142+
$this->assertTrue($this->machine->isTransitionAllowed($this->y));
143+
}
144+
145+
/**
146+
* @depends test_setInitState
147+
*/
148+
public function test_isTransitionAllowed_not()
149+
{
150+
$this->machine->setInitState($this->x);
151+
$this->machine->allowTransition($this->x, $this->y);
152+
153+
$this->assertFalse($this->machine->isTransitionAllowed($this->z));
154+
}
131155

156+
/**
157+
* @depends test_setInitState
158+
*/
159+
public function test_isTransitionAllowed_same()
160+
{
161+
$this->machine->setInitState($this->x);
162+
163+
// can't transit to the same state
164+
$this->assertFalse($this->machine->isTransitionAllowed($this->x));
165+
}
166+
167+
/**
168+
* @depends test_setInitState
169+
* @depends test_allowTransition
170+
* @throws TransitionException
171+
*/
172+
public function test_run_automatic_transition_through_n_states_succ()
173+
{
174+
$this->machine->setInitState($this->x);
175+
176+
$this->machine->allowTransition($this->x, $this->y, new AlwaysTrueAssertion());
177+
$this->machine->allowTransition($this->y, $this->z, new AlwaysTrueAssertion());
178+
179+
$this->machine->run();
180+
181+
$this->assertTrue($this->machine->isMachineState($this->z));
182+
183+
}
184+
185+
/**
186+
* @depends test_setInitState
187+
* @depends test_allowTransition
188+
* @throws TransitionException
189+
*/
190+
public function test_run_automatic_transition_through_n_states_stops()
191+
{
192+
$this->machine->setInitState($this->x);
193+
194+
$this->machine->allowTransition($this->x, $this->y, new AlwaysTrueAssertion());
195+
$this->machine->allowTransition($this->y, $this->z, new AlwaysFalseAssertion());
196+
197+
$this->machine->run();
198+
199+
$this->assertFalse($this->machine->isMachineState($this->z));
200+
$this->assertTrue($this->machine->isMachineState($this->y));
201+
202+
}
203+
204+
/**
205+
* @depends test_allowTransition
206+
* @depends test_setInitState
207+
* @throws MachineException
208+
* @throws TransitionException
209+
*/
210+
public function test_run_DefaultCallbackAssertion_succ()
211+
{
212+
$this->machine = $this->createPartialMock(SampleMachine::class, ['assertOneToTwo']);
213+
214+
$this->machine->allowTransition($this->x, $this->y, new DefaultCallbackAssertion());
215+
216+
$transition = $this->machine->getTransition($this->x, $this->y);
217+
218+
$this->machine->expects($this->once())
219+
->method('assertOneToTwo')
220+
->with($this->equalTo($transition))
221+
->willReturn(true);
222+
223+
$this->machine->setInitState($this->x);
224+
225+
$this->machine->run();
226+
227+
$this->assertTrue($this->machine->isMachineState($this->y));
228+
}
229+
230+
/**
231+
* @depends test_allowTransition
232+
* @depends test_setInitState
233+
* @throws MachineException
234+
* @throws TransitionException
235+
*/
236+
public function test_run_DefaultCallbackAssertion_fail()
237+
{
238+
$this->machine = $this->createPartialMock(SampleMachine::class, ['assertOneToTwo']);
239+
240+
$this->machine->allowTransition($this->x, $this->y, new DefaultCallbackAssertion());
241+
242+
$transition = $this->machine->getTransition($this->x, $this->y);
243+
244+
$this->machine->expects($this->once())
245+
->method('assertOneToTwo')
246+
->with($this->equalTo($transition))
247+
->willReturn(false);
248+
249+
$this->machine->setInitState($this->x);
250+
251+
$this->machine->run();
252+
253+
$this->assertTrue($this->machine->isMachineState($this->x));
254+
}
255+
256+
/**
257+
* @depends test_allowTransition
258+
* @depends test_setInitState
259+
* @throws MachineException
260+
* @throws TransitionException
261+
*/
262+
public function test_run_CommonCallbackAssertion_succ()
263+
{
264+
$this->machine = $this->createPartialMock(SampleMachine::class, ['assertTransition']);
265+
266+
$this->machine->allowTransition($this->x, $this->y, new CommonCallbackAssertion());
267+
268+
$transition = $this->machine->getTransition($this->x, $this->y);
269+
270+
$this->machine->expects($this->once())
271+
->method('assertTransition')
272+
->with($this->equalTo($transition))
273+
->willReturn(true);
274+
275+
$this->machine->setInitState($this->x);
276+
277+
$this->machine->run();
278+
279+
$this->assertTrue($this->machine->isMachineState($this->y));
280+
}
281+
282+
/**
283+
* @depends test_allowTransition
284+
* @depends test_setInitState
285+
* @throws MachineException
286+
* @throws TransitionException
287+
*/
288+
public function test_run_CommonCallbackAssertion_fail()
289+
{
290+
$this->machine = $this->createPartialMock(SampleMachine::class, ['assertTransition']);
291+
292+
$this->machine->allowTransition($this->x, $this->y, new CommonCallbackAssertion());
293+
294+
$transition = $this->machine->getTransition($this->x, $this->y);
295+
296+
$this->machine->expects($this->once())
297+
->method('assertTransition')
298+
->with($this->equalTo($transition))
299+
->willReturn(false);
300+
301+
$this->machine->setInitState($this->x);
302+
303+
$this->machine->run();
304+
305+
$this->assertTrue($this->machine->isMachineState($this->x));
306+
}
132307
}

0 commit comments

Comments
 (0)