1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Dice; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use App\Dice\Dice; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Test cases for class Dice. |
10
|
|
|
*/ |
11
|
|
|
class DiceTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* testCreateObject |
15
|
|
|
* |
16
|
|
|
* Construct object and verify that the object has the expected |
17
|
|
|
* properties, use no arguments. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function testCreateObject(): void |
22
|
|
|
{ |
23
|
|
|
$dice = new Dice(); |
24
|
|
|
$this->assertInstanceOf(Dice::class, $dice); |
25
|
|
|
|
26
|
|
|
$res = $dice->getString(); |
27
|
|
|
$this->assertNotEmpty($res); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* testStubRollDiceValue |
33
|
|
|
* |
34
|
|
|
* Create a mocked object that always returns 6. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function testStubRollValue() |
39
|
|
|
{ |
40
|
|
|
// Create a stub for the Dice class. |
41
|
|
|
$stub = $this->createMock(Dice::class); |
42
|
|
|
|
43
|
|
|
// Configure the stub. |
44
|
|
|
$stub->method('roll') |
45
|
|
|
->willReturn(6); |
46
|
|
|
|
47
|
|
|
$res = $stub->roll(); |
48
|
|
|
$exp = 6; |
49
|
|
|
$this->assertEquals($exp, $res); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* testRollDiceValueInRange |
54
|
|
|
* |
55
|
|
|
* Roll dice and check the value is in range. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function testRollDiceValueInRange(): void |
60
|
|
|
{ |
61
|
|
|
$dice = new Dice(); |
62
|
|
|
$res = $dice->roll(); |
63
|
|
|
$this->assertGreaterThanOrEqual(1, $res); |
64
|
|
|
$this->assertLessThanOrEqual(6, $res); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* testRollDiceValue |
69
|
|
|
* |
70
|
|
|
* Roll dice and check the value is same as value. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function testRollDiceValue(): void |
75
|
|
|
{ |
76
|
|
|
$dice = new Dice(); |
77
|
|
|
$exp = $dice->roll(); |
78
|
|
|
$res = $dice->getValue(); |
79
|
|
|
$this->assertEquals($exp, $res); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* testCreateObjectWithMock |
84
|
|
|
* |
85
|
|
|
* Construct a mock object of the class Dice and set consecutive values |
86
|
|
|
* to assert against. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function testCreateObjectWithMock(): void |
91
|
|
|
{ |
92
|
|
|
// Create a stub for the SomeClass class. |
93
|
|
|
$stub = $this->createMock(Dice::class); |
94
|
|
|
|
95
|
|
|
// Configure the stub. |
96
|
|
|
$stub->method('getValue') |
97
|
|
|
->willReturn(6); |
98
|
|
|
$stub->method('roll') |
99
|
|
|
->willReturnOnConsecutiveCalls(2, 3, 5); |
100
|
|
|
|
101
|
|
|
// $stub->doSomething() returns a different value each time |
102
|
|
|
$this->assertEquals(6, $stub->getValue()); |
103
|
|
|
$this->assertEquals(2, $stub->roll()); |
104
|
|
|
$this->assertEquals(3, $stub->roll()); |
105
|
|
|
$this->assertEquals(5, $stub->roll()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|