BlackJackTest::testSettersAndGetters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 38
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 66
rs 9.312

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Tests\Game;
4
5
use App\Cards\Card;
6
use App\Cards\DeckOfCards;
7
use App\Game\BlackJack;
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 BlackJack.
15
 */
16
class BlackJackTest 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();
29
        $this->assertInstanceOf(BlackJack::class, $blackJack);
30
31
        $blackJack->newGame();
32
33
        $res = $blackJack->stateOfGame();
34
        $this->assertEquals("1", $res["numOfPlayers"]);
35
    }
36
37
    /**
38
     * testToFewPlayers
39
     *
40
     * @return void
41
     */
42
    public function testToFewPlayers(): void
43
    {
44
        $this->expectException(\RuntimeException::class);
45
        $this->expectExceptionMessage("Can't have less then one player in Black Jack");
46
        new BlackJack(0); // Less than 1
47
    }
48
49
    /**
50
     * testToManyPlayers
51
     *
52
     * @return void
53
     */
54
    public function testToManyPlayers(): void
55
    {
56
        $this->expectException(\RuntimeException::class);
57
        $this->expectExceptionMessage('Maximum of ' . BlackJack::MAX_PLAYERS . ' players in Black Jack');
58
        new BlackJack(BlackJack::MAX_PLAYERS + 1);
59
    }
60
61
    /**
62
     * testBooleans
63
     *
64
     * Test the boolean values
65
     *
66
     * @return void
67
     */
68
    public function testBooleans(): void
69
    {
70
        $blackJack = new BlackJack();
71
72
        $blackJack->newGame();
73
74
        $playerBust = $blackJack->isPlayerBust();
75
        $this->assertEquals(false, $playerBust);
76
77
        $playerBroke = $blackJack->isPlayerBroke();
78
        $this->assertEquals(false, $playerBroke);
79
80
        $dealerBust = $blackJack->isDealerBust();
81
        $this->assertEquals(false, $dealerBust);
82
83
        //Test if index out of bounds
84
        $playerBust = $blackJack->isPlayerBust(-1);
85
        $this->assertEquals(true, $playerBust);
86
87
        $playerBroke = $blackJack->isPlayerBroke(-1);
88
        $this->assertEquals(true, $playerBroke);
89
    }
90
91
    /**
92
     * testSettersAndGetters
93
     *
94
     * Test the setters and getters
95
     *
96
     * @return void
97
     */
98
    public function testSettersAndGetters(): void
99
    {
100
        $blackJack = new BlackJack();
101
102
        $deck = new DeckOfCards();
103
        $deck->drawCard();
104
105
        //Test setDeck
106
        $blackJack->setDeck($deck);
107
108
        //Test get
109
        $deckCopy = $blackJack->getDeck();
110
        $this->assertEquals(51, count($deckCopy->getString()));
111
112
        //Test setPlayers
113
        $player = new Player();
114
        $player->setGameState(Player::STAYED);
115
        $player->addCard(new Card("A", "Spade"));
116
        $player->addCard(new Card("King", "Spade"));
117
118
        $blackJack->setPlayers([$player, $player]);
119
120
        //Test getPlayers
121
        $players = $blackJack->getPlayers();
122
        $numOfPlayers = $blackJack->getNumOfPlayers();
123
        $this->assertEquals(2, count($players));
124
        $this->assertEquals(2, $numOfPlayers);
125
126
        //Test setPlayer
127
        $players[1]->addCard(new Card("A", "Spade"));
128
        $blackJack->setPlayer(1, $players[1]);
129
130
        $players = $blackJack->getPlayers();
131
        $numOfPlayers = $blackJack->getNumOfPlayers();
132
        $this->assertEquals(2, count($players));
133
        $this->assertEquals(2, $numOfPlayers);
134
135
        //Test setPlayer out of bounds
136
        $blackJack->setPlayer(2, $player);
137
138
        $players = $blackJack->getPlayers();
139
        $this->assertEquals(3, count($players[1]->getString()));
140
141
        //Test setDealer
142
        $dealer = new Dealer();
143
        $dealer->addCard(new Card("10", "Heart"));
144
        $dealer->addCard(new Card("10", "Heart"));
145
146
        $blackJack->setDealer($dealer);
147
148
        //Test getDealer
149
        $dealerCopy = $blackJack->getDealer();
150
        $this->assertEquals(2, count($dealerCopy->getString()));
151
152
        //Check if to many players
153
        $players = [];
154
        for ($i = 0; $i <= BlackJack::MAX_PLAYERS; $i++) {
155
            $players[] = $player;
156
        }
157
158
        $blackJack->setPlayers([$player]);
159
160
        $players = $blackJack->getPlayers();
161
        $numOfPlayers = $blackJack->getNumOfPlayers();
162
        $this->assertEquals(1, count($players));
163
        $this->assertEquals(1, $numOfPlayers);
164
    }
165
166
    /**
167
     * testStayPlayer
168
     *
169
     * Test the stay action of the player
170
     *
171
     * @return void
172
     */
173
    public function testStayPlayer(): void
174
    {
175
        $blackJack = new BlackJack();
176
177
        $blackJack->newGame();
178
179
        $blackJack->stayPlayer();
180
181
        $res = $blackJack->stateOfGame();
182
183
        $this->assertNotEquals("Undecided", $res["gameStates"][0]);
184
185
        $blackJack->stayPlayer(-1);
186
    }
187
188
    /**
189
     * testHitPlayer
190
     *
191
     * Test the hit action of the player
192
     *
193
     * @return void
194
     */
195
    public function testHitPlayer(): void
196
    {
197
        $blackJack = new BlackJack();
198
199
        $blackJack->newGame();
200
201
        $blackJack->hitPlayer();
202
203
        $player = $blackJack->getPlayers()[0];
204
205
        $this->assertEquals(3, count($player->getString()));
206
207
        //Check if gone bust
208
        while ($blackJack->isPlayerBust() === false) {
209
            $blackJack->hitPlayer();
210
        }
211
212
        $player = $blackJack->getPlayers()[0];
213
        $this->assertEquals(0, $player->getBet());
214
215
        //Check if index out of bound
216
        $blackJack->hitPlayer(-1);
217
    }
218
219
    /**
220
     * testDoubleDownPlayer
221
     *
222
     * Test the double down action of the player
223
     *
224
     * @return void
225
     */
226
    public function testDoubleDownPlayer(): void
227
    {
228
        $blackJack = new BlackJack();
229
230
        $blackJack->newGame();
231
232
        $blackJack->doubleDownPlayer();
233
234
        $player = $blackJack->getPlayers()[0];
235
236
        $this->assertEquals(3, count($player->getString()));
237
238
        (true === $player->isBust()) ?
239
            $this->assertEquals(Player::DEFAULT_STARTING_CREDITS - (BlackJack::MINIMUM_BET * 2), $player->getCredits()) :
240
            $this->assertGreaterThanOrEqual(Player::DEFAULT_STARTING_CREDITS - (BlackJack::MINIMUM_BET * 2), $player->getCredits());
241
242
        $blackJack->newGame();
243
244
        $blackJack->doubleDownPlayer(-1);
245
    }
246
}
247