Passed
Push — 6.0 ( 0b31ac...12bd4b )
by yun
02:48
created

HttpTest::testEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\tests;
4
5
use Mockery as m;
6
use Mockery\MockInterface;
7
use org\bovigo\vfs\vfsStream;
8
use PHPUnit\Framework\TestCase;
9
use think\App;
10
use think\Config;
11
use think\Console;
12
use think\Event;
13
use think\Exception;
14
use think\exception\Handle;
15
use think\Http;
16
use think\Log;
17
use think\Request;
18
use think\Response;
19
use think\Route;
20
use think\Session;
21
22
class HttpTest extends TestCase
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class HttpTest
Loading history...
23
{
24
    /** @var App|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
25
    protected $app;
26
27
    /** @var Http|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
28
    protected $http;
29
30
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
31
    {
32
        m::close();
33
    }
34
35
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
36
    {
37
        $this->app = m::mock(App::class)->makePartial();
38
39
        $this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial();
40
    }
41
42
    protected function prepareApp($request, $response)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function prepareApp()
Loading history...
43
    {
44
        $this->app->shouldReceive('instance')->once()->with('request', $request);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on think\App. ( Ignorable by Annotation )

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

44
        $this->app->/** @scrutinizer ignore-call */ 
45
                    shouldReceive('instance')->once()->with('request', $request);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        $this->app->shouldReceive('initialized')->once()->andReturnFalse();
0 ignored issues
show
Bug introduced by
The method andReturnFalse() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

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

45
        $this->app->shouldReceive('initialized')->once()->/** @scrutinizer ignore-call */ andReturnFalse();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        $this->app->shouldReceive('initialize')->once();
47
        $this->app->shouldReceive('get')->with('request')->andReturn($request);
48
49
        $route = m::mock(Route::class);
50
51
        $route->shouldReceive('dispatch')->withArgs(function ($req, $withRoute) use ($request) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
52
            if ($withRoute) {
53
                $withRoute();
54
            }
55
            return $req === $request;
56
        })->andReturn($response);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
57
58
        $route->shouldReceive('config')->with('route_annotation')->andReturn(true);
59
60
        $this->app->shouldReceive('get')->with('route')->andReturn($route);
61
62
        $console = m::mock(Console::class);
63
64
        $console->shouldReceive('call');
65
66
        $this->app->shouldReceive('get')->with('console')->andReturn($console);
67
    }
68
69
    public function testRun()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testRun()
Loading history...
70
    {
71
        $root = vfsStream::setup('rootDir', null, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
72
            'app'   => [
73
                'controller'     => [],
74
                'middleware.php' => '<?php return [];',
75
            ],
76
            'route' => [
77
                'route.php' => '<?php return [];',
78
            ],
79
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
80
81
        $this->http->multi(false);
82
83
        $this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
84
        $this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
85
86
        $request  = m::mock(Request::class)->makePartial();
87
        $response = m::mock(Response::class)->makePartial();
88
89
        $this->prepareApp($request, $response);
90
91
        $this->assertEquals($response, $this->http->run($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type null|think\Request expected by parameter $request of think\Http::run(). ( Ignorable by Annotation )

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

91
        $this->assertEquals($response, $this->http->run(/** @scrutinizer ignore-type */ $request));
Loading history...
92
93
        $this->assertFalse($this->http->isMulti());
94
    }
95
96
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $auto should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $path should have a doc-comment as per coding-style.
Loading history...
97
     * @param $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 8 spaces but found 1
Loading history...
98
     * @param $auto
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 8 spaces but found 1
Loading history...
99
     * @param $name
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 8 spaces but found 1
Loading history...
100
     * @dataProvider multiAppRunProvider
101
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
102
    public function testMultiAppRun($request, $auto, $name, $path = null)
103
    {
104
        $root = vfsStream::setup('rootDir', null, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
105
            'app'    => [
106
                'middleware.php' => '<?php return [];',
107
                'app1'           => [
108
                    'common.php'     => '',
109
                    'event.php'      => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];',
110
                    'provider.php'   => '<?php return [];',
111
                    'middleware.php' => '<?php return [];',
112
                    'config'         => [
113
                        'app.php' => '<?php return [];',
114
                    ],
115
                ],
116
            ],
117
            'config' => [
118
                'app.php' => '<?php return [];',
119
                'app1'    => [
120
                    'app.php' => '<?php return [];',
121
                ],
122
            ],
123
            'route'  => [
124
                'route.php' => '<?php return [];',
125
            ],
126
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
127
128
        $config = m::mock(Config::class)->makePartial();
129
130
        $config->shouldReceive('get')->with('app.auto_multi_app', false)->andReturn($auto);
131
132
        $config->shouldReceive('get')->with('app.domain_bind', [])->andReturn([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
133
            'www.domain.com' => 'app1',
134
            'app2'           => 'app2',
135
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
136
137
        $config->shouldReceive('get')->with('app.app_map', [])->andReturn([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
138
            'some1' => 'app3',
139
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
140
141
        $this->app->shouldReceive('get')->with('config')->andReturn($config);
142
143
        $this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
144
        $this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
145
        $this->app->shouldReceive('getConfigPath')->andReturn($root->getChild('config')->url() . DIRECTORY_SEPARATOR);
146
147
        $response = m::mock(Response::class)->makePartial();
148
149
        $this->prepareApp($request, $response);
150
151
        $this->assertTrue($this->http->isMulti());
152
153
        if ($name === null) {
154
            $this->http->shouldReceive('reportException')->once();
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on think\Http. ( Ignorable by Annotation )

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

154
            $this->http->/** @scrutinizer ignore-call */ 
155
                         shouldReceive('reportException')->once();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
156
            $this->http->shouldReceive('renderException')->once()->andReturn($response);
157
        }
158
159
        if (!$auto) {
160
            $this->http->name($name);
161
        }
162
163
        if ($path) {
164
            $this->http->path($path);
165
        }
166
167
        $this->assertEquals($response, $this->http->run($request));
168
169
        if ($name !== null) {
170
            $this->assertEquals($name, $this->http->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Mockery\MockInterface. Did you maybe mean mockery_getName()? ( Ignorable by Annotation )

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

170
            $this->assertEquals($name, $this->http->/** @scrutinizer ignore-call */ getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
        }
172
173
        if ($name === 'app1' || $name === 'app2') {
174
            $this->assertTrue($this->http->isBindDomain());
175
        }
176
        if ($path) {
177
            $this->assertEquals($path, $this->app->getAppPath());
178
        }
179
    }
180
181
    public function multiAppRunProvider()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function multiAppRunProvider()
Loading history...
182
    {
183
        $request1 = m::mock(Request::class)->makePartial();
184
        $request1->shouldReceive('subDomain')->andReturn('www');
185
        $request1->shouldReceive('host')->andReturn('www.domain.com');
186
187
        $request2 = m::mock(Request::class)->makePartial();
188
        $request2->shouldReceive('subDomain')->andReturn('app2');
189
        $request2->shouldReceive('host')->andReturn('app2.domain.com');
190
191
        $request3 = m::mock(Request::class)->makePartial();
192
        $request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c');
193
194
        $request4 = m::mock(Request::class)->makePartial();
195
        $request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c');
196
197
        $request5 = m::mock(Request::class)->makePartial();
198
        $request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c');
199
200
        return [
201
            [$request1, true, 'app1'],
202
            [$request2, true, 'app2'],
203
            [$request3, true, 'app3'],
204
            [$request4, true, null],
205
            [$request5, true, 'some2', 'path'],
206
            [$request1, false, 'some3'],
207
        ];
208
    }
209
210
    public function testRunWithException()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testRunWithException()
Loading history...
211
    {
212
        $request  = m::mock(Request::class);
213
        $response = m::mock(Response::class);
214
215
        $this->app->shouldReceive('instance')->once()->with('request', $request);
216
217
        $exception = new Exception();
218
219
        $this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception);
220
221
        $handle = m::mock(Handle::class);
222
223
        $handle->shouldReceive('report')->once()->with($exception);
224
        $handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response);
225
226
        $this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle);
227
228
        $response->shouldReceive('setCookie')->andReturn($response);
229
230
        $this->assertEquals($response, $this->http->run($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\MockInterface is incompatible with the type null|think\Request expected by parameter $request of think\Http::run(). ( Ignorable by Annotation )

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

230
        $this->assertEquals($response, $this->http->run(/** @scrutinizer ignore-type */ $request));
Loading history...
231
    }
232
233
    public function testEnd()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testEnd()
Loading history...
234
    {
235
        $response = m::mock(Response::class);
236
        $event    = m::mock(Event::class);
237
        $event->shouldReceive('trigger')->once()->with('HttpEnd', $response);
238
        $this->app->shouldReceive('get')->once()->with('event')->andReturn($event);
239
        $log = m::mock(Log::class);
240
        $log->shouldReceive('save')->once();
241
        $this->app->shouldReceive('get')->once()->with('log')->andReturn($log);
242
        $session = m::mock(Session::class);
243
        $session->shouldReceive('save')->once();
244
        $this->app->shouldReceive('get')->once()->with('session')->andReturn($session);
245
246
        $this->http->end($response);
0 ignored issues
show
Bug introduced by
$response of type Mockery\MockInterface is incompatible with the type think\Response expected by parameter $response of think\Http::end(). ( Ignorable by Annotation )

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

246
        $this->http->end(/** @scrutinizer ignore-type */ $response);
Loading history...
247
    }
248
249
}
250