1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
7
|
|
|
use Symfony\Component\Routing\RouterInterface; |
8
|
|
|
|
9
|
|
|
class DiceGameControllerExceptionTest extends WebTestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* testException |
13
|
|
|
* |
14
|
|
|
* Test the exception handling |
15
|
|
|
* |
16
|
|
|
* @param KernelBrowser $client |
17
|
|
|
* @param string $url |
18
|
|
|
* @param string $errorMessage |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
private function testException(KernelBrowser $client, string $url, string $errorMessage = ""): void |
22
|
|
|
{ |
23
|
|
|
// Disable exception catching |
24
|
|
|
$client->catchExceptions(false); |
25
|
|
|
|
26
|
|
|
//Test the exception handling |
27
|
|
|
$this->expectException(\Exception::class); |
28
|
|
|
if ($errorMessage != "") { |
29
|
|
|
$this->expectExceptionMessage($errorMessage); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Send a GET request to the route |
33
|
|
|
$client->request('GET', $url); |
34
|
|
|
|
35
|
|
|
// Re-enable exception catching |
36
|
|
|
$client->catchExceptions(true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* testDiceLowException |
41
|
|
|
* |
42
|
|
|
* Test the exception handling for higher bound |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function testDiceHighException(): void |
47
|
|
|
{ |
48
|
|
|
// Create a client to browse the application |
49
|
|
|
$client = static::createClient(); |
50
|
|
|
|
51
|
|
|
// Retrieve router service |
52
|
|
|
/** @var RouterInterface $router */ |
53
|
|
|
$router = static::getContainer()->get('router'); |
54
|
|
|
|
55
|
|
|
// Generate URL from route name |
56
|
|
|
$url = $router->generate('test_roll_num_dices', ['num' => 100]); |
57
|
|
|
|
58
|
|
|
// Error message to look for |
59
|
|
|
$errorMessage = "Can't roll more than 99 dices!"; |
60
|
|
|
|
61
|
|
|
$this->testException($client, $url, $errorMessage); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* testDiceLowException |
66
|
|
|
* |
67
|
|
|
* Test the exception handling for lower bound |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function testDiceLowException(): void |
72
|
|
|
{ |
73
|
|
|
// Create a client to browse the application |
74
|
|
|
$client = static::createClient(); |
75
|
|
|
|
76
|
|
|
// Retrieve router service |
77
|
|
|
/** @var RouterInterface $router */ |
78
|
|
|
$router = static::getContainer()->get('router'); |
79
|
|
|
|
80
|
|
|
// Generate URL from route name |
81
|
|
|
$url = $router->generate('test_roll_num_dices', ['num' => 0]); |
82
|
|
|
|
83
|
|
|
// Error message to look for |
84
|
|
|
$errorMessage = "Can't roll less than 1 die!"; |
85
|
|
|
|
86
|
|
|
// Disable exception catching |
87
|
|
|
$client->catchExceptions(false); |
88
|
|
|
|
89
|
|
|
$this->testException($client, $url, $errorMessage); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* testDicehandHighException |
94
|
|
|
* |
95
|
|
|
* Test the exception handling for higher bound |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function testDicehandHighException(): void |
100
|
|
|
{ |
101
|
|
|
// Create a client to browse the application |
102
|
|
|
$client = static::createClient(); |
103
|
|
|
|
104
|
|
|
// Retrieve router service |
105
|
|
|
/** @var RouterInterface $router */ |
106
|
|
|
$router = static::getContainer()->get('router'); |
107
|
|
|
|
108
|
|
|
// Generate URL from route name |
109
|
|
|
$url = $router->generate('test_dicehand', ['num' => 100]); |
110
|
|
|
|
111
|
|
|
// Error message to look for |
112
|
|
|
$errorMessage = "Can't roll more than 99 dices!"; |
113
|
|
|
|
114
|
|
|
$this->testException($client, $url, $errorMessage); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* testDicehandLowException |
119
|
|
|
* |
120
|
|
|
* Test the exception handling for lower bound |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function testDicehandLowException(): void |
125
|
|
|
{ |
126
|
|
|
// Create a client to browse the application |
127
|
|
|
$client = static::createClient(); |
128
|
|
|
|
129
|
|
|
// Retrieve router service |
130
|
|
|
/** @var RouterInterface $router */ |
131
|
|
|
$router = static::getContainer()->get('router'); |
132
|
|
|
|
133
|
|
|
// Generate URL from route name |
134
|
|
|
$url = $router->generate('test_dicehand', ['num' => 0]); |
135
|
|
|
|
136
|
|
|
// Error message to look for |
137
|
|
|
$errorMessage = "Can't roll less than 1 die!"; |
138
|
|
|
|
139
|
|
|
// Disable exception catching |
140
|
|
|
$client->catchExceptions(false); |
141
|
|
|
|
142
|
|
|
$this->testException($client, $url, $errorMessage); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|