Completed
Push — master ( 022616...175363 )
by Alex
08:53
created

CommonApplicationUnitTest::testRestException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Application\Tests;
3
4
use Mezon\Rest;
5
use Mezon\Application\View;
6
7
class CommonApplicationUnitTest extends \PHPUnit\Framework\TestCase
8
{
9
10
    /**
11
     * Running with complex router result
12
     */
13
    public function testComplexRouteResult()
14
    {
15
        $application = new TestCommonApplication();
16
17
        $_SERVER['REQUEST_METHOD'] = 'GET';
18
        $_GET['r'] = '/array-result/';
19
20
        ob_start();
21
        $application->run();
22
        $output = ob_get_contents();
23
        ob_end_clean();
24
25
        $this->assertTrue(strpos($output, 'Array result') !== false, 'Template compilation failed (1)');
26
        $this->assertTrue(strpos($output, 'Route main') !== false, 'Template compilation failed (2)');
27
    }
28
29
    /**
30
     * Compiling page with functional view
31
     */
32
    public function testComplexViewRenderring()
33
    {
34
        // setup
35
        $application = new TestCommonApplication();
36
37
        $_SERVER['REQUEST_METHOD'] = 'GET';
38
        $_GET['r'] = '/view-result/';
39
        $_GET['redirect-to'] = 'redirectTo';
40
41
        // test body
42
        ob_start();
43
        $application->run();
44
        $output = ob_get_contents();
45
        ob_end_clean();
46
47
        // assertions
48
        $this->assertStringContainsString('View result', $output);
49
        $this->assertStringContainsString('Test view result', $output);
50
        $this->assertStringContainsString('redirectTo', $output);
51
    }
52
53
    /**
54
     * Method asserts exception field
55
     *
56
     * @param string $output
57
     *            textual representation of the exception
58
     */
59
    protected function assertExceptionFields(string $output): void
60
    {
61
        $this->assertStringContainsString('"message"', $output);
62
        $this->assertStringContainsString('"code"', $output);
63
        $this->assertStringContainsString('"call_stack"', $output);
64
        $this->assertStringContainsString('"host"', $output);
65
    }
66
67
    /**
68
     * Testing handleException method
69
     */
70
    public function testHandleException()
71
    {
72
        // setup
73
        $application = new TestCommonApplication();
74
        $output = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
75
        $e = new \Exception('', 0);
76
77
        // test body
78
        ob_start();
79
        $application->handleException($e);
80
        $output = ob_get_contents();
81
        ob_end_clean();
82
83
        // assertions
84
        $this->assertExceptionFields($output);
85
    }
86
87
    /**
88
     * Testing handle_rest_exception method
89
     */
90
    public function testHandleRestException()
91
    {
92
        // setup
93
        $application = new TestCommonApplication();
94
95
        $e = new Rest\Exception('', 0, 200, '');
96
        // test body
97
        ob_start();
98
        $application->handleRestException($e);
99
        $output = ob_get_contents();
100
        ob_end_clean();
101
102
        // assertions
103
        $this->assertExceptionFields($output);
104
        $this->assertStringContainsString('"httpBody"', $output);
105
    }
106
107
    /**
108
     * Testing handleException method
109
     */
110
    public function testHandleExceptionWithHost()
111
    {
112
        // setup
113
        $application = new TestCommonApplication();
114
        $output = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
115
        $_SERVER['HTTP_HOST'] = 'some host';
116
        $_SERVER['REQUEST_URI'] = 'some uri';
117
        try {
118
            throw (new \Exception('', 0));
119
        } catch (\Exception $e) {
120
            // test body
121
            ob_start();
122
            $application->handleException($e);
123
            $output = ob_get_contents();
124
            ob_end_clean();
125
        }
126
127
        // assertions
128
        $output = json_decode(str_replace('<pre>', '', $output), true);
129
        $this->assertEquals('some hostsome uri', $output['host']);
130
    }
131
132
    /**
133
     * Testing exception throwing after invalid route handling
134
     */
135
    public function testInvalidRouteException(): void
136
    {
137
        // setup and assertions
138
        $_GET['r'] = 'invalid';
139
        $application = $this->getMockBuilder(TestCommonApplication::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

139
        $application = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TestCommonApplication::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
140
            ->setMethods([
141
            'handleException'
142
        ])
143
            ->getMock();
144
        $application->expects($this->once())
145
            ->method('handleException');
146
147
        // test body
148
        $application->run();
149
    }
150
151
    /**
152
     * Testing exception throwing after invalid route handling
153
     */
154
    public function testRestException(): void
155
    {
156
        // setup and assertions
157
        $_GET['r'] = 'rest';
158
        $application = $this->getMockBuilder(TestCommonApplication::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

158
        $application = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TestCommonApplication::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
159
            ->setMethods([
160
            'handleRestException'
161
        ])
162
            ->getMock();
163
        $application->expects($this->once())
164
            ->method('handleRestException');
165
166
        // test body
167
        $application->run();
168
    }
169
170
    /**
171
     * Data provider
172
     *
173
     * @return array data provider
174
     */
175
    public function resultMethodDataProvider(): array
176
    {
177
        $presenter = new TestingPresenter(new View(), 'Result');
178
        return [
179
            [ // #0 testing controller
180
                function (): TestCommonApplication {
181
                    return new TestCommonApplication();
182
                },
183
                new TestingController('Result'),
184
                function (array $params) {
185
                    $this->assertTrue($params[0]->wasCalled);
186
                }
187
            ],
188
            [ // #1 testing presenter
189
                function (): TestCommonApplication {
190
                    return new TestCommonApplication();
191
                },
192
                $presenter,
193
                function (array $params) {
194
                    $this->assertTrue($params[0]->wasCalled);
195
                }
196
            ],
197
            [ // #2 testing action message setup
198
                function (): TestCommonApplication {
199
                    $_GET['action-message'] = 'test-error';
200
                    return new TestCommonApplication();
201
                },
202
                $presenter,
203
                function (array $params): void {
204
                    $this->assertEquals('error', $params[1]->getTemplate()
205
                        ->getPageVar('action-message'));
206
                }
207
            ],
208
            [ // #3 no file with the messages
209
                function (): TestCommonApplication {
210
                    $_GET['action-message'] = 'test-error';
211
                    $application = new TestCommonApplication();
212
                    $application->hasMessages = false;
213
                    return $application;
214
                },
215
                $presenter,
216
                function (array $params): void {
217
                    $this->assertEquals('', $params[1]->getTemplate()
218
                        ->getPageVar('action-message'));
219
                }
220
            ]
221
        ];
222
    }
223
224
    /**
225
     * Testing result() method
226
     *
227
     * @param callable $setup
228
     *            setup of the test
229
     * @param object $handler
230
     *            controller or presenter
231
     * @param callable $assert
232
     *            asserter
233
     * @dataProvider resultMethodDataProvider
234
     */
235
    public function testResultMethod(callable $setup, object $handler, callable $assert = null): void
236
    {
237
        // setup
238
        $application = $setup();
239
240
        // test body
241
        $application->result($handler);
242
243
        // assertions
244
        if ($assert !== null) {
245
            $assert([
246
                $handler,
247
                $application
248
            ]);
249
        }
250
    }
251
252
    /**
253
     * Testing exception wile action-message parsing
254
     */
255
    public function testUnexistingException(): void
256
    {
257
        // assertions
258
        $this->expectException(\Exception::class);
259
260
        // setup
261
        $_GET['action-message'] = 'unexisting-message';
262
        $application = new TestCommonApplication();
263
264
        // test body
265
        $application->result();
266
    }
267
}
268