1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Game\BlackJack; |
4
|
|
|
|
5
|
|
|
use App\Cards\Card; |
6
|
|
|
use App\Game\BlackJack; |
7
|
|
|
use App\Game\BlackJack\GameLogic; |
8
|
|
|
use App\Game\BlackJack\Dealer; |
9
|
|
|
use App\Game\BlackJack\Player; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use App\Cards\CardGraphic; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Test cases for class GameLogic. |
15
|
|
|
*/ |
16
|
|
|
class GameLogicTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* testCreateObject |
20
|
|
|
* |
21
|
|
|
* Construct object and verify that the object has the expected |
22
|
|
|
* properties, use no arguments. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function testCreateObject(): void |
27
|
|
|
{ |
28
|
|
|
$blackJack = new BlackJack(1); |
29
|
|
|
$this->assertInstanceOf(BlackJack::class, $blackJack); |
30
|
|
|
|
31
|
|
|
$gameLogic = new GameLogic($blackJack); |
32
|
|
|
$this->assertInstanceOf(GameLogic::class, $gameLogic); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* testNewGame |
37
|
|
|
* |
38
|
|
|
* Test the newGame function |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function testNewGame(): void |
43
|
|
|
{ |
44
|
|
|
$blackJack = new BlackJack(1); |
45
|
|
|
$gameLogic = new GameLogic($blackJack); |
46
|
|
|
|
47
|
|
|
$gameLogic->newGame(); |
48
|
|
|
|
49
|
|
|
$player = $blackJack->getPlayers()[0]; |
50
|
|
|
$dealer = $blackJack->getDealer(); |
51
|
|
|
|
52
|
|
|
$this->assertEquals(2, count($player->getString())); |
53
|
|
|
$this->assertEquals(2, count($dealer->getString())); |
54
|
|
|
$this->assertEquals(BlackJack::MINIMUM_BET, $player->getBet()); |
55
|
|
|
|
56
|
|
|
//Check if player broke new game |
57
|
|
|
//Bet all |
58
|
|
|
$player->increaseBet(Player::DEFAULT_STARTING_CREDITS - BlackJack::MINIMUM_BET); |
59
|
|
|
$blackJack->setPlayers([$player]); |
60
|
|
|
|
61
|
|
|
//Reset game so player have 0 credits and no bet === broke |
62
|
|
|
$gameLogic->newGame(); |
63
|
|
|
|
64
|
|
|
$player = $blackJack->getPlayers()[0]; |
65
|
|
|
$dealer = $blackJack->getDealer(); |
66
|
|
|
|
67
|
|
|
$this->assertEquals(0, count($player->getString())); |
68
|
|
|
$this->assertEquals(2, count($dealer->getString())); |
69
|
|
|
$this->assertEquals(0, $player->getBet()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* testStateOfGameAndCheckIfDealersTurn |
74
|
|
|
* |
75
|
|
|
* Test the stateOfGame and checkIfDealersTurn function |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function testStateOfGameAndCheckIfDealersTurn(): void |
80
|
|
|
{ |
81
|
|
|
$blackJack = new BlackJack(1); |
82
|
|
|
$gameLogic = new GameLogic($blackJack); |
83
|
|
|
|
84
|
|
|
$gameLogic->newGame(); |
85
|
|
|
|
86
|
|
|
$gameLogic->checkIfDealersTurn(); |
87
|
|
|
|
88
|
|
|
$data = $gameLogic->stateOfGame(); |
89
|
|
|
|
90
|
|
|
$this->assertEquals(1, $data["numOfPlayers"]); |
91
|
|
|
$this->assertEquals(Player::DEFAULT_PLAYER_NAME, $data["playersNames"][0]); |
92
|
|
|
$this->assertEquals(2, count($data["playersCards"][0])); |
93
|
|
|
$this->assertEquals(1, count($data["playersHandValue"])); |
94
|
|
|
$this->assertEquals(strval(Player::DEFAULT_STARTING_CREDITS - BlackJack::MINIMUM_BET), $data["playersCredits"][0]); |
95
|
|
|
$this->assertEquals(strval(BlackJack::MINIMUM_BET), $data["playersBets"][0]); |
96
|
|
|
$this->assertEquals(2, count($data["dealerCards"])); |
97
|
|
|
$this->assertEquals('🂠', $data["dealerCards"][1]); |
98
|
|
|
$this->assertEquals('0', $data["dealerHandValue"]); |
99
|
|
|
$this->assertEquals('Undecided', $data["gameStates"][0]); |
100
|
|
|
|
101
|
|
|
//If game is decided. |
102
|
|
|
$player = $blackJack->getPlayers()[0]; |
103
|
|
|
$player->setGameState(Player::STAYED); |
104
|
|
|
$blackJack->setPlayers([$player]); |
105
|
|
|
|
106
|
|
|
$gameLogic->checkIfDealersTurn(); |
107
|
|
|
|
108
|
|
|
$data = $gameLogic->stateOfGame(); |
109
|
|
|
|
110
|
|
|
$this->assertEquals(1, $data["numOfPlayers"]); |
111
|
|
|
$this->assertEquals(Player::DEFAULT_PLAYER_NAME, $data["playersNames"][0]); |
112
|
|
|
$this->assertEquals(2, count($data["playersCards"][0])); |
113
|
|
|
$this->assertEquals(1, count($data["playersHandValue"])); |
114
|
|
|
$this->assertGreaterThanOrEqual(strval(Player::DEFAULT_STARTING_CREDITS - BlackJack::MINIMUM_BET), $data["playersCredits"][0]); |
115
|
|
|
$this->assertEquals('0', $data["playersBets"][0]); |
116
|
|
|
$this->assertGreaterThanOrEqual(2, count($data["dealerCards"])); |
117
|
|
|
$this->assertNotEquals('🂠', $data["dealerCards"][1]); |
118
|
|
|
$this->assertNotEquals('0', $data["dealerHandValue"]); |
119
|
|
|
$this->assertNotEquals('Undecided', $data["gameStates"][0]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* testPlayerBust |
124
|
|
|
* |
125
|
|
|
* Test if player is busted and dealers wins |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function testPlayerBust(): void |
130
|
|
|
{ |
131
|
|
|
$player = new Player(); |
132
|
|
|
$player->setGameState(Player::DEALER_WIN); |
133
|
|
|
$player->addCard(new Card("King", "Spade")); |
134
|
|
|
$player->addCard(new Card("King", "Spade")); |
135
|
|
|
$player->addCard(new Card("King", "Spade")); |
136
|
|
|
|
137
|
|
|
$dealer = new Dealer(); |
138
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
139
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
140
|
|
|
|
141
|
|
|
// Create BlackJack instance |
142
|
|
|
$blackJack = new BlackJack(1); |
143
|
|
|
|
144
|
|
|
$blackJack->setPlayers([$player]); |
145
|
|
|
$blackJack->setDealer($dealer); |
146
|
|
|
|
147
|
|
|
$gameLogic = new GameLogic($blackJack); |
148
|
|
|
|
149
|
|
|
$gameLogic->checkIfDealersTurn(); |
150
|
|
|
|
151
|
|
|
// Get the game state |
152
|
|
|
$data = $gameLogic->stateOfGame(); |
153
|
|
|
|
154
|
|
|
// Assert player is bust |
155
|
|
|
$this->assertTrue($blackJack->isPlayerBust()); |
156
|
|
|
|
157
|
|
|
// Assert that the dealer wins since player busts |
158
|
|
|
$this->assertEquals('Dealer', $data['gameStates'][0]); |
159
|
|
|
|
160
|
|
|
//Check if player is bust but BlackJack code is broken |
161
|
|
|
$player->setGameState(Player::STAYED); |
162
|
|
|
$blackJack->setPlayers([$player]); |
163
|
|
|
|
164
|
|
|
$gameLogic->checkIfDealersTurn(); |
165
|
|
|
|
166
|
|
|
// Get the game state |
167
|
|
|
$data = $gameLogic->stateOfGame(); |
168
|
|
|
|
169
|
|
|
// Assert player is bust |
170
|
|
|
$this->assertTrue($blackJack->isPlayerBust()); |
171
|
|
|
|
172
|
|
|
// Assert that the dealer wins since player busts |
173
|
|
|
$this->assertEquals('Dealer', $data['gameStates'][0]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* testDealerBust |
178
|
|
|
* |
179
|
|
|
* Test if dealer is busted and player wins |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function testDealerBust(): void |
184
|
|
|
{ |
185
|
|
|
$player = new Player(); |
186
|
|
|
$player->setGameState(Player::STAYED); |
187
|
|
|
$player->addCard(new Card("A", "Spade")); |
188
|
|
|
$player->addCard(new Card("King", "Spade")); |
189
|
|
|
|
190
|
|
|
$dealer = new Dealer(); |
191
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
192
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
193
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
194
|
|
|
|
195
|
|
|
// Create BlackJack instance |
196
|
|
|
$blackJack = new BlackJack(1); |
197
|
|
|
|
198
|
|
|
$blackJack->setPlayers([$player]); |
199
|
|
|
$blackJack->setDealer($dealer); |
200
|
|
|
|
201
|
|
|
$gameLogic = new GameLogic($blackJack); |
202
|
|
|
|
203
|
|
|
$gameLogic->checkIfDealersTurn(); |
204
|
|
|
|
205
|
|
|
// Get the game state |
206
|
|
|
$data = $gameLogic->stateOfGame(); |
207
|
|
|
|
208
|
|
|
// Assert dealer is bust |
209
|
|
|
$this->assertTrue($blackJack->isDealerBust()); |
210
|
|
|
|
211
|
|
|
// Assert that the player wins since dealer busts |
212
|
|
|
$this->assertEquals('Player', $data['gameStates'][0]); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* testPlayerWin |
217
|
|
|
* |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function testPlayerWin(): void |
221
|
|
|
{ |
222
|
|
|
$player = new Player(); |
223
|
|
|
$player->setGameState(Player::STAYED); |
224
|
|
|
$player->addCard(new Card("A", "Spade")); |
225
|
|
|
$player->addCard(new Card("King", "Spade")); |
226
|
|
|
|
227
|
|
|
$dealer = new Dealer(); |
228
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
229
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
230
|
|
|
|
231
|
|
|
// Create BlackJack instance |
232
|
|
|
$blackJack = new BlackJack(); |
233
|
|
|
|
234
|
|
|
$blackJack->setPlayers([$player]); |
235
|
|
|
$blackJack->setDealer($dealer); |
236
|
|
|
|
237
|
|
|
$gameLogic = new GameLogic($blackJack); |
238
|
|
|
|
239
|
|
|
$gameLogic->checkIfDealersTurn(); |
240
|
|
|
|
241
|
|
|
// Get the game state |
242
|
|
|
$data = $gameLogic->stateOfGame(); |
243
|
|
|
|
244
|
|
|
// Assert that the player wins since higher value then dealer |
245
|
|
|
$this->assertEquals('Player', $data['gameStates'][0]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* testDealerWin |
250
|
|
|
* |
251
|
|
|
* @return void |
252
|
|
|
*/ |
253
|
|
|
public function testDealerWin(): void |
254
|
|
|
{ |
255
|
|
|
$player = new Player(); |
256
|
|
|
$player->setGameState(Player::STAYED); |
257
|
|
|
$player->addCard(new Card("10", "Spade")); |
258
|
|
|
$player->addCard(new Card("King", "Spade")); |
259
|
|
|
|
260
|
|
|
$dealer = new Dealer(); |
261
|
|
|
$dealer->addCard(new Card("A", "Heart")); |
262
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
263
|
|
|
|
264
|
|
|
// Create BlackJack instance |
265
|
|
|
$blackJack = new BlackJack(); |
266
|
|
|
|
267
|
|
|
$blackJack->setPlayers([$player]); |
268
|
|
|
$blackJack->setDealer($dealer); |
269
|
|
|
|
270
|
|
|
$gameLogic = new GameLogic($blackJack); |
271
|
|
|
|
272
|
|
|
$gameLogic->checkIfDealersTurn(); |
273
|
|
|
|
274
|
|
|
// Get the game state |
275
|
|
|
$data = $gameLogic->stateOfGame(); |
276
|
|
|
|
277
|
|
|
// Assert that the dealer wins since higher value then player |
278
|
|
|
$this->assertEquals('Dealer', $data['gameStates'][0]); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* testTie |
283
|
|
|
* |
284
|
|
|
* @return void |
285
|
|
|
*/ |
286
|
|
|
public function testTie(): void |
287
|
|
|
{ |
288
|
|
|
$player = new Player(); |
289
|
|
|
$player->setGameState(Player::STAYED); |
290
|
|
|
$player->addCard(new Card("A", "Spade")); |
291
|
|
|
$player->addCard(new Card("King", "Spade")); |
292
|
|
|
|
293
|
|
|
$dealer = new Dealer(); |
294
|
|
|
$dealer->addCard(new Card("A", "Heart")); |
295
|
|
|
$dealer->addCard(new Card("10", "Heart")); |
296
|
|
|
|
297
|
|
|
// Create BlackJack instance |
298
|
|
|
$blackJack = new BlackJack(); |
299
|
|
|
|
300
|
|
|
$blackJack->setPlayers([$player]); |
301
|
|
|
$blackJack->setDealer($dealer); |
302
|
|
|
|
303
|
|
|
$gameLogic = new GameLogic($blackJack); |
304
|
|
|
|
305
|
|
|
$gameLogic->checkIfDealersTurn(); |
306
|
|
|
|
307
|
|
|
// Get the game state |
308
|
|
|
$data = $gameLogic->stateOfGame(); |
309
|
|
|
|
310
|
|
|
// Assert that it is a tie since dealer and player have same value |
311
|
|
|
$this->assertEquals('Tie', $data['gameStates'][0]); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|