Test Failed
Push — 6.0 ( dfc5cb...e7c66b )
by yun
03:24
created

HttpTest::testRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 21
rs 9.8666
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\Exception;
13
use think\exception\HttpException;
14
use think\Http;
15
use think\Request;
16
use think\Response;
17
use think\Route;
18
19
class HttpTest extends TestCase
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class HttpTest
Loading history...
20
{
21
    /** @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...
22
    protected $app;
23
24
    /** @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...
25
    protected $http;
26
27
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
28
    {
29
        m::close();
30
    }
31
32
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
33
    {
34
        $this->app = m::mock(App::class)->makePartial();
35
36
        $this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial();
37
    }
38
39
    protected function prepareApp($request, $response)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function prepareApp()
Loading history...
40
    {
41
        $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

41
        $this->app->/** @scrutinizer ignore-call */ 
42
                    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...
42
        $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

42
        $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...
43
        $this->app->shouldReceive('initialize')->once();
44
        $this->app->shouldReceive('get')->with('request')->andReturn($request);
45
46
        $route = m::mock(Route::class);
47
48
        $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...
49
            if ($withRoute) {
50
                $withRoute();
51
            }
52
            return $req === $request;
53
        })->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...
54
55
        $route->shouldReceive('config')->with('route_annotation')->andReturn(true);
56
57
        $this->app->shouldReceive('get')->with('route')->andReturn($route);
58
59
        $console = m::mock(Console::class);
60
61
        $console->shouldReceive('call');
62
63
        $this->app->shouldReceive('get')->with('console')->andReturn($console);
64
    }
65
66
    public function testRun()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testRun()
Loading history...
67
    {
68
        $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...
69
            'app'   => [
70
                'controller'     => [],
71
                'middleware.php' => '<?php return [];',
72
            ],
73
            'route' => [
74
                'route.php' => '<?php return [];',
75
            ],
76
        ]);
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...
77
78
        $this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
79
        $this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
80
81
        $request  = m::mock(Request::class)->makePartial();
82
        $response = m::mock(Response::class)->makePartial();
83
84
        $this->prepareApp($request, $response);
85
86
        $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

86
        $this->assertEquals($response, $this->http->run(/** @scrutinizer ignore-type */ $request));
Loading history...
87
    }
88
89
    /**
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 $index should have a doc-comment as per coding-style.
Loading history...
90
     * @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...
91
     * @dataProvider multiAppRunProvider
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function testMultiAppRun($request, $auto, $index)
94
    {
95
        $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...
96
            'app'   => [
97
                'middleware.php' => '<?php return [];',
98
            ],
99
            'route' => [
100
                'route.php' => '<?php return [];',
101
            ],
102
        ]);
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...
103
104
        $config = m::mock(Config::class)->makePartial();
105
106
        $config->shouldReceive('get')->with('app.auto_multi_app', false)->andReturn($auto);
107
108
        $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...
109
            'www.domain.com' => 'app1',
110
            'app2'           => 'app2',
111
        ]);
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...
112
113
        $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...
114
            'some1' => 'app1',
115
            'some2' => function ($app) {
116
                $this->assertEquals($this->app, $app);
117
            },
118
        ]);
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...
119
120
        $this->app->shouldReceive('get')->with('config')->andReturn($config);
121
122
        $this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
123
        $this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
124
125
        $response = m::mock(Response::class)->makePartial();
126
127
        $this->prepareApp($request, $response);
128
129
        $this->assertTrue($this->http->isMulti());
130
131
        if ($index === 4) {
132
            $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

132
            $this->http->/** @scrutinizer ignore-call */ 
133
                         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...
133
134
            $this->http->shouldReceive('renderException')->once()->andReturn($response);
135
        }
136
137
        $this->assertEquals($response, $this->http->run($request));
138
    }
139
140
    public function multiAppRunProvider()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function multiAppRunProvider()
Loading history...
141
    {
142
        $request1 = m::mock(Request::class)->makePartial();
143
        $request1->shouldReceive('subDomain')->andReturn('www');
144
        $request1->shouldReceive('host')->andReturn('www.domain.com');
145
146
        $request2 = m::mock(Request::class)->makePartial();
147
        $request2->shouldReceive('subDomain')->andReturn('app2');
148
        $request2->shouldReceive('host')->andReturn('app2.domain.com');
149
150
        $request3 = m::mock(Request::class)->makePartial();
151
        $request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c');
152
153
        $request4 = m::mock(Request::class)->makePartial();
154
        $request4->shouldReceive('pathinfo')->andReturn('app1/a/b/c');
155
156
        return [
157
            [$request1, true, 1],
158
            [$request2, true, 2],
159
            [$request3, true, 3],
160
            [$request4, true, 4],
161
            [$request1, false, 5],
162
        ];
163
    }
164
165
    public function testRunWithException()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testRunWithException()
Loading history...
166
    {
167
        $request  = m::mock(Request::class);
168
        $response = m::mock(Response::class);
169
170
        $this->app->shouldReceive('instance')->once()->with('request', $request);
171
172
        $exception = new Exception();
173
174
        $this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception);
175
176
        $this->http->shouldReceive('reportException')->once()->with($exception);
177
178
        $this->http->shouldReceive('renderException')->once()->with($request, $exception)->andReturn($response);
179
180
        $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

180
        $this->assertEquals($response, $this->http->run(/** @scrutinizer ignore-type */ $request));
Loading history...
181
    }
182
183
}