-
Notifications
You must be signed in to change notification settings - Fork 3
/
refactoring-2.phpt
119 lines (91 loc) · 2.11 KB
/
refactoring-2.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
<?php declare(strict_types=1);
/**
* @see README.md
*/
namespace Grifart\Enum\Example\OrderState\__test_refactoring_2;
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';
}
class OrderService
{
public function canDoTransition(OrderState $currentState, OrderState $desiredState): bool
{
if ($currentState === OrderState::RECEIVED()) {
return $desiredState === OrderState::PROCESSING() || $desiredState === OrderState::CANCELLED();
}
if ($currentState === OrderState::PROCESSING()) {
return $desiredState === OrderState::FINISHED();
}
if ($currentState === OrderState::FINISHED()) {
return FALSE;
}
if ($currentState === OrderState::CANCELLED()) {
return FALSE;
}
throw new \LogicException('Should not happen: Unknown state');
}
}
$orderService = new OrderService();
// Standard order flow:
Assert::true(
$orderService->canDoTransition(
OrderState::RECEIVED(),
OrderState::PROCESSING()
)
);
Assert::true(
$orderService->canDoTransition(
OrderState::PROCESSING(),
OrderState::FINISHED()
)
);
// Cancellation order flow
Assert::true(
$orderService->canDoTransition(
OrderState::RECEIVED(),
OrderState::CANCELLED()
)
);
// Reflexivity test
Assert::false(
$orderService->canDoTransition(
OrderState::CANCELLED(),
OrderState::CANCELLED()
)
);
// --- NEGATIVE TESTS ---
// Invalid order flow
Assert::false(
$orderService->canDoTransition(
OrderState::RECEIVED(),
OrderState::FINISHED()
)
);
Assert::false(
$orderService->canDoTransition(
OrderState::PROCESSING(),
OrderState::CANCELLED()
)
);
Assert::false(
$orderService->canDoTransition(
OrderState::FINISHED(),
OrderState::CANCELLED()
)
);