-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
163 lines (138 loc) · 3.93 KB
/
test.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
final class Account
{
public $status;
public $balance;
}
abstract class Event
{
/**
* @var Account
*/
public $account;
public function __construct(Account $account)
{
$this->account = $account;
}
}
class EventProcessor
{
public function addEvent(Event $event): void
{
switch (\get_class($event))
{
case OpenAccount::class:
/** @var OpenAccount $event */
$event->account->status = $event->status;
$event->account->balance = $event->balance;
return;
case DepositMoney::class:
/** @var DepositMoney $event */
$event->account->balance += $event->amount;
return;
case WithdrawMoney::class:
/** @var WithdrawMoney $event */
if ($event->amount > $event->account->balance) {
throw new \UnexpectedValueException('Insufficient funds');
}
$event->account->balance -= $event->amount;
return;
}
throw new \InvalidArgumentException(\sprintf('Unknown event: %s', \get_class($event)));
}
}
final class OpenAccount extends Event
{
/**
* @var string
*/
public $status;
/**
* @var int
*/
public $balance;
public function __construct(Account $account, string $status, int $balance)
{
parent::__construct($account);
$this->status = $status;
$this->balance = $balance;
}
}
final class DepositMoney extends Event
{
/**
* @var int
*/
public $amount;
public function __construct(Account $account, int $amount, string $title)
{
parent::__construct($account);
$this->amount = $amount;
}
}
final class WithdrawMoney extends Event
{
/**
* @var int
*/
public $amount;
public function __construct(Account $account, int $amount, string $title, string $method)
{
parent::__construct($account);
$this->amount = $amount;
}
}
class Test
{
public $testsExecuted = 0;
public $testsPassed = 0;
/**
* @var EventProcessor
*/
private $eventProcessor;
public function __construct()
{
$this->eventProcessor = new EventProcessor();
}
public function testOpensAccount()
{
$account = new Account();
$this->eventProcessor->addEvent(new OpenAccount($account, "OPEN", 0));
$this->assertEquals("OPEN", $account->status);
$this->assertEquals(0, $account->balance);
}
public function testDeposit()
{
$account = new Account();
$this->eventProcessor->addEvent(new OpenAccount($account, "OPEN", 0));
$this->eventProcessor->addEvent(new DepositMoney($account, 100, "Wpłata 1 zł"));
$this->assertEquals(100, $account->balance);
}
public function testWithdraw()
{
$account = new Account();
$this->eventProcessor->addEvent(new OpenAccount($account, "OPEN", 0));
$this->eventProcessor->addEvent(new DepositMoney($account, 100, "Wpłata 1 zł"));
$this->eventProcessor->addEvent(new DepositMoney($account, 200, "Wpłata 2 zł"));
$this->eventProcessor->addEvent(new WithdrawMoney($account, 50, "Wypłata środków", "bank_transfer"));
$this->assertEquals(250, $account->balance);
}
private function assertEquals($expected, $actual): bool
{
$this->testsExecuted++;
if ($expected != $actual) {
echo \sprintf('Assertion failed: %s is not equal %s', $expected, $actual) . PHP_EOL;
return false;
}
echo \sprintf('%s is equal %s', $expected, $actual) . PHP_EOL;
$this->testsPassed++;
return true;
}
}
$test = new Test();
$test->testOpensAccount();
$test->testDeposit();
$test->testWithdraw();
if ($test->testsPassed === $test->testsExecuted) {
echo 'All tests passed!';
}