Passed
Push — master ( f83aa9...a7daae )
by Alex
03:39
created

CommonApplicationUnitTest::resultMethodDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 35
rs 9.552
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('Page title', $output);
49
        $this->assertStringContainsString('View rendered content', $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
     * Testing exception wile action-message parsing
172
     */
173
    public function testUnexistingException(): void
174
    {
175
        // assertions
176
        $this->expectException(\Exception::class);
177
178
        // setup
179
        $_GET['action-message'] = 'unexisting-message';
180
        $application = new TestCommonApplication();
181
182
        // test body
183
        $application->result();
184
    }
185
}
186