1+ <?php
2+
3+
4+ namespace Coff \SMF \Test ;
5+
6+
7+ use Coff \SMF \Exception \MachineException ;
8+ use Coff \SMF \Exception \TransitionException ;
9+ use Coff \SMF \Transition \Transition ;
10+ use PHPUnit \Framework \TestCase ;
11+
12+ class MachineTest extends TestCase
13+ {
14+ /** @var SampleMachine */
15+ protected $ machine ;
16+
17+ /** @var SampleStateEnum */
18+ protected $ x , $ y ;
19+
20+ public function setUp ()
21+ {
22+ $ this ->machine = new SampleMachine ();
23+ $ this ->x = SampleStateEnum::ONE ();
24+ $ this ->y = SampleStateEnum::TWO ();
25+ }
26+
27+ /**
28+ * @depends test_setInitState
29+ */
30+ public function test_setInitState_sets_state_on_no_state ()
31+ {
32+ $ this ->machine ->setInitState ($ this ->x );
33+
34+ $ this ->assertInstanceOf (SampleStateEnum::class, $ this ->machine ->getMachineState ());
35+ $ this ->assertEquals ($ this ->x , $ this ->machine ->getMachineState ());
36+ }
37+
38+ /**
39+ * @depends test_setInitState_sets_state_on_no_state
40+ */
41+ public function test_setInitState_doesnt_set_state_when_has_state ()
42+ {
43+
44+ // sets initial state and current
45+ $ this ->machine ->setInitState ($ this ->x );
46+
47+ // should only set initial state but not current one
48+ $ this ->machine ->setInitState ($ this ->y );
49+
50+ $ this ->assertEquals ($ this ->x , $ this ->machine ->getMachineState ());
51+ $ this ->assertEquals ($ this ->y , $ this ->machine ->getInitState ());
52+ }
53+
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+
62+ /**
63+ * @depends test_setInitState
64+ */
65+ public function test_isMachineState ()
66+ {
67+ $ this ->machine ->setInitState ($ x = SampleStateEnum::ONE ());
68+
69+ $ this ->assertEquals (true , $ this ->machine ->isMachineState ($ x ));
70+ }
71+
72+ /**
73+ * @depends test_setInitState
74+ * @depends test_isMachineState
75+ */
76+ public function test_isMachineState_one_state_not_same_object ()
77+ {
78+ $ this ->machine ->setInitState (SampleStateEnum::ONE ());
79+
80+ $ this ->assertEquals (true , $ this ->machine ->isMachineState (SampleStateEnum::ONE ()));
81+ }
82+
83+ /**
84+ * @throws \Coff\SMF\Exception\MachineException
85+ */
86+ public function test_allowTransition ()
87+ {
88+ $ this ->machine ->allowTransition ($ this ->x , $ this ->y );
89+
90+ $ this ->assertInstanceOf (Transition::class, $ this ->machine ->getTransition ($ this ->x , $ this ->y ));
91+ }
92+
93+ /**
94+ * @depends test_setInitState
95+ * @throws TransitionException
96+ * @throws MachineException
97+ */
98+ public function test_setMachineState_not_allowed_transition ()
99+ {
100+
101+ $ this ->machine ->setInitState (SampleStateEnum::ONE ());
102+ $ this ->expectException (TransitionException::class);
103+ $ this ->machine ->setMachineState (SampleStateEnum::TWO ());
104+ }
105+
106+ /**
107+ * @depends test_allowTransition
108+ * @depends test_setInitState
109+ * @throws MachineException
110+ * @throws TransitionException
111+ */
112+ public function test_setMachineState_calls_onTransition ()
113+ {
114+
115+
116+ $ this ->machine = $ this ->createPartialMock (SampleMachine::class, ['onTransition ' ]);
117+
118+ $ this ->machine ->allowTransition ($ this ->x , $ this ->y );
119+
120+ $ transition = $ this ->machine ->getTransition ($ this ->x , $ this ->y );
121+
122+ $ this ->machine ->expects ($ this ->once ())
123+ ->method ('onTransition ' )
124+ ->with ($ this ->equalTo ($ transition ));
125+
126+ $ this ->machine ->setInitState ($ this ->x );
127+
128+ $ this ->machine ->setMachineState ($ this ->y );
129+ }
130+
131+
132+ }
0 commit comments