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 DiceControllerTest extends WebTestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* testPigInitPOST |
13
|
|
|
* |
14
|
|
|
* Test pig_init_post route |
15
|
|
|
* Is only private because it runs twice and don't need to be run three times also if you do the testPigRoll don't test everything |
16
|
|
|
* |
17
|
|
|
* @param KernelBrowser $client |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
private function testPigInitPOST(KernelBrowser $client): void |
21
|
|
|
{ |
22
|
|
|
// Retrieve router service |
23
|
|
|
/** @var RouterInterface $router */ |
24
|
|
|
$router = static::getContainer()->get('router'); |
25
|
|
|
|
26
|
|
|
// Generate URL from route name |
27
|
|
|
$url = $router->generate('pig_init_post'); |
28
|
|
|
|
29
|
|
|
// Prepare POST data |
30
|
|
|
$postData = [ |
31
|
|
|
'num_dices' => 3, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
// Send POST request to the route |
35
|
|
|
$client->request('POST', $url, $postData); |
36
|
|
|
|
37
|
|
|
// Assert that response is a redirect (302 status) |
38
|
|
|
$response = $client->getResponse(); |
39
|
|
|
$this->assertTrue($response->isRedirect()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* testPigStart |
44
|
|
|
* |
45
|
|
|
* Test pig_start route |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function testPigStart(): void |
50
|
|
|
{ |
51
|
|
|
// Create a client to browse the application |
52
|
|
|
$client = static::createClient(); |
53
|
|
|
|
54
|
|
|
// Retrieve router service |
55
|
|
|
/** @var RouterInterface $router */ |
56
|
|
|
$router = static::getContainer()->get('router'); |
57
|
|
|
|
58
|
|
|
// Generate URL from route name |
59
|
|
|
$url = $router->generate('pig_start'); |
60
|
|
|
|
61
|
|
|
// Send a GET request to the route |
62
|
|
|
$crawler = $client->request('GET', $url); |
63
|
|
|
|
64
|
|
|
// Assert the response status code |
65
|
|
|
$this->assertEquals(200, $client->getResponse()->getStatusCode()); |
66
|
|
|
|
67
|
|
|
// Assert that certain content exists in the response |
68
|
|
|
$this->assertStringContainsString('Pig Game:', $crawler->filter('title')->text()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* testPigInitGET |
73
|
|
|
* |
74
|
|
|
* Test pig_init_get route |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function testPigInitGET(): void |
79
|
|
|
{ |
80
|
|
|
// Create a client to browse the application |
81
|
|
|
$client = static::createClient(); |
82
|
|
|
|
83
|
|
|
// Retrieve router service |
84
|
|
|
/** @var RouterInterface $router */ |
85
|
|
|
$router = static::getContainer()->get('router'); |
86
|
|
|
|
87
|
|
|
// Generate URL from route name |
88
|
|
|
$url = $router->generate('pig_init_get'); |
89
|
|
|
|
90
|
|
|
// Send a GET request to the route you want to test |
91
|
|
|
$crawler = $client->request('GET', $url); |
92
|
|
|
|
93
|
|
|
// Assert the response status code |
94
|
|
|
$this->assertEquals(200, $client->getResponse()->getStatusCode()); |
95
|
|
|
|
96
|
|
|
// Assert that certain content exists in the response |
97
|
|
|
$this->assertStringContainsString('Pig Game: init', $crawler->filter('title')->text()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* testPigPlay |
102
|
|
|
* |
103
|
|
|
* Test pig_play route |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function testPigPlay(): void |
108
|
|
|
{ |
109
|
|
|
// Create a client to browse the application |
110
|
|
|
$client = static::createClient(); |
111
|
|
|
|
112
|
|
|
// Retrieve router service |
113
|
|
|
/** @var RouterInterface $router */ |
114
|
|
|
$router = static::getContainer()->get('router'); |
115
|
|
|
|
116
|
|
|
//Needed or the server code becomes 500 |
117
|
|
|
$this->testPigInitPOST($client); |
118
|
|
|
|
119
|
|
|
// Generate URL from route name |
120
|
|
|
$url = $router->generate('pig_play'); |
121
|
|
|
|
122
|
|
|
// Send a GET request to the route you want to test |
123
|
|
|
$crawler = $client->request('GET', $url); |
124
|
|
|
|
125
|
|
|
// Assert the response status code |
126
|
|
|
$this->assertEquals(200, $client->getResponse()->getStatusCode()); |
127
|
|
|
|
128
|
|
|
// Assert that certain content exists in the response |
129
|
|
|
$this->assertStringContainsString('Pig Game: Play', $crawler->filter('title')->text()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* testPigSave |
134
|
|
|
* |
135
|
|
|
* Test pig_save route |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testPigSave(): void |
140
|
|
|
{ |
141
|
|
|
// Create a client to browse the application |
142
|
|
|
$client = static::createClient(); |
143
|
|
|
|
144
|
|
|
// Retrieve router service |
145
|
|
|
/** @var RouterInterface $router */ |
146
|
|
|
$router = static::getContainer()->get('router'); |
147
|
|
|
|
148
|
|
|
// Generate URL from route name |
149
|
|
|
$url = $router->generate('pig_save'); |
150
|
|
|
|
151
|
|
|
// Send POST request to the route |
152
|
|
|
$client->request('POST', $url); |
153
|
|
|
|
154
|
|
|
// Assert that response is a redirect (302 status) |
155
|
|
|
$response = $client->getResponse(); |
156
|
|
|
$this->assertTrue($response->isRedirect()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* testPigRoll |
161
|
|
|
* |
162
|
|
|
* Test pig_roll route |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function testPigRoll(): void |
167
|
|
|
{ |
168
|
|
|
// Create a client to browse the application |
169
|
|
|
$client = static::createClient(); |
170
|
|
|
|
171
|
|
|
// Retrieve router service |
172
|
|
|
/** @var RouterInterface $router */ |
173
|
|
|
$router = static::getContainer()->get('router'); |
174
|
|
|
|
175
|
|
|
//Needed or the server code becomes 500 |
176
|
|
|
$this->testPigInitPOST($client); |
177
|
|
|
|
178
|
|
|
// Generate URL from route name |
179
|
|
|
$url = $router->generate('pig_roll'); |
180
|
|
|
|
181
|
|
|
// Send POST request to the route |
182
|
|
|
for ($i = 0; $i < 5; $i++) { |
183
|
|
|
$client->request('POST', $url); |
184
|
|
|
|
185
|
|
|
// Assert that response is a redirect (302 status) |
186
|
|
|
$response = $client->getResponse(); |
187
|
|
|
$this->assertTrue($response->isRedirect()); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* testRollDice |
193
|
|
|
* |
194
|
|
|
* Test test_roll_dice route |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function testRollDice(): void |
199
|
|
|
{ |
200
|
|
|
// Create a client to browse the application |
201
|
|
|
$client = static::createClient(); |
202
|
|
|
|
203
|
|
|
// Retrieve router service |
204
|
|
|
/** @var RouterInterface $router */ |
205
|
|
|
$router = static::getContainer()->get('router'); |
206
|
|
|
|
207
|
|
|
// Generate URL from route name |
208
|
|
|
$url = $router->generate('test_roll_dice'); |
209
|
|
|
|
210
|
|
|
// Send a GET request to the route |
211
|
|
|
$crawler = $client->request('GET', $url); |
212
|
|
|
|
213
|
|
|
// Assert the response status code |
214
|
|
|
$this->assertEquals(200, $client->getResponse()->getStatusCode()); |
215
|
|
|
|
216
|
|
|
// Assert that certain content exists in the response |
217
|
|
|
$this->assertStringContainsString('Pig Game: Test: Roll', $crawler->filter('title')->text()); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* testRollNumDice |
222
|
|
|
* |
223
|
|
|
* Test test_roll_num_dices route |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
public function testRollNumDice(): void |
228
|
|
|
{ |
229
|
|
|
// Create a client to browse the application |
230
|
|
|
$client = static::createClient(); |
231
|
|
|
|
232
|
|
|
// Retrieve router service |
233
|
|
|
/** @var RouterInterface $router */ |
234
|
|
|
$router = static::getContainer()->get('router'); |
235
|
|
|
|
236
|
|
|
// Generate URL from route name |
237
|
|
|
$url = $router->generate('test_roll_num_dices', ['num' => 3]); |
238
|
|
|
|
239
|
|
|
// Send a GET request to the route |
240
|
|
|
$crawler = $client->request('GET', $url); |
241
|
|
|
|
242
|
|
|
// Assert the response status code |
243
|
|
|
$this->assertEquals(200, $client->getResponse()->getStatusCode()); |
244
|
|
|
|
245
|
|
|
// Assert that certain content exists in the response |
246
|
|
|
$this->assertStringContainsString('Pig Game: Test: Roll', $crawler->filter('title')->text()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* testDicehand |
251
|
|
|
* |
252
|
|
|
* Test test_dicehand route |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function testDicehand(): void |
257
|
|
|
{ |
258
|
|
|
// Create a client to browse the application |
259
|
|
|
$client = static::createClient(); |
260
|
|
|
|
261
|
|
|
// Retrieve router service |
262
|
|
|
/** @var RouterInterface $router */ |
263
|
|
|
$router = static::getContainer()->get('router'); |
264
|
|
|
|
265
|
|
|
// Generate URL from route name |
266
|
|
|
$url = $router->generate('test_dicehand', ['num' => 3]); |
267
|
|
|
|
268
|
|
|
// Send a GET request to the route |
269
|
|
|
$crawler = $client->request('GET', $url); |
270
|
|
|
|
271
|
|
|
// Assert the response status code |
272
|
|
|
$this->assertEquals(200, $client->getResponse()->getStatusCode()); |
273
|
|
|
|
274
|
|
|
// Assert that certain content exists in the response |
275
|
|
|
$this->assertStringContainsString('Pig Game: Test: Dicehand', $crawler->filter('title')->text()); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|