Completed
Push — 6.0 ( 09da7f...677f21 )
by liu
06:22
created

HttpTest::testMultiAppRun()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 76
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 44
c 1
b 0
f 0
nc 64
nop 4
dl 0
loc 76
rs 7.9715

2 Methods

Rating   Name   Duplication   Size   Complexity  
A HttpTest::testRunWithException() 0 21 1
A HttpTest::testEnd() 0 11 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Console;
11
use think\Event;
12
use think\event\HttpEnd;
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
21
class HttpTest extends TestCase
22
{
23
    /** @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...
24
    protected $app;
25
26
    /** @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...
27
    protected $http;
28
29
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
30
    {
31
        m::close();
32
    }
33
34
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
35
    {
36
        $this->app = m::mock(App::class)->makePartial();
37
38
        $this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial();
39
    }
40
41
    protected function prepareApp($request, $response)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function prepareApp()
Loading history...
42
    {
43
        $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

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

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

80
        $this->http->/** @scrutinizer ignore-call */ 
81
                     multi(false);

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...
81
82
        $this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
83
        $this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
84
85
        $request  = m::mock(Request::class)->makePartial();
86
        $response = m::mock(Response::class)->makePartial();
87
88
        $this->prepareApp($request, $response);
89
90
        $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

90
        $this->assertEquals($response, $this->http->run(/** @scrutinizer ignore-type */ $request));
Loading history...
91
    }
92
93
    public function multiAppRunProvider()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function multiAppRunProvider()
Loading history...
94
    {
95
        $request1 = m::mock(Request::class)->makePartial();
96
        $request1->shouldReceive('subDomain')->andReturn('www');
97
        $request1->shouldReceive('host')->andReturn('www.domain.com');
98
99
        $request2 = m::mock(Request::class)->makePartial();
100
        $request2->shouldReceive('subDomain')->andReturn('app2');
101
        $request2->shouldReceive('host')->andReturn('app2.domain.com');
102
103
        $request3 = m::mock(Request::class)->makePartial();
104
        $request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c');
105
106
        $request4 = m::mock(Request::class)->makePartial();
107
        $request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c');
108
109
        $request5 = m::mock(Request::class)->makePartial();
110
        $request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c');
111
112
        return [
113
            [$request1, true, 'app1'],
114
            [$request2, true, 'app2'],
115
            [$request3, true, 'app3'],
116
            [$request4, true, null],
117
            [$request5, true, 'some2', 'path'],
118
            [$request1, false, 'some3'],
119
        ];
120
    }
121
122
    public function testRunWithException()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testRunWithException()
Loading history...
123
    {
124
        $request  = m::mock(Request::class);
125
        $response = m::mock(Response::class);
126
127
        $this->app->shouldReceive('instance')->once()->with('request', $request);
128
129
        $exception = new Exception();
130
131
        $this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception);
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

131
        $this->http->/** @scrutinizer ignore-call */ 
132
                     shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception);

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...
132
133
        $handle = m::mock(Handle::class);
134
135
        $handle->shouldReceive('report')->once()->with($exception);
136
        $handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response);
137
138
        $this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle);
139
140
        $response->shouldReceive('setCookie')->andReturn($response);
141
142
        $this->assertEquals($response, $this->http->run($request));
0 ignored issues
show
Bug introduced by
$request of type Mockery\LegacyMockInterface|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

142
        $this->assertEquals($response, $this->http->run(/** @scrutinizer ignore-type */ $request));
Loading history...
143
    }
144
145
    public function testEnd()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testEnd()
Loading history...
146
    {
147
        $response = m::mock(Response::class);
148
        $event    = m::mock(Event::class);
149
        $event->shouldReceive('trigger')->once()->with(HttpEnd::class, $response);
150
        $this->app->shouldReceive('get')->once()->with('event')->andReturn($event);
151
        $log = m::mock(Log::class);
152
        $log->shouldReceive('save')->once();
153
        $this->app->shouldReceive('get')->once()->with('log')->andReturn($log);
154
155
        $this->http->end($response);
0 ignored issues
show
Bug introduced by
$response of type Mockery\LegacyMockInterface|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

155
        $this->http->end(/** @scrutinizer ignore-type */ $response);
Loading history...
156
    }
157
158
}
159