GameTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 111
c 1
b 0
f 1
dl 0
loc 216
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testBankWinn() 0 32 1
A testBankIsWinner() 0 16 1
A testBankLose() 0 32 1
A testPlayerIsWinner() 0 16 1
A testCreateGame() 0 11 1
A testBankOver21() 0 16 1
A testPlayerOver21() 0 16 1
A testPlayerLose() 0 32 1
1
<?php
2
3
namespace App\Game;
4
5
use App\Card\CardGraphic;
6
use App\Card\DeckOfCards;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * Test cases for class Game.
11
 */
12
class GameTest extends TestCase
13
{
14
    /**
15
     * Construct object without input arguments and verify that the object has the expected
16
     * properties.
17
     */
18
    public function testCreateGame(): void
19
    {
20
        $game = new Game();
21
        $this->assertInstanceOf("\App\Game\Game", $game);
22
23
        $exp = 0;
24
        $res = $game->getPlayerScore();
25
        $this->assertEquals($exp, $res);
26
27
        $res = $game->getBankScore();
28
        $this->assertEquals($exp, $res);
29
    }
30
31
    /**
32
     * Test a round of the game where the player loses, based on mocked scores.
33
     */
34
    public function testPlayerLose(): void
35
    {
36
        $stubDeck = $this->createMock(DeckOfCards::class);
37
        $stubPlayer = $this->createMock(Player::class);
38
39
        $card = new CardGraphic(["C", "2"]);
40
41
        $stubDeck ->method('drawCard')
42
                    ->willReturn(["C", "2"]);
43
44
        $stubPlayer ->method('setHand')
45
                    ->with($card);
46
47
        $stubPlayer ->method('isOver21')
48
                    ->willReturn(true);
49
50
        $stubPlayer ->method('getScore')
51
                    ->willReturn(25);
52
53
        $stubPlayer ->method('getHand')
54
                    ->willReturn([["C", "2"], ["H", "K"]]);
55
56
        $game = new Game($stubDeck, $stubPlayer);
57
58
        $res = $game->playerTurn();
59
        $cards = [["C", "2"], ["H", "K"]];
60
        $score = 25;
61
        $lost = true;
62
63
        $this->assertEquals($cards, $res["cards"]);
64
        $this->assertEquals($score, $res["score"]);
65
        $this->assertEquals($lost, $res["lost"]);
66
    }
67
68
    /**
69
     * Test a round of the game where the bank winns, based on mocked scores.
70
     */
71
    public function testBankWinn(): void
72
    {
73
        $stubDeck = $this->createMock(DeckOfCards::class);
74
        $stubBank = $this->createMock(Player::class);
75
76
        $card = new CardGraphic(["S", "A"]);
77
78
        $stubDeck ->method('drawCard')
79
                    ->willReturn(["S", "A"]);
80
81
        $stubBank ->method('setHand')
82
                    ->with($card);
83
84
        $stubBank ->method('isOver21')
85
                    ->willReturn(false);
86
87
        $stubBank ->method('getScore')
88
                    ->willReturn(18);
89
90
        $stubBank ->method('getHand')
91
                    ->willReturn([["S", "A"], ["D", "10"]]);
92
93
        $game = new Game($stubDeck, null, $stubBank);
94
95
        $res = $game->bankTurn();
96
        $cards = [["S", "A"], ["D", "10"]];
97
        $score = 18;
98
        $lost = false;
99
100
        $this->assertEquals($cards, $res["cards"]);
101
        $this->assertEquals($score, $res["score"]);
102
        $this->assertEquals($lost, $res["lost"]);
103
104
    }
105
106
    /**
107
     * Test a round of the game where the bank loses, based on mocked scores.
108
     */
109
    public function testBankLose(): void
110
    {
111
        $stubDeck = $this->createMock(DeckOfCards::class);
112
        $stubBank = $this->createMock(Player::class);
113
114
        $card = new CardGraphic(["C", "A"]);
115
116
        $stubDeck ->method('drawCard')
117
                    ->willReturn(["C", "A"]);
118
119
        $stubBank ->method('setHand')
120
                    ->with($card);
121
122
        $stubBank ->method('isOver21')
123
                    ->willReturn(true);
124
125
        $stubBank ->method('getScore')
126
                    ->willReturn(22);
127
128
        $stubBank ->method('getHand')
129
                    ->willReturn([["C", "A"], ["D", "J"], ["H", "K"]]);
130
131
        $game = new Game($stubDeck, null, $stubBank);
132
133
        $res = $game->bankTurn();
134
        $cards = [["C", "A"], ["D", "J"], ["H", "K"]];
135
        $score = 22;
136
        $lost = true;
137
138
        $this->assertEquals($cards, $res["cards"]);
139
        $this->assertEquals($score, $res["score"]);
140
        $this->assertEquals($lost, $res["lost"]);
141
142
    }
143
144
    /**
145
     * Player is returned as the winner of the game, based on mocked scores.
146
     */
147
    public function testPlayerIsWinner(): void
148
    {
149
        $stubPlayer = $this->createMock(Player::class);
150
        $stubBank = $this->createMock(Player::class);
151
152
        $stubPlayer ->method('getScore')
153
            ->willReturn(21);
154
155
        $stubBank ->method('getScore')
156
            ->willReturn(18);
157
158
        $game = new Game(null, $stubPlayer, $stubBank);
159
160
        $exp = "Spelaren";
161
        $res = $game->isWinner();
162
        $this->assertEquals($exp, $res);
163
164
    }
165
166
    /**
167
     * Bank is returned as the winner of the game, based on mocked scores.
168
     */
169
    public function testBankIsWinner(): void
170
    {
171
        $stubPlayer = $this->createMock(Player::class);
172
        $stubBank = $this->createMock(Player::class);
173
174
        $stubPlayer ->method('getScore')
175
            ->willReturn(12);
176
177
        $stubBank ->method('getScore')
178
            ->willReturn(20);
179
180
        $game = new Game(null, $stubPlayer, $stubBank);
181
182
        $exp = "Banken";
183
        $res = $game->isWinner();
184
        $this->assertEquals($exp, $res);
185
    }
186
187
    /**
188
     * Player has a total score over 21, and the bank winns. Mocked scores used.
189
     */
190
    public function testPlayerOver21(): void
191
    {
192
        $stubPlayer = $this->createMock(Player::class);
193
        $stubBank = $this->createMock(Player::class);
194
195
        $stubPlayer ->method('getScore')
196
            ->willReturn(26);
197
198
        $stubBank ->method('getScore')
199
            ->willReturn(20);
200
201
        $game = new Game(null, $stubPlayer, $stubBank);
202
203
        $exp = "Banken";
204
        $res = $game->isWinner();
205
        $this->assertEquals($exp, $res);
206
207
    }
208
209
    /**
210
     * Bank has a total score over 21, and the player winns. Mocked scores used.
211
     */
212
    public function testBankOver21(): void
213
    {
214
        $stubPlayer = $this->createMock(Player::class);
215
        $stubBank = $this->createMock(Player::class);
216
217
        $stubPlayer ->method('getScore')
218
            ->willReturn(21);
219
220
        $stubBank ->method('getScore')
221
            ->willReturn(29);
222
223
        $game = new Game(null, $stubPlayer, $stubBank);
224
225
        $exp = "Spelaren";
226
        $res = $game->isWinner();
227
        $this->assertEquals($exp, $res);
228
229
    }
230
}
231