1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Dice; |
4
|
|
|
|
5
|
|
|
use App\Dice\Dice; |
6
|
|
|
use App\Dice\DiceHand; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Test cases for class DiceHand. |
11
|
|
|
*/ |
12
|
|
|
class DiceHandTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* testCreateObject |
16
|
|
|
* |
17
|
|
|
* Construct object and verify that the object has the expected |
18
|
|
|
* properties. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function testCreateObject(): void |
23
|
|
|
{ |
24
|
|
|
$diceHand = new DiceHand(); |
25
|
|
|
$this->assertInstanceOf(DiceHand::class, $diceHand); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* testAddDice |
30
|
|
|
* |
31
|
|
|
* Add dice, Roll the dice hand and the sum of the values are in range. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testAddDice(): void |
36
|
|
|
{ |
37
|
|
|
// Create a stub for the Dice class. |
38
|
|
|
$stub = $this->createMock(Dice::class); |
39
|
|
|
|
40
|
|
|
// Configure the stub. |
41
|
|
|
$stub->method('roll') |
42
|
|
|
->willReturn(6); |
43
|
|
|
$stub->method('getValue') |
44
|
|
|
->willReturn(6); |
45
|
|
|
|
46
|
|
|
$diceHand = new DiceHand(); |
47
|
|
|
$diceHand->addDie(clone $stub); |
48
|
|
|
$diceHand->addDie(clone $stub); |
49
|
|
|
$diceHand->roll(); |
50
|
|
|
$res = $diceHand->sum(); |
51
|
|
|
$this->assertEquals(12, $res); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* testRemoveDice |
56
|
|
|
* |
57
|
|
|
* Remove dice, check how many dice is left |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function testRemoveDice(): void |
62
|
|
|
{ |
63
|
|
|
$diceHand = new DiceHand(); |
64
|
|
|
$diceHand->addDie(new Dice()); |
65
|
|
|
$diceHand->addDie(new Dice()); |
66
|
|
|
$diceHand->addDie(new Dice()); |
67
|
|
|
$numDice = $diceHand->getNumberDices(); |
68
|
|
|
$this->assertEquals(3, $numDice); |
69
|
|
|
$diceHand->removeDie(); |
70
|
|
|
$diceHand->removeDie(); |
71
|
|
|
$numDice = $diceHand->getNumberDices(); |
72
|
|
|
$this->assertEquals(1, $numDice); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* testGetValues |
77
|
|
|
* |
78
|
|
|
* Stub the dice class to return predictable values, then check get value |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function testGetValues(): void |
83
|
|
|
{ |
84
|
|
|
// Create a stub for the Dice class. |
85
|
|
|
$stub = $this->createMock(Dice::class); |
86
|
|
|
|
87
|
|
|
// Configure the stub. |
88
|
|
|
$stub->method('getValue') |
89
|
|
|
->willReturn(6); |
90
|
|
|
|
91
|
|
|
$diceHand = new DiceHand(); |
92
|
|
|
$diceHand->addDie(clone $stub); |
93
|
|
|
$diceHand->addDie(clone $stub); |
94
|
|
|
$res = $diceHand->getValues(); |
95
|
|
|
$this->assertEquals([6,6], $res); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* testGetString |
100
|
|
|
* |
101
|
|
|
* Stub the dice class to return predictable values, then check get string |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function testGetString(): void |
106
|
|
|
{ |
107
|
|
|
// Create a stub for the Dice class. |
108
|
|
|
$stub = $this->createMock(Dice::class); |
109
|
|
|
|
110
|
|
|
// Configure the stub. |
111
|
|
|
$stub->method('getString') |
112
|
|
|
->willReturn("6"); |
113
|
|
|
|
114
|
|
|
$diceHand = new DiceHand(); |
115
|
|
|
$diceHand->addDie(clone $stub); |
116
|
|
|
$diceHand->addDie(clone $stub); |
117
|
|
|
$res = $diceHand->getString(); |
118
|
|
|
$this->assertEquals(["6","6"], $res); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|