Skip to content

Commit 4872528

Browse files
committed
started tests implementation, code formatting fixes
1 parent 09c3a0b commit 4872528

8 files changed

Lines changed: 226 additions & 12 deletions

File tree

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
"autoload": {
1313
"psr-4": {
1414
"Coff\\SMF\\": "src/",
15+
"Coff\\SMF\\Test\\": "tests/",
1516
"Coff\\SMF\\Example\\": "example/"
1617
}
1718
},
1819
"require": {
1920
"php": "^7.0",
2021
"garoevans/php-enum": "^1.2"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^6.5"
2125
}
2226
}

example/example01.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
namespace Coff\SMF\Example;
44

55
use Coff\SMF\Assertion\AlwaysFalseAssertion;
6+
use Coff\SMF\Exception\MachineException;
7+
use Coff\SMF\Exception\TransitionException;
68
use Coff\SMF\Machine;
79
use Coff\SMF\StateEnum;
810
use Coff\SMF\Transition\Transition;
911

10-
include (__DIR__ . '/../vendor/autoload.php');
12+
include(__DIR__ . '/../vendor/autoload.php');
1113

14+
/**
15+
* @method static DRAFT()
16+
* @method static SENT()
17+
* @method static VOTED()
18+
* @method static ACCEPTED()
19+
* @method static REJECTED()
20+
* @method static CANCELED()
21+
*/
1222
class PetitionEnum extends StateEnum
1323
{
1424
const __default = self::DRAFT,
@@ -35,20 +45,27 @@ public function init()
3545
// prevents changing state upon assertion when AlwaysFalseAssertion is given
3646
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT(), new AlwaysFalseAssertion())
3747
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED(), new AlwaysFalseAssertion())
38-
3948
// when no Assertion is given uses DefaultCallbackAssertion which calls assertXToY methods
4049
->allowTransition(PetitionEnum::SENT(), PetitionEnum::VOTED())
4150
->allowTransition(PetitionEnum::VOTED(), PetitionEnum::ACCEPTED())
4251
->allowTransition(PetitionEnum::VOTED(), PetitionEnum::REJECTED());
4352

4453
}
4554

55+
/**
56+
* @throws MachineException
57+
* @throws TransitionException
58+
*/
4659
public function send()
4760
{
4861
// shall throw an exception if current state is not DRAFT because it wasn't allowed transition
4962
$this->setMachineState(PetitionEnum::SENT());
5063
}
5164

65+
/**
66+
* @throws MachineException
67+
* @throws TransitionException
68+
*/
5269
public function cancel()
5370
{
5471
// shall throw an exception if current state is not DRAFT because it wasn't allowed transition

phpunit.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Application Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">app/</directory>
20+
</whitelist>
21+
</filter>
22+
<php>
23+
<env name="AUTH_TOKEN" value="correctToken"/>
24+
</php>
25+
</phpunit>

src/Assertion/DefaultCallbackAssertion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function make(): bool
4545
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface) {
4646
return call_user_func_array([
4747
$this->object,
48-
'assert' . ucfirst($this->transition->getFromState()) . 'To' . ucfirst($this->transition->getToState())
48+
'assert' . ucfirst($this->transition->getFromState()) . 'To' . ucfirst($this->transition->getToState()),
4949
], [$this->transition]);
5050
} else {
5151
throw new AssertionException('Callback not configured!');

src/Machine.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ public function getTransition(StateEnum $stateFrom, StateEnum $stateTo)
171171
}
172172
}
173173

174+
/**
175+
* Returns machine's initial state
176+
* @return StateEnum
177+
*/
178+
public function getInitState(): StateEnum
179+
{
180+
return $this->initState;
181+
}
182+
174183
/**
175184
* Sets machine's initial state
176185
* @param StateEnum $state
@@ -187,15 +196,6 @@ public function setInitState(StateEnum $state)
187196
return $this;
188197
}
189198

190-
/**
191-
* Returns machine's initial state
192-
* @return StateEnum
193-
*/
194-
public function getInitState() : StateEnum
195-
{
196-
return $this->initState;
197-
}
198-
199199
/**
200200
* Default handler method for CommonCallbackAssertion
201201
* @param Transition $transition

tests/MachineTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

tests/SampleMachine.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
4+
namespace Coff\SMF\Test;
5+
6+
7+
use Coff\SMF\Machine;
8+
use Coff\SMF\StateEnum;
9+
10+
class SampleMachine extends Machine
11+
{
12+
public function setMachineState(StateEnum $newState)
13+
{
14+
return parent::setMachineState($newState); // TODO: Change the autogenerated stub
15+
}
16+
}

tests/SampleStateEnum.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Coff\SMF\Test;
5+
6+
7+
use Coff\SMF\StateEnum;
8+
9+
/**
10+
* @method static ONE()
11+
* @method static TWO()
12+
* @method static THREE()
13+
*/
14+
class SampleStateEnum extends StateEnum
15+
{
16+
const __default = self::ONE,
17+
ONE = 'one',
18+
TWO = 'two',
19+
THREE = 'three';
20+
}

0 commit comments

Comments
 (0)