-
Notifications
You must be signed in to change notification settings - Fork 3
/
refactoring-1.phpt
109 lines (86 loc) · 2.2 KB
/
refactoring-1.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
<?php declare(strict_types=1);
/**
* This test shows implicit enum declaration.
* @see README.md
*/
namespace Grifart\Enum\Example\OrderState\__test_refactoring_1;
require __DIR__ . '/../../bootstrap.php';
use Tester\Assert;
final class InvalidTransitionException extends \RuntimeException {}
class OrderService
{
public const STATE_RECEIVED = 'received';
public const STATE_PROCESSING = 'processing';
public const STATE_FINISHED = 'finished';
public const STATE_CANCELLED = 'cancelled';
public function canDoTransition(string $currentState, string $desiredState): bool
{
if ($currentState === $desiredState) {
return TRUE;
}
switch ($currentState) {
case self::STATE_RECEIVED:
return $desiredState === self::STATE_PROCESSING || $desiredState === self::STATE_CANCELLED;
case self::STATE_PROCESSING:
return $desiredState === self::STATE_FINISHED;
case self::STATE_FINISHED:
return FALSE;
case self::STATE_CANCELLED:
return FALSE;
default:
throw new \LogicException('Should not happen: Unknown state');
}
}
}
$orderService = new OrderService();
// Standard order flow:
Assert::true(
$orderService->canDoTransition(
OrderService::STATE_RECEIVED,
OrderService::STATE_PROCESSING
)
);
Assert::true(
$orderService->canDoTransition(
OrderService::STATE_PROCESSING,
OrderService::STATE_FINISHED
)
);
// Cancellation order flow
Assert::true(
$orderService->canDoTransition(
OrderService::STATE_RECEIVED,
OrderService::STATE_CANCELLED
)
);
// Reflexivity test
Assert::true(
$orderService->canDoTransition(
OrderService::STATE_CANCELLED,
OrderService::STATE_CANCELLED
)
);
// --- NEGATIVE TESTS ---
// Invalid order flow
Assert::false(
$orderService->canDoTransition(
OrderService::STATE_RECEIVED,
OrderService::STATE_FINISHED
)
);
Assert::false(
$orderService->canDoTransition(
OrderService::STATE_PROCESSING,
OrderService::STATE_CANCELLED
)
);
Assert::false(
$orderService->canDoTransition(
OrderService::STATE_FINISHED,
OrderService::STATE_CANCELLED
)
);
// check for completely invalid arguments
Assert::exception(function () use ($orderService) {
$orderService->canDoTransition('invalid', 'non-existing');
}, \LogicException::class);