1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Game; |
4
|
|
|
|
5
|
|
|
use App\Game\BlackJack; |
6
|
|
|
use App\Game\BlackJack\Dealer; |
7
|
|
|
use App\Game\BlackJack\Player; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use App\Cards\CardGraphic; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Test cases for class BlackJack. |
13
|
|
|
*/ |
14
|
|
|
class BlackJackTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* testCreateObject |
18
|
|
|
* |
19
|
|
|
* Construct object and verify that the object has the expected |
20
|
|
|
* properties, use no arguments. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function testCreateObject(): void |
25
|
|
|
{ |
26
|
|
|
$blackJack = new BlackJack(); |
27
|
|
|
$this->assertInstanceOf(BlackJack::class, $blackJack); |
28
|
|
|
|
29
|
|
|
$res = $blackJack->stateOfGame(); |
30
|
|
|
$this->assertEquals("1", $res["numOfPlayers"]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* testToFewPlayers |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function testToFewPlayers(): void |
39
|
|
|
{ |
40
|
|
|
$this->expectException(\RuntimeException::class); |
41
|
|
|
$this->expectExceptionMessage("Can't have less then one player in Black Jack"); |
42
|
|
|
new BlackJack(0); // Less than 1 |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* testToManyPlayers |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function testToManyPlayers(): void |
51
|
|
|
{ |
52
|
|
|
$this->expectException(\RuntimeException::class); |
53
|
|
|
$this->expectExceptionMessage('Maximum of ' . BlackJack::MAX_PLAYERS . ' players in Black Jack'); |
54
|
|
|
new BlackJack(BlackJack::MAX_PLAYERS + 1); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* testBooleans |
59
|
|
|
* |
60
|
|
|
* Test the boolean values |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function testBooleans(): void |
65
|
|
|
{ |
66
|
|
|
$blackJack = new BlackJack(); |
67
|
|
|
|
68
|
|
|
$playerBust = $blackJack->isPlayerBust(); |
69
|
|
|
$this->assertEquals(false, $playerBust); |
70
|
|
|
|
71
|
|
|
$dealerBust = $blackJack->isDealerBust(); |
72
|
|
|
$this->assertEquals(false, $dealerBust); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* testHit |
77
|
|
|
* |
78
|
|
|
* Test the hit action of the player |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function testHit(): void |
83
|
|
|
{ |
84
|
|
|
$blackJack = new BlackJack(); |
85
|
|
|
|
86
|
|
|
$blackJack->hitPlayer(); |
87
|
|
|
$res = $blackJack->stateOfGame(); |
88
|
|
|
|
89
|
|
|
$this->assertEquals(3, count($res["playersCards"][0])); |
90
|
|
|
|
91
|
|
|
while ($blackJack->isPlayerBust() === false) { |
92
|
|
|
$blackJack->hitPlayer(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$res = $blackJack->stateOfGame(); |
96
|
|
|
$this->assertEquals("Dealer", $res["gameStates"][0]); |
97
|
|
|
|
98
|
|
|
$blackJack->hitPlayer(-1); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* testStay |
103
|
|
|
* |
104
|
|
|
* Test the stay action of the player |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function testStay(): void |
109
|
|
|
{ |
110
|
|
|
$blackJack = new BlackJack(); |
111
|
|
|
|
112
|
|
|
$blackJack->stayPlayer(); |
113
|
|
|
|
114
|
|
|
$res = $blackJack->stateOfGame(); |
115
|
|
|
|
116
|
|
|
$this->assertNotEquals("Undecided", $res["gameStates"][0]); |
117
|
|
|
|
118
|
|
|
$blackJack->stayPlayer(-1); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* testResetGame |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function testResetGame(): void |
127
|
|
|
{ |
128
|
|
|
$blackJack = new BlackJack(); |
129
|
|
|
|
130
|
|
|
$blackJack->stayPlayer(); |
131
|
|
|
|
132
|
|
|
$res = $blackJack->stateOfGame(); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$blackJack->resetGame(); |
135
|
|
|
|
136
|
|
|
$res = $blackJack->stateOfGame(); |
137
|
|
|
|
138
|
|
|
$this->assertEquals("Undecided", $res["gameStates"][0]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* testDealerBust |
143
|
|
|
* |
144
|
|
|
* Test if dealer can be busted and player wins |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function testDealerBust(): void |
149
|
|
|
{ |
150
|
|
|
// Create mocks for Dealer and Player |
151
|
|
|
$dealerMock = $this->createMock(Dealer::class); |
152
|
|
|
$playerMock = $this->createMock(Player::class); |
153
|
|
|
|
154
|
|
|
// Set up the Player mock to return a hand value of 20 (not bust) |
155
|
|
|
$playerMock->method('getHandValue')->willReturn(20); |
156
|
|
|
$playerMock->method('isBust')->willReturn(false); |
157
|
|
|
$playerMock->method('getString')->willReturn(['🂡', '🂢']); |
158
|
|
|
|
159
|
|
|
// Set up the Dealer mock to return a hand value > 21 (bust) |
160
|
|
|
$dealerMock->method('getHandValue')->willReturn(22); |
161
|
|
|
$dealerMock->method('isBust')->willReturn(true); |
162
|
|
|
$dealerMock->method('getString')->willReturn(['🂠', '🂠']); |
163
|
|
|
|
164
|
|
|
$blackJack = new BlackJack(); |
165
|
|
|
|
166
|
|
|
// Inject mocked Dealer and Player into the game |
167
|
|
|
$reflection = new \ReflectionClass($blackJack); |
168
|
|
|
|
169
|
|
|
// Set the dealer mock |
170
|
|
|
$dealerProperty = $reflection->getProperty('dealer'); |
171
|
|
|
$dealerProperty->setAccessible(true); |
172
|
|
|
$dealerProperty->setValue($blackJack, $dealerMock); |
173
|
|
|
|
174
|
|
|
// Set the player mock |
175
|
|
|
$playersProperty = $reflection->getProperty('players'); |
176
|
|
|
$playersProperty->setAccessible(true); |
177
|
|
|
$playersProperty->setValue($blackJack, [$playerMock]); |
178
|
|
|
|
179
|
|
|
// Invoke the private calculateWinner method via reflection |
180
|
|
|
$method = $reflection->getMethod('calculateWinner'); |
181
|
|
|
$method->setAccessible(true); |
182
|
|
|
$method->invoke($blackJack, 0); |
183
|
|
|
|
184
|
|
|
// Get the game state |
185
|
|
|
$res = $blackJack->stateOfGame(); |
186
|
|
|
|
187
|
|
|
// Assert dealer is bust |
188
|
|
|
$this->assertTrue($blackJack->isDealerBust()); |
189
|
|
|
|
190
|
|
|
// Assert that the player wins since dealer busts |
191
|
|
|
$this->assertEquals( |
192
|
|
|
'Player', |
193
|
|
|
$res['gameStates'][0] |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* testPlayerWin |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
public function testPlayerWin(): void |
204
|
|
|
{ |
205
|
|
|
// Create mocks for Dealer and Player |
206
|
|
|
$dealerMock = $this->createMock(Dealer::class); |
207
|
|
|
$playerMock = $this->createMock(Player::class); |
208
|
|
|
|
209
|
|
|
// Set up the Player mock to return a hand value of 20 |
210
|
|
|
$playerMock->method('getHandValue')->willReturn(20); |
211
|
|
|
$playerMock->method('isBust')->willReturn(false); |
212
|
|
|
$playerMock->method('getString')->willReturn(['🂡', '🂢']); |
213
|
|
|
|
214
|
|
|
// Set up the Dealer mock to return a hand value 17 |
215
|
|
|
$dealerMock->method('getHandValue')->willReturn(17); |
216
|
|
|
$dealerMock->method('isBust')->willReturn(false); |
217
|
|
|
$dealerMock->method('getString')->willReturn(['🂠', '🂠']); |
218
|
|
|
|
219
|
|
|
$blackJack = new BlackJack(); |
220
|
|
|
|
221
|
|
|
// Inject mocked Dealer and Player into the game |
222
|
|
|
$reflection = new \ReflectionClass($blackJack); |
223
|
|
|
|
224
|
|
|
// Set the dealer mock |
225
|
|
|
$dealerProperty = $reflection->getProperty('dealer'); |
226
|
|
|
$dealerProperty->setAccessible(true); |
227
|
|
|
$dealerProperty->setValue($blackJack, $dealerMock); |
228
|
|
|
|
229
|
|
|
// Set the player mock |
230
|
|
|
$playersProperty = $reflection->getProperty('players'); |
231
|
|
|
$playersProperty->setAccessible(true); |
232
|
|
|
$playersProperty->setValue($blackJack, [$playerMock]); |
233
|
|
|
|
234
|
|
|
// Invoke the private calculateWinner method via reflection |
235
|
|
|
$method = $reflection->getMethod('calculateWinner'); |
236
|
|
|
$method->setAccessible(true); |
237
|
|
|
$method->invoke($blackJack, 0); |
238
|
|
|
|
239
|
|
|
// Get the game state |
240
|
|
|
$res = $blackJack->stateOfGame(); |
241
|
|
|
|
242
|
|
|
$this->assertEquals('Player', $res['gameStates'][0]); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* testTie |
247
|
|
|
* |
248
|
|
|
* @return void |
249
|
|
|
*/ |
250
|
|
|
public function testTie(): void |
251
|
|
|
{ |
252
|
|
|
// Create mocks for Dealer and Player |
253
|
|
|
$dealerMock = $this->createMock(Dealer::class); |
254
|
|
|
$playerMock = $this->createMock(Player::class); |
255
|
|
|
|
256
|
|
|
// Set up the Player mock to return a hand value of 20 |
257
|
|
|
$playerMock->method('getHandValue')->willReturn(20); |
258
|
|
|
$playerMock->method('isBust')->willReturn(false); |
259
|
|
|
$playerMock->method('getString')->willReturn(['🂡', '🂢']); |
260
|
|
|
|
261
|
|
|
// Set up the Dealer mock to return a hand value 20 |
262
|
|
|
$dealerMock->method('getHandValue')->willReturn(20); |
263
|
|
|
$dealerMock->method('isBust')->willReturn(false); |
264
|
|
|
$dealerMock->method('getString')->willReturn(['🂠', '🂠']); |
265
|
|
|
|
266
|
|
|
$blackJack = new BlackJack(); |
267
|
|
|
|
268
|
|
|
// Inject mocked Dealer and Player into the game |
269
|
|
|
$reflection = new \ReflectionClass($blackJack); |
270
|
|
|
|
271
|
|
|
// Set the dealer mock |
272
|
|
|
$dealerProperty = $reflection->getProperty('dealer'); |
273
|
|
|
$dealerProperty->setAccessible(true); |
274
|
|
|
$dealerProperty->setValue($blackJack, $dealerMock); |
275
|
|
|
|
276
|
|
|
// Set the player mock |
277
|
|
|
$playersProperty = $reflection->getProperty('players'); |
278
|
|
|
$playersProperty->setAccessible(true); |
279
|
|
|
$playersProperty->setValue($blackJack, [$playerMock]); |
280
|
|
|
|
281
|
|
|
// Invoke the private calculateWinner method via reflection |
282
|
|
|
$method = $reflection->getMethod('calculateWinner'); |
283
|
|
|
$method->setAccessible(true); |
284
|
|
|
$method->invoke($blackJack, 0); |
285
|
|
|
|
286
|
|
|
// Get the game state |
287
|
|
|
$res = $blackJack->stateOfGame(); |
288
|
|
|
|
289
|
|
|
$this->assertEquals('Tie', $res['gameStates'][0]); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|