Passed
Push — main ( 101f2d...06b8a6 )
by Emil
25:10
created

ProjectControllerExceptionTest::testException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 16
rs 10
1
<?php
2
3
namespace App\Tests\Controller;
4
5
use App\Game\BlackJack;
6
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
8
use Symfony\Component\Routing\RouterInterface;
9
10
class ProjectControllerExceptionTest extends WebTestCase
11
{
12
    /**
13
     * testException
14
     *
15
     * Test the exception handling
16
     *
17
     * @param KernelBrowser $client
18
     * @param  string $url
19
     * @param  string $errorMessage
20
     * @return void
21
     */
22
    private function testException(KernelBrowser $client, string $url, string $errorMessage = ""): void
23
    {
24
        // Disable exception catching
25
        $client->catchExceptions(false);
26
27
        //Test the exception handling
28
        $this->expectException(\Exception::class);
29
        if ($errorMessage != "") {
30
            $this->expectExceptionMessage($errorMessage);
31
        }
32
33
        // Send a GET request to the route
34
        $client->request('GET', $url);
35
36
        // Re-enable exception catching
37
        $client->catchExceptions(true);
38
    }
39
40
    /**
41
     * testProjectPlayerInitHighException
42
     *
43
     * Test the exception handling for higher bound
44
     *
45
     * @return void
46
     */
47
    public function testProjectPlayerInitHighException(): void
48
    {
49
        // Create a client to browse the application
50
        $client = static::createClient();
51
52
        // Retrieve router service
53
        /** @var RouterInterface $router */
54
        $router = static::getContainer()->get('router');
55
56
        // Generate URL from route name
57
        $url = $router->generate('proj_player_init', ['num' => 100]);
58
59
        // Error message to look for
60
        $errorMessage = "Can't have more then ".BlackJack::MAX_PLAYERS.'players!';
61
62
        $this->testException($client, $url, $errorMessage);
63
    }
64
65
    /**
66
     * testProjectPlayerInitLowException
67
     *
68
     * Test the exception handling for lower bound
69
     *
70
     * @return void
71
     */
72
    public function testProjectPlayerInitLowException(): void
73
    {
74
        // Create a client to browse the application
75
        $client = static::createClient();
76
77
        // Retrieve router service
78
        /** @var RouterInterface $router */
79
        $router = static::getContainer()->get('router');
80
81
        // Generate URL from route name
82
        $url = $router->generate('proj_player_init', ['num' => 0]);
83
84
        // Error message to look for
85
        $errorMessage = "Can't have less than 1 player!";
86
87
        // Disable exception catching
88
        $client->catchExceptions(false);
89
90
        $this->testException($client, $url, $errorMessage);
91
    }
92
93
    /**
94
     * testProjBlackJackGameNoSession
95
     *
96
     * Test proj_BlackJack_game route with no session
97
     *
98
     * @return void
99
     */
100
    public function testProjBlackJackGameNoSession(): void
101
    {
102
        // Create a client to browse the application
103
        $client = static::createClient();
104
105
        // Retrieve router service
106
        /** @var RouterInterface $router */
107
        $router = static::getContainer()->get('router');
108
109
        // Generate URL
110
        $url = $router->generate('proj_BlackJack_game');
111
112
        // Make the request
113
        $client->request('GET', $url);
114
115
        // Assert that response is a redirect (302 status)
116
        $response = $client->getResponse();
117
        $this->assertTrue($response->isRedirect());
118
    }
119
120
    /**
121
     * testProjBlackJackBetNoSession
122
     *
123
     * Test proj_BlackJack_Bet route with no session
124
     *
125
     * @return void
126
     */
127
    public function testProjBlackJackBetNoSession(): void
128
    {
129
        // Create a client to browse the application
130
        $client = static::createClient();
131
132
        // Retrieve router service
133
        /** @var RouterInterface $router */
134
        $router = static::getContainer()->get('router');
135
136
        // Generate URL from route name
137
        $url = $router->generate('proj_BlackJack_Bet');
138
139
        // Send POST request to the route
140
        $client->request('POST', $url);
141
142
        // Assert that response is a redirect (302 status)
143
        $response = $client->getResponse();
144
        $this->assertTrue($response->isRedirect());
145
    }
146
147
    /**
148
     * testProjBlackJackDoubleDownNoSession
149
     *
150
     * Test proj_BlackJack_DoubleDown route with no session
151
     *
152
     * @return void
153
     */
154
    public function testProjBlackJackDoubleDownNoSession(): void
155
    {
156
        // Create a client to browse the application
157
        $client = static::createClient();
158
159
        // Retrieve router service
160
        /** @var RouterInterface $router */
161
        $router = static::getContainer()->get('router');
162
163
        // Generate URL from route name
164
        $url = $router->generate('proj_BlackJack_DoubleDown');
165
166
        // Send POST request to the route
167
        $client->request('POST', $url);
168
169
        // Assert that response is a redirect (302 status)
170
        $response = $client->getResponse();
171
        $this->assertTrue($response->isRedirect());
172
    }
173
174
    /**
175
     * testProjBlackJackHitNoSession
176
     *
177
     * Test proj_BlackJack_Hit route with no session
178
     *
179
     * @return void
180
     */
181
    public function testProjBlackJackHitNoSession(): void
182
    {
183
        // Create a client to browse the application
184
        $client = static::createClient();
185
186
        // Retrieve router service
187
        /** @var RouterInterface $router */
188
        $router = static::getContainer()->get('router');
189
190
        // Generate URL from route name
191
        $url = $router->generate('proj_BlackJack_Hit');
192
193
        // Send POST request to the route
194
        $client->request('POST', $url);
195
196
        // Assert that response is a redirect (302 status)
197
        $response = $client->getResponse();
198
        $this->assertTrue($response->isRedirect());
199
    }
200
201
    /**
202
     * testProjBlackJackStayNoSession
203
     *
204
     * Test proj_BlackJack_Stay route with no session
205
     *
206
     * @return void
207
     */
208
    public function testProjBlackJackStayNoSession(): void
209
    {
210
        // Create a client to browse the application
211
        $client = static::createClient();
212
213
        // Retrieve router service
214
        /** @var RouterInterface $router */
215
        $router = static::getContainer()->get('router');
216
217
        // Generate URL from route name
218
        $url = $router->generate('proj_BlackJack_Stay');
219
220
        // Send POST request to the route
221
        $client->request('POST', $url);
222
223
        // Assert that response is a redirect (302 status)
224
        $response = $client->getResponse();
225
        $this->assertTrue($response->isRedirect());
226
    }
227
}
228