1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Game\BlackJack; |
4
|
|
|
|
5
|
|
|
use App\Cards\Card; |
6
|
|
|
use App\Game\BlackJack\Player; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Test cases for class Player. |
11
|
|
|
*/ |
12
|
|
|
class PlayerTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* testCreateObject |
16
|
|
|
* |
17
|
|
|
* Construct object and verify that the object has the expected |
18
|
|
|
* properties, use no arguments. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function testCreateObject(): void |
23
|
|
|
{ |
24
|
|
|
$player = new Player(); |
25
|
|
|
$this->assertInstanceOf(Player::class, $player); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* testSettersAndGetters |
30
|
|
|
* |
31
|
|
|
* Test the setters and getters |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testSettersAndGetters(): void |
36
|
|
|
{ |
37
|
|
|
$player = new Player("Player& Test", -15); |
38
|
|
|
|
39
|
|
|
$name = $player->getName(); |
40
|
|
|
$this->assertEquals("Player& Test", $name); |
41
|
|
|
|
42
|
|
|
$credits = $player->getCredits(); |
43
|
|
|
$this->assertEquals(0, $credits); |
44
|
|
|
|
45
|
|
|
$bet = $player->getBet(); |
46
|
|
|
$this->assertEquals(0, $bet); |
47
|
|
|
|
48
|
|
|
$states = [Player::DOUBLE_DOWN, player::STAYED, player::UNDECIDED, player::TIE, player::PLAYER_WIN, player::DEALER_WIN]; |
49
|
|
|
|
50
|
|
|
foreach ($states as $state) { |
51
|
|
|
$player->setGameState($state); |
52
|
|
|
$currentState = $player->getGameState(); |
53
|
|
|
$this->assertEquals($state, $currentState); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* testChangeCredits |
59
|
|
|
* |
60
|
|
|
* Test the changeCredits function |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function testChangeCredits(): void |
65
|
|
|
{ |
66
|
|
|
$player = new Player("", 0); |
67
|
|
|
|
68
|
|
|
$player->changeCredits(-1); |
69
|
|
|
|
70
|
|
|
$balance = $player->getCredits(); |
71
|
|
|
$this->assertEquals(0, $balance); |
72
|
|
|
|
73
|
|
|
$maxCredits = PHP_INT_MAX; |
74
|
|
|
$player->changeCredits(1); |
75
|
|
|
$player->changeCredits($maxCredits); |
76
|
|
|
|
77
|
|
|
$balance = $player->getCredits(); |
78
|
|
|
$this->assertEquals($maxCredits, $balance); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* testBet |
83
|
|
|
* |
84
|
|
|
* Test the increaseBet and resetBet function |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function testBet(): void |
89
|
|
|
{ |
90
|
|
|
$player = new Player("Player", 100); |
91
|
|
|
|
92
|
|
|
$player->increaseBet(50); |
93
|
|
|
|
94
|
|
|
$credits = $player->getCredits(); |
95
|
|
|
$this->assertEquals(50, $credits); |
96
|
|
|
|
97
|
|
|
$bet = $player->getBet(); |
98
|
|
|
$this->assertEquals(50, $bet); |
99
|
|
|
|
100
|
|
|
//Test increase over available credits |
101
|
|
|
$player->increaseBet(100); |
102
|
|
|
|
103
|
|
|
$credits = $player->getCredits(); |
104
|
|
|
$this->assertEquals(0, $credits); |
105
|
|
|
|
106
|
|
|
$bet = $player->getBet(); |
107
|
|
|
$this->assertEquals(100, $bet); |
108
|
|
|
|
109
|
|
|
//Test reset bet |
110
|
|
|
$player->resetBet(); |
111
|
|
|
|
112
|
|
|
$bet = $player->getBet(); |
113
|
|
|
$this->assertEquals(0, $bet); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* testBooleans |
118
|
|
|
* |
119
|
|
|
* Test the boolean values |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function testBooleans(): void |
124
|
|
|
{ |
125
|
|
|
$player = new Player(); |
126
|
|
|
|
127
|
|
|
$card = new Card('K', 'Spades'); |
128
|
|
|
|
129
|
|
|
$player->addCard($card); |
130
|
|
|
$player->addCard($card); |
131
|
|
|
|
132
|
|
|
$bust = $player->isBust(); |
133
|
|
|
$this->assertFalse($bust); |
134
|
|
|
|
135
|
|
|
$player->addCard($card); |
136
|
|
|
|
137
|
|
|
$bust = $player->isBust(); |
138
|
|
|
$this->assertTrue($bust); |
139
|
|
|
|
140
|
|
|
$broke = $player->isBroke(); |
141
|
|
|
$this->assertFalse($broke); |
142
|
|
|
|
143
|
|
|
$player->setCredits(0); |
144
|
|
|
|
145
|
|
|
$broke = $player->isBroke(); |
146
|
|
|
$this->assertTrue($broke); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* testGetValue |
151
|
|
|
* |
152
|
|
|
* Test the GetValue function |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function testGetValue(): void |
157
|
|
|
{ |
158
|
|
|
$player = new Player(); |
159
|
|
|
|
160
|
|
|
$card = new Card('K', 'Spades'); |
161
|
|
|
|
162
|
|
|
$player->addCard($card); |
163
|
|
|
$player->addCard($card); |
164
|
|
|
|
165
|
|
|
$handValue = $player->getHandValue(); |
166
|
|
|
$this->assertEquals(20, $handValue); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* testDropHand |
171
|
|
|
* |
172
|
|
|
* Test the dropHand function |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function testDropHand(): void |
177
|
|
|
{ |
178
|
|
|
$player = new Player(); |
179
|
|
|
|
180
|
|
|
$card = new Card('K', 'Spades'); |
181
|
|
|
|
182
|
|
|
$player->addCard($card); |
183
|
|
|
|
184
|
|
|
$numOfCards = count($player->getString()); |
185
|
|
|
$this->assertEquals(1, $numOfCards); |
186
|
|
|
|
187
|
|
|
$player->dropHand(); |
188
|
|
|
|
189
|
|
|
$numOfCards = count($player->getString()); |
190
|
|
|
$this->assertEquals(0, $numOfCards); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|