-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrefactoring-3.phpt
124 lines (87 loc) · 2.06 KB
/
refactoring-3.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php declare(strict_types=1);
/**
* Logic moved into enum
* @see README.md
*/
namespace Grifart\Enum\Example\__test_refactoring_3;
require __DIR__ . '/../../bootstrap.php';
use Grifart\Enum\Enum;
use Tester\Assert;
final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PROCESSING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
final class OrderState extends Enum
{
use \Grifart\Enum\AutoInstances;
protected const
RECEIVED = 'received',
PROCESSING = 'processing',
FINISHED = 'finished',
CANCELLED = 'cancelled';
public function canDoTransition(OrderState $desiredState): bool
{
if ($this === self::RECEIVED()) {
return $desiredState === self::PROCESSING() || $desiredState === self::CANCELLED();
}
if ($this === self::PROCESSING()) {
return $desiredState === self::FINISHED();
}
if ($this === self::FINISHED()) {
return FALSE;
}
if ($this === self::CANCELLED()) {
return FALSE;
}
throw new \LogicException('Should not happen: Unknown state');
}
}
// Standard order flow:
Assert::true(
OrderState::RECEIVED()->canDoTransition(
OrderState::PROCESSING()
)
);
Assert::true(
OrderState::PROCESSING()->canDoTransition(
OrderState::FINISHED()
)
);
// Cancellation order flow
Assert::true(
OrderState::RECEIVED()->canDoTransition(
OrderState::CANCELLED()
)
);
// Non-reflexivity test
Assert::false(
OrderState::CANCELLED()->canDoTransition(
OrderState::CANCELLED()
)
);
// --- NEGATIVE TESTS ---
// Invalid order flow
Assert::false(
OrderState::RECEIVED()->canDoTransition(
OrderState::FINISHED()
)
);
Assert::false(
OrderState::PROCESSING()->canDoTransition(
OrderState::CANCELLED()
)
);
Assert::false(
OrderState::FINISHED()->canDoTransition(
OrderState::CANCELLED()
)
);
$state1 = OrderState::RECEIVED();
$state2 = OrderState::RECEIVED();
Assert::true($state1 === $state2);
$state3 = OrderState::PROCESSING();
Assert::true($state1 !== $state3);
Assert::true($state2 !== $state3);