Completed
Branch 6.0 (d30585)
by yun
06:27
created

FooClass::bar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
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 Closure;
6
use Mockery as m;
7
use Mockery\MockInterface;
8
use PHPUnit\Framework\TestCase;
9
use think\helper\Str;
10
use think\Request;
11
use think\Route;
12
13
class RouteTest extends TestCase
14
{
15
    use InteractsWithApp;
16
17
    /** @var Route|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...
18
    protected $route;
19
20
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
21
    {
22
        m::close();
23
    }
24
25
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
26
    {
27
        $this->prepareApp();
28
        $this->route = new Route($this->app);
0 ignored issues
show
Bug introduced by
$this->app of type Mockery\Mock is incompatible with the type think\App expected by parameter $app of think\Route::__construct(). ( Ignorable by Annotation )

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

28
        $this->route = new Route(/** @scrutinizer ignore-type */ $this->app);
Loading history...
29
    }
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
     * @param $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
33
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
34
     * @param string $host
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
35
     * @return m\Mock|Request
36
     */
37
    protected function makeRequest($path, $method = 'GET', $host = 'localhost')
38
    {
39
        $request = m::mock(Request::class)->makePartial();
40
        $request->shouldReceive('host')->andReturn($host);
41
        $request->shouldReceive('pathinfo')->andReturn($path);
42
        $request->shouldReceive('url')->andReturn('/' . $path);
43
        $request->shouldReceive('subDomain')->andReturn('');
44
        $request->shouldReceive('panDomain')->andReturn('');
45
        $request->shouldReceive('method')->andReturn(strtoupper($method));
46
        return $request;
47
    }
48
49
    public function testSimpleRequest()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSimpleRequest()
Loading history...
50
    {
51
        $this->route->get('foo', function () {
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
            return 'get-foo';
53
        });
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
        $this->route->put('foo', function () {
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...
56
            return 'put-foo';
57
        });
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...
58
59
        $this->route->group(function () {
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...
60
            $this->route->post('foo', function () {
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...
61
                return 'post-foo';
62
            });
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...
63
        });
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...
64
65
        $request  = $this->makeRequest('foo', 'post');
66
        $response = $this->route->dispatch($request);
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type think\Request expected by parameter $request of think\Route::dispatch(). ( Ignorable by Annotation )

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

66
        $response = $this->route->dispatch(/** @scrutinizer ignore-type */ $request);
Loading history...
67
        $this->assertEquals(200, $response->getCode());
68
        $this->assertEquals('post-foo', $response->getContent());
69
70
        $request  = $this->makeRequest('foo', 'get');
71
        $response = $this->route->dispatch($request);
72
        $this->assertEquals(200, $response->getCode());
73
        $this->assertEquals('get-foo', $response->getContent());
74
    }
75
76
    public function testOptionsRequest()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testOptionsRequest()
Loading history...
77
    {
78
        $this->route->get('foo', function () {
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...
79
            return 'get-foo';
80
        });
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...
81
82
        $this->route->put('foo', function () {
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...
83
            return 'put-foo';
84
        });
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...
85
86
        $this->route->group(function () {
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...
87
            $this->route->post('foo', function () {
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...
88
                return 'post-foo';
89
            });
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...
90
        });
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...
91
92
        $this->route->resource('bar', 'SomeClass');
93
94
        $request  = $this->makeRequest('foo', 'options');
95
        $response = $this->route->dispatch($request);
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type think\Request expected by parameter $request of think\Route::dispatch(). ( Ignorable by Annotation )

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

95
        $response = $this->route->dispatch(/** @scrutinizer ignore-type */ $request);
Loading history...
96
        $this->assertEquals(204, $response->getCode());
97
        $this->assertEquals('GET, PUT, POST', $response->getHeader('Allow'));
98
99
        $request  = $this->makeRequest('bar', 'options');
100
        $response = $this->route->dispatch($request);
101
        $this->assertEquals(204, $response->getCode());
102
        $this->assertEquals('GET, POST', $response->getHeader('Allow'));
103
104
        $request  = $this->makeRequest('bar/1', 'options');
105
        $response = $this->route->dispatch($request);
106
        $this->assertEquals(204, $response->getCode());
107
        $this->assertEquals('GET, PUT, DELETE', $response->getHeader('Allow'));
108
    }
109
110
    public function testAllowCrossDomain()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testAllowCrossDomain()
Loading history...
111
    {
112
        $this->route->get('foo', function () {
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...
113
            return 'get-foo';
114
        })->allowCrossDomain(['some' => 'bar']);
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...
115
116
        $request  = $this->makeRequest('foo', 'get');
117
        $response = $this->route->dispatch($request);
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type think\Request expected by parameter $request of think\Route::dispatch(). ( Ignorable by Annotation )

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

117
        $response = $this->route->dispatch(/** @scrutinizer ignore-type */ $request);
Loading history...
118
119
        $this->assertEquals('bar', $response->getHeader('some'));
120
        $this->assertArrayHasKey('Access-Control-Allow-Credentials', $response->getHeader());
121
122
        $request  = $this->makeRequest('foo', 'options');
123
        $response = $this->route->dispatch($request);
124
125
        $this->assertEquals(204, $response->getCode());
126
        $this->assertArrayHasKey('Access-Control-Allow-Credentials', $response->getHeader());
127
        $this->assertEquals('GET', $response->getHeader('Allow'));
128
    }
129
130
    public function testControllerDispatch()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testControllerDispatch()
Loading history...
131
    {
132
        $this->route->get('foo', 'foo/bar');
133
134
        $controller = m::Mock(\stdClass::class);
135
136
        $this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
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

136
        $this->app->/** @scrutinizer ignore-call */ 
137
                    shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_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...
137
        $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
138
139
        $controller->shouldReceive('bar')->andReturn('bar');
140
141
        $request  = $this->makeRequest('foo');
142
        $response = $this->route->dispatch($request);
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type think\Request expected by parameter $request of think\Route::dispatch(). ( Ignorable by Annotation )

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

142
        $response = $this->route->dispatch(/** @scrutinizer ignore-type */ $request);
Loading history...
143
        $this->assertEquals('bar', $response->getContent());
144
    }
145
146
    public function testEmptyControllerDispatch()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testEmptyControllerDispatch()
Loading history...
147
    {
148
        $this->route->get('foo', 'foo/bar');
149
150
        $controller = m::Mock(\stdClass::class);
151
152
        $this->app->shouldReceive('parseClass')->with('controller', 'Error')->andReturn($controller->mockery_getName());
153
        $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
154
155
        $controller->shouldReceive('bar')->andReturn('bar');
156
157
        $request  = $this->makeRequest('foo');
158
        $response = $this->route->dispatch($request);
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type think\Request expected by parameter $request of think\Route::dispatch(). ( Ignorable by Annotation )

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

158
        $response = $this->route->dispatch(/** @scrutinizer ignore-type */ $request);
Loading history...
159
        $this->assertEquals('bar', $response->getContent());
160
    }
161
162
    protected function createMiddleware($times = 1)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createMiddleware()
Loading history...
163
    {
164
        $middleware = m::mock(Str::random(5));
165
        $middleware->shouldReceive('handle')->times($times)->andReturnUsing(function ($request, Closure $next) {
0 ignored issues
show
Bug introduced by
The method andReturnUsing() 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

165
        $middleware->shouldReceive('handle')->times($times)->/** @scrutinizer ignore-call */ andReturnUsing(function ($request, Closure $next) {

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...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
166
            return $next($request);
167
        });
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...
168
        $this->app->shouldReceive('make')->with($middleware->mockery_getName())->andReturn($middleware);
169
170
        return $middleware;
171
    }
172
173
    public function testControllerWithMiddleware()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testControllerWithMiddleware()
Loading history...
174
    {
175
        $this->route->get('foo', 'foo/bar');
176
177
        $controller = m::mock(FooClass::class);
178
179
        $controller->middleware = [
0 ignored issues
show
Bug introduced by
Accessing middleware on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing middleware on the interface Mockery\LegacyMockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
180
            $this->createMiddleware()->mockery_getName() . ":params1:params2",
181
            $this->createMiddleware(0)->mockery_getName() => ['except' => 'bar'],
182
            $this->createMiddleware()->mockery_getName()  => ['only' => 'bar'],
183
        ];
184
185
        $this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
186
        $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
187
188
        $controller->shouldReceive('bar')->once()->andReturn('bar');
189
190
        $request  = $this->makeRequest('foo');
191
        $response = $this->route->dispatch($request);
0 ignored issues
show
Bug introduced by
$request of type Mockery\Mock is incompatible with the type think\Request expected by parameter $request of think\Route::dispatch(). ( Ignorable by Annotation )

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

191
        $response = $this->route->dispatch(/** @scrutinizer ignore-type */ $request);
Loading history...
192
        $this->assertEquals('bar', $response->getContent());
193
    }
194
}
195
196
class FooClass
197
{
198
    public $middleware = [];
199
200
    public function bar()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bar()
Loading history...
201
    {
202
203
    }
204
}
205