Passed
Push — master ( 94bd17...112d90 )
by Alex
02:16
created

TestCommonApplication   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A actionArrayResult() 0 5 1
A actionRest() 0 3 1
A actionViewResult() 0 5 1
A actionInvalid() 0 3 1
A redirectTo() 0 1 1
1
<?php
2
use Mezon\HtmlTemplate\HtmlTemplate;
3
use Mezon\Rest;
4
5
/**
6
 * View class
7
 *
8
 * @author Dodonov A.A.
9
 */
10
class TestView extends \Mezon\Application\View
11
{
12
13
    public function __construct(string $content)
14
    {
15
        parent::__construct(null, 'default');
16
17
        $this->content = $content;
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
    }
19
20
    public function render(string $viewName = ''): string
21
    {
22
        return $this->content;
23
    }
24
}
25
26
/**
27
 * Application for testing purposes.
28
 */
29
class TestCommonApplication extends \Mezon\Application\CommonApplication
30
{
31
32
    /**
33
     * Constructor
34
     */
35
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        parent::__construct(new HtmlTemplate(__DIR__, 'index'));
38
    }
39
40
    function actionArrayResult(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
    {
42
        return [
43
            'title' => 'Array result',
44
            'main' => 'Route main'
45
        ];
46
    }
47
48
    function actionViewResult(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
    {
50
        return [
51
            'title' => 'View result',
52
            'main' => new TestView('Test view result')
53
        ];
54
    }
55
56
    function actionInvalid(): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
    {
58
        return 'Invalid';
59
    }
60
61
    function actionRest(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
    {
63
        throw (new Rest\Exception('exception', - 1, 502, 'body'));
64
    }
65
66
    function redirectTo($url):void{
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
        // do nothing
68
    }
69
}
70
71
class CommonApplicationUnitTest extends \PHPUnit\Framework\TestCase
72
{
73
74
    /**
75
     * Running with complex router result
76
     */
77
    public function testComplexRouteResult()
78
    {
79
        $application = new TestCommonApplication();
80
81
        $_SERVER['REQUEST_METHOD'] = 'GET';
82
        $_GET['r'] = '/array-result/';
83
84
        ob_start();
85
        $application->run();
86
        $output = ob_get_contents();
87
        ob_end_clean();
88
89
        $this->assertTrue(strpos($output, 'Array result') !== false, 'Template compilation failed (1)');
90
        $this->assertTrue(strpos($output, 'Route main') !== false, 'Template compilation failed (2)');
91
    }
92
93
    /**
94
     * Compiling page with functional view
95
     */
96
    public function testComplexViewRenderring()
97
    {
98
        // setup
99
        $application = new TestCommonApplication();
100
101
        $_SERVER['REQUEST_METHOD'] = 'GET';
102
        $_GET['r'] = '/view-result/';
103
        $_GET['redirect-to'] = 'redirectTo';
104
105
        // test body
106
        ob_start();
107
        $application->run();
108
        $output = ob_get_contents();
109
        ob_end_clean();
110
111
        // assertions
112
        $this->assertStringContainsString('View result', $output);
113
        $this->assertStringContainsString('Test view result', $output);
114
        $this->assertStringContainsString('redirectTo', $output);
115
    }
116
117
    /**
118
     * Testing handleException method
119
     */
120
    public function testHandleException()
121
    {
122
        // setup
123
        $application = new TestCommonApplication();
124
        $output = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
125
        $e = new Exception('', 0);
126
127
        // test body
128
        ob_start();
129
        $application->handleException($e);
130
        $output = ob_get_contents();
131
        ob_end_clean();
132
133
        // assertions
134
        $this->assertStringContainsString('"message"', $output);
135
        $this->assertStringContainsString('"code"', $output);
136
        $this->assertStringContainsString('"call_stack"', $output);
137
        $this->assertStringContainsString('"host"', $output);
138
    }
139
140
    /**
141
     * Testing handle_rest_exception method
142
     */
143
    public function testHandleRestException()
144
    {
145
        // setup
146
        $application = new TestCommonApplication();
147
148
        $e = new Rest\Exception('', 0, 200, '');
149
        // test body
150
        ob_start();
151
        $application->handleRestException($e);
152
        $output = ob_get_contents();
153
        ob_end_clean();
154
155
        // assertions
156
        $this->assertStringContainsString('"message"', $output);
157
        $this->assertStringContainsString('"code"', $output);
158
        $this->assertStringContainsString('"call_stack"', $output);
159
        $this->assertStringContainsString('"host"', $output);
160
        $this->assertStringContainsString('"host"', $output);
161
        $this->assertStringContainsString('"httpBody"', $output);
162
    }
163
164
    /**
165
     * Testing handleException method
166
     */
167
    public function testHandleExceptionWithHost()
168
    {
169
        // setup
170
        $application = new TestCommonApplication();
171
        $output = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
172
        $_SERVER['HTTP_HOST'] = 'some host';
173
        $_SERVER['REQUEST_URI'] = 'some uri';
174
        try {
175
            throw (new Exception('', 0));
176
        } catch (Exception $e) {
177
            // test body
178
            ob_start();
179
            $application->handleException($e);
180
            $output = ob_get_contents();
181
            ob_end_clean();
182
        }
183
184
        // assertions
185
        $output = json_decode(str_replace('<pre>', '', $output), true);
186
        $this->assertEquals('some hostsome uri', $output['host']);
187
    }
188
189
    /**
190
     * Testing exception throwing after invalid route handling
191
     */
192
    public function testInvalidRouteException(): void
193
    {
194
        // setup and assertions
195
        $_GET['r'] = 'invalid';
196
        $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

196
        $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...
197
            ->setMethods([
198
            'handleException'
199
        ])
200
            ->getMock();
201
        $application->expects($this->once())
202
            ->method('handleException');
203
204
        // test body
205
        $application->run();
206
    }
207
208
    /**
209
     * Testing exception throwing after invalid route handling
210
     */
211
    public function testRestException(): void
212
    {
213
        // setup and assertions
214
        $_GET['r'] = 'rest';
215
        $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

215
        $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...
216
            ->setMethods([
217
            'handleRestException'
218
        ])
219
            ->getMock();
220
        $application->expects($this->once())
221
            ->method('handleRestException');
222
223
        // test body
224
        $application->run();
225
    }
226
}
227