testAppProductUpdateAndDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 38
rs 9.7998
cc 1
nc 1
nop 0
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 ProductControllerTest extends WebTestCase
10
{
11
    private static ?int $testId = null;
12
13
    /**
14
     * testException
15
     *
16
     * Test the exception handling
17
     *
18
     * @param KernelBrowser $client
19
     * @param  string $url
20
     * @param  string $errorMessage
21
     * @return void
22
     */
23
    private function testException(KernelBrowser $client, string $url, string $errorMessage = ""): void
24
    {
25
        // Disable exception catching
26
        $client->catchExceptions(false);
27
28
        //Test the exception handling
29
        $this->expectException(\Exception::class);
30
        if ($errorMessage != "") {
31
            $this->expectExceptionMessage($errorMessage);
32
        }
33
34
        // Send a GET request to the route
35
        $client->request('GET', $url);
36
37
        // Re-enable exception catching
38
        $client->catchExceptions(true);
39
    }
40
41
    /**
42
     * testAppProduct
43
     *
44
     * Test app_product route
45
     *
46
     * @return void
47
     */
48
    public function testAppProduct(): void
49
    {
50
        // Create a client to browse the application
51
        $client = static::createClient();
52
53
        // Retrieve router service
54
        /** @var RouterInterface $router */
55
        $router = static::getContainer()->get('router');
56
57
        // Generate URL from route name
58
        $url = $router->generate('app_product');
59
60
        // Send a GET request to the route
61
        $crawler = $client->request('GET', $url);
62
63
        // Assert the response status code
64
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
65
66
        // Assert that certain content exists in the response
67
        $this->assertStringContainsString('Hello ProductController!', $crawler->filter('title')->text());
68
    }
69
70
    /**
71
     * testAppProductCreate
72
     *
73
     * Test product_create route
74
     *
75
     * @return void
76
     */
77
    public function testAppProductCreate(): void
78
    {
79
        // Create a client to browse the application
80
        $client = static::createClient();
81
82
        // Retrieve router service
83
        /** @var RouterInterface $router */
84
        $router = static::getContainer()->get('router');
85
86
        // Generate URL from route name
87
        $url = $router->generate('product_create');
88
89
        // Send a GET request to the route
90
        $crawler = $client->request('GET', $url);
91
92
        // Assert response status
93
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
94
95
        // Set up testing variable
96
        self::$testId = intval(substr($crawler->text(), 26));
97
    }
98
99
    /**
100
     * testAppProductShow
101
     *
102
     * Test product_show_all route
103
     *
104
     * @return void
105
     */
106
    public function testAppProductShow(): void
107
    {
108
        // Create a client to browse the application
109
        $client = static::createClient();
110
111
        // Retrieve router service
112
        /** @var RouterInterface $router */
113
        $router = static::getContainer()->get('router');
114
115
        // Generate URL from route name
116
        $url = $router->generate('product_show_all');
117
118
        // Send a GET request to the route
119
        $client->request('GET', $url);
120
121
        // Assert response status
122
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
123
    }
124
125
    /**
126
     * testAppProductShowId
127
     *
128
     * Test product_by_id route
129
     *
130
     * @depends testAppProductCreate
131
     *
132
     * @return void
133
     */
134
    public function testAppProductShowId(): void
135
    {
136
        // Create a client to browse the application
137
        $client = static::createClient();
138
139
        // Retrieve router service
140
        /** @var RouterInterface $router */
141
        $router = static::getContainer()->get('router');
142
143
        // Generate URL from route name
144
        $url = $router->generate('product_by_id', ['id' => self::$testId]);
145
146
        // Send a GET request to the route
147
        $client->request('GET', $url);
148
149
        // Assert response status
150
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
151
    }
152
153
    /**
154
     * testAppProductUpdateAndDelete
155
     *
156
     * Test product_delete_by_id and product_update route
157
     *
158
     * @depends testAppProductCreate
159
     *
160
     * @return void
161
     */
162
    public function testAppProductUpdateAndDelete(): void
163
    {
164
        // Create a client to browse the application
165
        $client = static::createClient();
166
167
        // Retrieve router service
168
        /** @var RouterInterface $router */
169
        $router = static::getContainer()->get('router');
170
171
        // Generate URL from route name
172
        $urlUpdate = $router->generate('product_update', ['id' => self::$testId, 'value' => 69]);
173
174
        // Generate URL from route name
175
        $urlDelete = $router->generate('product_delete_by_id', ['id' => self::$testId]);
176
177
        // Send a GET request to the route
178
        $client->request('GET', $urlUpdate);
179
180
        // Assert that response is a redirect (302 status)
181
        $response = $client->getResponse();
182
        $this->assertTrue($response->isRedirect());
183
184
        // Send a GET request to the route
185
        $client->request('GET', $urlDelete);
186
187
        // Assert that response is a redirect (302 status)
188
        $response = $client->getResponse();
189
        $this->assertTrue($response->isRedirect());
190
191
        // Error message to look for
192
        $errorMessageUpdate = "No product found for id " . self::$testId;
193
194
        // Error message to look for
195
        $errorMessageDelete = "No product found for id " . self::$testId;
196
197
        $this->testException($client, $urlUpdate, $errorMessageUpdate);
198
199
        $this->testException($client, $urlDelete, $errorMessageDelete);
200
    }
201
202
    /**
203
     * testAppProductUpdateException
204
     *
205
     * Test product_update route exception handling
206
     *
207
     * @depends testAppProductUpdateAndDelete
208
     *
209
     * @return void
210
     */
211
    public function testAppProductUpdateException(): void
212
    {
213
        // Create a client to browse the application
214
        $client = static::createClient();
215
216
        // Retrieve router service
217
        /** @var RouterInterface $router */
218
        $router = static::getContainer()->get('router');
219
220
        // Generate URL from route name
221
        $url = $router->generate('product_update', ['id' => self::$testId, 'value' => 69]);
222
223
        // Error message to look for
224
        $errorMessage = "No product found for id " . self::$testId;
225
226
        $this->testException($client, $url, $errorMessage);
227
    }
228
229
    /**
230
     * testAppProductDeleteException
231
     *
232
     * Test product_delete_by_id route exception handling
233
     *
234
     * @depends testAppProductUpdateAndDelete
235
     *
236
     * @return void
237
     */
238
    public function testAppProductDeleteException(): void
239
    {
240
        // Create a client to browse the application
241
        $client = static::createClient();
242
243
        // Retrieve router service
244
        /** @var RouterInterface $router */
245
        $router = static::getContainer()->get('router');
246
247
        // Generate URL from route name
248
        $url = $router->generate('product_delete_by_id', ['id' => self::$testId]);
249
250
        // Error message to look for
251
        $errorMessage = "No product found for id " . self::$testId;
252
253
        $this->testException($client, $url, $errorMessage);
254
    }
255
256
    /**
257
     * testAppProductView
258
     *
259
     * Test product_view_all route
260
     *
261
     * @return void
262
     */
263
    public function testAppProductView(): void
264
    {
265
        // Create a client to browse the application
266
        $client = static::createClient();
267
268
        // Retrieve router service
269
        /** @var RouterInterface $router */
270
        $router = static::getContainer()->get('router');
271
272
        // Generate URL from route name
273
        $url = $router->generate('product_view_all');
274
275
        // Send a GET request to the route
276
        $crawler = $client->request('GET', $url);
277
278
        // Assert the response status code
279
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
280
281
        // Assert that certain content exists in the response
282
        $this->assertStringContainsString('Hello ProductController!', $crawler->filter('title')->text());
283
    }
284
285
    /**
286
     * testAppProductViewWithMinimum
287
     *
288
     * Test product_view_minimum_value route
289
     *
290
     * @return void
291
     */
292
    public function testAppProductViewWithMinimum(): void
293
    {
294
        // Create a client to browse the application
295
        $client = static::createClient();
296
297
        // Retrieve router service
298
        /** @var RouterInterface $router */
299
        $router = static::getContainer()->get('router');
300
301
        // Generate URL from route name
302
        $url = $router->generate('product_view_minimum_value', ['value' => 5]);
303
304
        // Send a GET request to the route
305
        $crawler = $client->request('GET', $url);
306
307
        // Assert the response status code
308
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
309
310
        // Assert that certain content exists in the response
311
        $this->assertStringContainsString('Hello ProductController!', $crawler->filter('title')->text());
312
    }
313
314
    /**
315
     * testAppProductViewByMinimum
316
     *
317
     * Test product_by_min_value route
318
     *
319
     * @return void
320
     */
321
    public function testAppProductViewByMinimum(): void
322
    {
323
        // Create a client to browse the application
324
        $client = static::createClient();
325
326
        // Retrieve router service
327
        /** @var RouterInterface $router */
328
        $router = static::getContainer()->get('router');
329
330
        // Generate URL from route name
331
        $url = $router->generate('product_by_min_value', ['value' => 5]);
332
333
        // Send a GET request to the route
334
        $client->request('GET', $url);
335
336
        // Assert the response status code
337
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
338
    }
339
}
340