CardGameControllerExceptionTest::resetDeck()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
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 CardGameControllerExceptionTest 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
     * resetDeck
41
     *
42
     * Reset the deck for further testing
43
     *
44
     * @return void
45
     */
46
    private function resetDeck(KernelBrowser $client, RouterInterface $router): void
47
    {
48
        // Generate URL from route name
49
        $url = $router->generate('card_session_delete');
50
51
        // Send a GET request to the route
52
        $client->request('GET', $url);
53
    }
54
55
    /**
56
     * testCardDeckDrawEmptyException
57
     *
58
     * Test the exception handling for drawing when the deck is empty
59
     *
60
     * @return void
61
     */
62
    public function testCardDeckDrawEmptyException(): void
63
    {
64
        // Create a client to browse the application
65
        $client = static::createClient();
66
67
        // Retrieve router service
68
        /** @var RouterInterface $router */
69
        $router = static::getContainer()->get('router');
70
71
        //Reset deck
72
        $this->resetDeck($client, $router);
73
74
        // Generate URL from route name
75
        $url = $router->generate('card_deck_draw_many', ['num' => 52]);
76
77
        // Send a GET request to the route
78
        $client->request('GET', $url);
79
80
        // Generate URL from route name
81
        $url = $router->generate('card_deck_draw');
82
83
        // Error message to look for
84
        $errorMessage = "Can't draw more cards as the deck is empty!";
85
86
        $this->testException($client, $url, $errorMessage);
87
    }
88
89
    /**
90
     * testCardDeckDrawManyHighException
91
     *
92
     * Test the exception handling for higher bound
93
     *
94
     * @return void
95
     */
96
    public function testCardDeckDrawManyHighException(): void
97
    {
98
        // Create a client to browse the application
99
        $client = static::createClient();
100
101
        // Retrieve router service
102
        /** @var RouterInterface $router */
103
        $router = static::getContainer()->get('router');
104
105
        // Generate URL from route name
106
        $url = $router->generate('card_deck_draw_many', ['num' => 53]);
107
108
        // Error message to look for
109
        $errorMessage = "Can't draw more than cards in deck!";
110
111
        // Disable exception catching
112
        $client->catchExceptions(false);
113
114
        $this->testException($client, $url, $errorMessage);
115
    }
116
117
    /**
118
     * testCardDeckDrawManyLowException
119
     *
120
     * Test the exception handling for lower bound
121
     *
122
     * @return void
123
     */
124
    public function testCardDeckDrawManyLowException(): 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('card_deck_draw_many', ['num' => 0]);
135
136
        // Error message to look for
137
        $errorMessage = "Can't draw less than 1 card!";
138
139
        $this->testException($client, $url, $errorMessage);
140
    }
141
142
    /**
143
     * testCardDeckDrawManyToManyException
144
     *
145
     * Test the exception handling for drawing more cards then the deck have
146
     *
147
     * @return void
148
     */
149
    public function testCardDeckDrawManyToManyException(): void
150
    {
151
        // Create a client to browse the application
152
        $client = static::createClient();
153
154
        // Retrieve router service
155
        /** @var RouterInterface $router */
156
        $router = static::getContainer()->get('router');
157
158
        //Reset deck
159
        $this->resetDeck($client, $router);
160
161
        // Generate URL from route name
162
        $url = $router->generate('card_deck_draw_many', ['num' => 51]);
163
164
        // Send a GET request to the route
165
        $client->request('GET', $url);
166
167
        //Test the exception handling for drawing more cards then the deck have
168
169
        // Generate URL from route name
170
        $url = $router->generate('card_deck_draw_many', ['num' => 2]);
171
172
        $this->testException($client, $url);
173
    }
174
175
    /**
176
     * testCardDeckDrawManyEmptyException
177
     *
178
     * Test the exception handling for drawing when the deck is empty
179
     *
180
     * @return void
181
     */
182
    public function testCardDeckDrawManyEmptyException(): void
183
    {
184
        // Create a client to browse the application
185
        $client = static::createClient();
186
187
        // Retrieve router service
188
        /** @var RouterInterface $router */
189
        $router = static::getContainer()->get('router');
190
191
        //Reset deck
192
        $this->resetDeck($client, $router);
193
194
        // Generate URL from route name
195
        $url = $router->generate('card_deck_draw_many', ['num' => 52]);
196
197
        // Send a GET request to the route
198
        $client->request('GET', $url);
199
200
        // Generate URL from route name
201
        $url = $router->generate('card_deck_draw_many', ['num' => 1]);
202
203
        // Error message to look for
204
        $errorMessage = "Can't draw more cards as the deck is empty!";
205
206
        $this->testException($client, $url, $errorMessage);
207
    }
208
}
209