|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Game; |
|
4
|
|
|
|
|
5
|
|
|
use App\Card\Card; |
|
6
|
|
|
use App\Card\DeckOfCards; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Test cases for class Player. |
|
11
|
|
|
*/ |
|
12
|
|
|
class PlayerTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Construct object without input arguments and verify that the object has the expected |
|
16
|
|
|
* properties. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testCreatePlayer(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$player = new Player(); |
|
21
|
|
|
$this->assertInstanceOf("\App\Game\Player", $player); |
|
22
|
|
|
|
|
23
|
|
|
$exp = 0; |
|
24
|
|
|
$res = $player->getScore(); |
|
25
|
|
|
$this->assertEquals($exp, $res); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Draw a card from mocked deck and verify that correct card is returned. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testDrawCardAndGetHand(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$stub = $this->createMock(DeckOfCards::class); |
|
34
|
|
|
|
|
35
|
|
|
$stub->method('drawCard') |
|
36
|
|
|
->willReturn(["D", "7"]); |
|
37
|
|
|
|
|
38
|
|
|
$player = new Player(); |
|
39
|
|
|
$player->draw(clone $stub); |
|
40
|
|
|
|
|
41
|
|
|
$res = $player->getHand(); |
|
42
|
|
|
$exp = [["D", "7", "♦"]]; |
|
43
|
|
|
|
|
44
|
|
|
$this->assertEquals($exp, $res); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add a mocked card to the hand and verify that the expected score is returned. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testSetHandAndGetScore(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$stub = $this->createMock(Card::class); |
|
53
|
|
|
|
|
54
|
|
|
$stub->method('getValue') |
|
55
|
|
|
->willReturn(["H", "Q"]); |
|
56
|
|
|
|
|
57
|
|
|
$player = new Player(); |
|
58
|
|
|
$player->setHand(clone $stub); |
|
59
|
|
|
|
|
60
|
|
|
$res = $player->getScore(); |
|
61
|
|
|
$exp = 12; |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals($exp, $res); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Draw cards from a mocked deck and verify that the total score is over 21. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testDrawCardOver21(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$player = new Player(); |
|
72
|
|
|
$stub = $this->createMock(DeckOfCards::class); |
|
73
|
|
|
|
|
74
|
|
|
$stub->method('drawCard') |
|
75
|
|
|
->willReturn(["D", "Q"]); |
|
76
|
|
|
|
|
77
|
|
|
$player->draw(clone $stub); |
|
78
|
|
|
|
|
79
|
|
|
$stub->method('drawCard') |
|
80
|
|
|
->willReturn(["S", "K"]); |
|
81
|
|
|
|
|
82
|
|
|
$player->draw(clone $stub); |
|
83
|
|
|
|
|
84
|
|
|
$res = $player->isOver21(); |
|
85
|
|
|
$this->assertTrue($res); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Draw card from a mocked deck and verify that the total score is under 17. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testHandLessThan17(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$stub = $this->createMock(Card::class); |
|
94
|
|
|
|
|
95
|
|
|
$stub->method('getValue') |
|
96
|
|
|
->willReturn(["D", "2"]); |
|
97
|
|
|
|
|
98
|
|
|
$player = new Player(); |
|
99
|
|
|
$player->setHand(clone $stub); |
|
100
|
|
|
|
|
101
|
|
|
$res = $player->isNotOver17(); |
|
102
|
|
|
$this->assertTrue($res); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|