|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Card; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Test cases for class DeckOfCards. |
|
9
|
|
|
*/ |
|
10
|
|
|
class DeckOfCardsTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Test create new object no argument. Expect a deck of 52 Card objects. |
|
14
|
|
|
*/ |
|
15
|
|
|
public function testCreateDeckNoArgument(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$deck = new DeckOfCards(); |
|
18
|
|
|
$cards = $deck->getDeck(); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertCount(52, $cards); |
|
21
|
|
|
$this->assertContainsOnlyInstancesOf(Card::class, $cards); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Test create new object with argument specifying an array of Card objects. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testCreateDeckWithArgument(): void |
|
28
|
|
|
{ |
|
29
|
|
|
// Create a test stub for Card |
|
30
|
|
|
$card = $this->createStub(Card::class); |
|
31
|
|
|
|
|
32
|
|
|
$deck = new DeckOfCards([$card, $card, $card]); |
|
33
|
|
|
|
|
34
|
|
|
$cards = $deck->getDeck(); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame([$card, $card, $card], $cards); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Test create new object with argument specifying an empty array. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testCreateEmptyDeck(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$deck = new DeckOfCards([]); |
|
45
|
|
|
$cards = $deck->getDeck(); |
|
46
|
|
|
$this->assertEmpty($cards); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Test add card. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testAddCard(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$deck = new DeckOfCards([]); // Create empty deck |
|
55
|
|
|
|
|
56
|
|
|
// Create a test stub for Card |
|
57
|
|
|
$card = $this->createStub(Card::class); |
|
58
|
|
|
|
|
59
|
|
|
$deck->addCard($card); |
|
60
|
|
|
$deck->addCard($card); |
|
61
|
|
|
$deck->addCard($card); |
|
62
|
|
|
|
|
63
|
|
|
$cards = $deck->getDeck(); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertSame([$card, $card, $card], $cards); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Test get cards as array of strings. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testGetCardsAsString(): void |
|
72
|
|
|
{ |
|
73
|
|
|
// Create a test stub for Card |
|
74
|
|
|
$card = $this->createStub(Card::class); |
|
75
|
|
|
|
|
76
|
|
|
// Configure the test stub |
|
77
|
|
|
$card->method('__toString') |
|
78
|
|
|
->willReturnOnConsecutiveCalls('first', 'second', 'third'); |
|
79
|
|
|
|
|
80
|
|
|
$deck = new DeckOfCards([$card, $card, $card]); |
|
81
|
|
|
|
|
82
|
|
|
$res = $deck->getString(); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertSame(['first', 'second', 'third'], $res); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Test get array of card values. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testGetCardValues(): void |
|
91
|
|
|
{ |
|
92
|
|
|
// Create a test stub for Card |
|
93
|
|
|
$card = $this->createStub(Card::class); |
|
94
|
|
|
|
|
95
|
|
|
// Configure the test stub |
|
96
|
|
|
$card->method('getRank') |
|
97
|
|
|
->willReturnOnConsecutiveCalls(1, 2, 3); |
|
98
|
|
|
|
|
99
|
|
|
$deck = new DeckOfCards([$card, $card, $card]); |
|
100
|
|
|
|
|
101
|
|
|
$res = $deck->getValues(); |
|
102
|
|
|
$this->assertSame([1, 2, 3], $res); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Test get card count. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testGetCardCount(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$card = $this->createStub(Card::class); // Create a test stub for Card |
|
111
|
|
|
|
|
112
|
|
|
$deck = new DeckOfCards([$card, $card, $card]); |
|
113
|
|
|
|
|
114
|
|
|
$count = $deck->getCount(); |
|
115
|
|
|
$this->assertSame(3, $count); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Test drawing a single card. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testDrawSingleCardIsSame(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$card = $this->createStub(Card::class); // Create a test stub for Card |
|
124
|
|
|
|
|
125
|
|
|
$deck = new DeckOfCards([$card]); // Create deck with single card |
|
126
|
|
|
|
|
127
|
|
|
$draw = $deck->draw(1); // Draw single card |
|
128
|
|
|
$this->assertSame([$card], $draw); // Assert it is the same |
|
129
|
|
|
|
|
130
|
|
|
$cards = $deck->getDeck(); // Get cards left in deck |
|
131
|
|
|
$this->assertEmpty($cards); // Assert deck is now empty |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testDrawMultipleCardsCountOk(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$deck = new DeckOfCards(); // Create full deck of 52 cards |
|
137
|
|
|
|
|
138
|
|
|
$draw = $deck->draw(3); // Draw three cards |
|
139
|
|
|
$this->assertCount(3, $draw); // Assert count is correct |
|
140
|
|
|
|
|
141
|
|
|
$cards = $deck->getDeck(); // Get cards left in deck |
|
142
|
|
|
$this->assertCount(49, $cards); // Assert deck has three fewer cards |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testDrawMultipleCardsOrderIsOk(): void |
|
149
|
|
|
{ |
|
150
|
|
|
$deck = new DeckOfCards(); // Create full deck of 52 cards |
|
151
|
|
|
|
|
152
|
|
|
$draw = $deck->draw(3); // Draw three cards |
|
153
|
|
|
|
|
154
|
|
|
$res = array_map('strval', $draw); // Get string representation of each card |
|
155
|
|
|
|
|
156
|
|
|
// Because deck is ordered by default, |
|
157
|
|
|
// we can expect draw to be King, Queen and Jack of Spades |
|
158
|
|
|
$this->assertSame(['K♠', 'Q♠', 'J♠'], $res); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Test shuffle cards. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function testShuffleCards(): void |
|
165
|
|
|
{ |
|
166
|
|
|
srand(42); // Seed the random number generator to get reproducible results |
|
167
|
|
|
|
|
168
|
|
|
$deck = new DeckOfCards([]); |
|
169
|
|
|
|
|
170
|
|
|
// Create a test stubs for Card |
|
171
|
|
|
for ($i = 0; $i < 10; $i++) { |
|
172
|
|
|
$card = $this->createStub(Card::class); |
|
173
|
|
|
$card->method('getRank') |
|
174
|
|
|
->willReturn($i); |
|
175
|
|
|
$deck->addCard($card); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$deck->shuffleCards(); |
|
179
|
|
|
$res = $deck->getValues(); |
|
180
|
|
|
$exp = [1, 3, 9, 7, 6, 0, 8, 4, 5, 2]; |
|
181
|
|
|
$this->assertSame($exp, $res); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|