|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Model; |
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
use App\Observer\SessionSavingObserver; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Test cases for class Card. |
|
11
|
|
|
*/ |
|
12
|
|
|
class GameTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Construct object and verify that the object is of the expected |
|
16
|
|
|
* type. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testCreate(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$game = new Game(); |
|
21
|
|
|
$this->assertInstanceOf("\App\Model\Game", $game); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Assert that create from savedState returns a game object. |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
public function testCreateFromSavedState(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$savedGame = new Game( |
|
31
|
|
|
[new HumanPlayer(), new BankPlayer()], |
|
32
|
|
|
FrenchSuitedDeck::create() |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
$savedState = $savedGame->getGameState( |
|
36
|
|
|
); |
|
37
|
|
|
$game = Game::createFromSavedState($savedState); |
|
38
|
|
|
$this->assertInstanceOf("\App\Model\Game", $game); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Assert that the playRound fuction throws an exception on invalid form input |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
public function testPlayRoundInvalidInput(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$game = new Game(); |
|
48
|
|
|
$this->expectException(Exception::class); |
|
49
|
|
|
$game->playRound(["invalid" => "data"]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Assert that the playRound fuction properly resets the game, when action == restart. |
|
54
|
|
|
*/ |
|
55
|
|
|
|
|
56
|
|
|
public function testPlayRoundRestart(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$human = new HumanPlayer(); |
|
59
|
|
|
$bank = new BankPlayer(); |
|
60
|
|
|
$game = new Game( |
|
61
|
|
|
[$human, $bank], |
|
62
|
|
|
FrenchSuitedDeck::create() |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$human->stand(); |
|
66
|
|
|
$bank->stand(); |
|
67
|
|
|
|
|
68
|
|
|
$game->playRound(["action" => "restart"]); |
|
69
|
|
|
|
|
70
|
|
|
$exp = $human->isStanding(); |
|
71
|
|
|
$this->assertFalse($exp); |
|
72
|
|
|
|
|
73
|
|
|
$exp = $bank->isStanding(); |
|
74
|
|
|
$this->assertFalse($exp); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Assert that the playRound fuction sets the game status to "ended" |
|
80
|
|
|
* when a winner is determined. |
|
81
|
|
|
*/ |
|
82
|
|
|
|
|
83
|
|
|
public function testPlayRoundWinner(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$human = new HumanPlayer(); |
|
86
|
|
|
$bank = new BankPlayer(); |
|
87
|
|
|
$players = [$human, $bank]; |
|
88
|
|
|
$game = new Game( |
|
89
|
|
|
$players, |
|
90
|
|
|
FrenchSuitedDeck::create() |
|
91
|
|
|
); |
|
92
|
|
|
// There is a "bug" in my code, the re-created game is not the same as the existing game |
|
93
|
|
|
$game = Game::createFromSavedState($game->getGameState()); |
|
94
|
|
|
|
|
95
|
|
|
$game->playRound(["action" => "hit"]); |
|
96
|
|
|
$game->playRound(["action" => "stand"]); |
|
97
|
|
|
$game->playRound(["action" => "hit"]); |
|
98
|
|
|
$game->playRound(["action" => "stand"]); |
|
99
|
|
|
|
|
100
|
|
|
$exp = $game->getGameStatus(); |
|
101
|
|
|
$this->assertEquals("ended", $exp); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Test getWinnerBasedOnHAnd returns correct winner. |
|
106
|
|
|
*/ |
|
107
|
|
|
|
|
108
|
|
|
public function testGetWinnerBasedOnHand(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$human = new HumanPlayer(); |
|
111
|
|
|
$bank = new BankPlayer(); |
|
112
|
|
|
$players = [$human, $bank]; |
|
113
|
|
|
|
|
114
|
|
|
$game = new Game( |
|
115
|
|
|
$players, |
|
116
|
|
|
FrenchSuitedDeck::create() |
|
117
|
|
|
); |
|
118
|
|
|
// There is a "bug" in my code, the re-created game is not the same as the existing game |
|
119
|
|
|
$game = Game::createFromSavedState($game->getGameState()); |
|
120
|
|
|
|
|
121
|
|
|
$human->addCardsToHand([new Card("hearts", "ace", 1, 14), new Card("hearts", "ace", 1, 14)]); |
|
122
|
|
|
$bank->addCardsToHand([new Card("hearts", "ace", 1, 14), new Card("hearts", "ace", 1, 14)]); |
|
123
|
|
|
$human->stand(); |
|
124
|
|
|
$bank->stand(); |
|
125
|
|
|
|
|
126
|
|
|
$exp = "bank"; |
|
127
|
|
|
$res = $game->getWinnerBasedOnHand(); |
|
128
|
|
|
$this->assertEquals($exp, $res); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Assert that it is possible to attach and detach an observer. |
|
133
|
|
|
*/ |
|
134
|
|
|
|
|
135
|
|
|
public function testAttachAndDetach(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$human = new HumanPlayer(); |
|
138
|
|
|
$bank = new BankPlayer(); |
|
139
|
|
|
$players = [$human, $bank]; |
|
140
|
|
|
|
|
141
|
|
|
$game = new Game( |
|
142
|
|
|
$players, |
|
143
|
|
|
FrenchSuitedDeck::create(), |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
$observer = new SessionSavingObserver(new Request(), "data"); |
|
147
|
|
|
$game->attach($observer); |
|
148
|
|
|
$this->assertEquals($game->getObservers()->count(), 1); |
|
149
|
|
|
|
|
150
|
|
|
$game->detach($observer); |
|
151
|
|
|
$this->assertEquals($game->getObservers()->count(), 0); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Test getPlayers that it returns the players. |
|
156
|
|
|
*/ |
|
157
|
|
|
|
|
158
|
|
|
public function testGetPlayers(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$human = new HumanPlayer(); |
|
161
|
|
|
$bank = new BankPlayer(); |
|
162
|
|
|
$players = [$human, $bank]; |
|
163
|
|
|
$game = new Game( |
|
164
|
|
|
$players, |
|
165
|
|
|
FrenchSuitedDeck::create() |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
$exp = $players; |
|
169
|
|
|
$res = $game->getPlayers(); |
|
170
|
|
|
$this->assertEquals($exp, $res); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Test get current player |
|
175
|
|
|
*/ |
|
176
|
|
|
|
|
177
|
|
|
public function testGetCurrentPlayer(): void |
|
178
|
|
|
{ |
|
179
|
|
|
$human = new HumanPlayer(); |
|
180
|
|
|
$bank = new BankPlayer(); |
|
181
|
|
|
$players = [$human, $bank]; |
|
182
|
|
|
$game = new Game( |
|
183
|
|
|
$players, |
|
184
|
|
|
FrenchSuitedDeck::create() |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
$exp = "human"; |
|
188
|
|
|
$res = $game->getCurrentPlayer(); |
|
189
|
|
|
$this->assertEquals($exp, $res); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|