Completed
Push — 6.0 ( 9db11b...892b87 )
by yun
06:42
created

MiddlewareTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 Mockery as m;
6
use Mockery\MockInterface;
7
use PHPUnit\Framework\TestCase;
8
use think\App;
9
use think\Config;
10
use think\Container;
11
use think\Exception;
12
use think\exception\Handle;
13
use think\Middleware;
14
use think\Pipeline;
15
use think\Request;
16
use think\Response;
17
18
class MiddlewareTest extends TestCase
19
{
20
    /** @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...
21
    protected $app;
22
23
    /** @var Middleware|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 $middleware;
25
26
    /** @var Config|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 $config;
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
        Container::setInstance($this->app);
38
39
        $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
40
        $this->config = m::mock(Config::class)->makePartial();
41
        $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
42
        $this->app->shouldReceive('runningInConsole')->andReturn(false);
43
44
        $this->middleware = new Middleware($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\Middleware::__construct(). ( Ignorable by Annotation )

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

44
        $this->middleware = new Middleware(/** @scrutinizer ignore-type */ $this->app);
Loading history...
45
    }
46
47
    public function testSetMiddleware()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSetMiddleware()
Loading history...
48
    {
49
        $this->middleware->add('BarMiddleware', 'bar');
50
51
        $this->assertEquals(1, count($this->middleware->all('bar')));
52
53
        $this->middleware->controller('BarMiddleware');
54
        $this->assertEquals(1, count($this->middleware->all('controller')));
55
56
        $this->middleware->import(['FooMiddleware']);
57
        $this->assertEquals(1, count($this->middleware->all()));
58
59
        $this->middleware->unshift(['BazMiddleware', 'baz']);
60
        $this->assertEquals(2, count($this->middleware->all()));
61
        $this->assertEquals([['BazMiddleware', 'handle'], 'baz'], $this->middleware->all()[0]);
62
63
        $this->config->shouldReceive('get')->with('middleware.alias', [])->andReturn(['foo' => ['FooMiddleware', 'FarMiddleware']]);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on think\Config. ( Ignorable by Annotation )

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

63
        $this->config->/** @scrutinizer ignore-call */ 
64
                       shouldReceive('get')->with('middleware.alias', [])->andReturn(['foo' => ['FooMiddleware', 'FarMiddleware']]);

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...
64
65
        $this->middleware->add('foo');
66
        $this->assertEquals(4, count($this->middleware->all()));
67
        $this->middleware->add(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...
68
        });
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...
69
        $this->assertEquals(5, count($this->middleware->all()));
70
    }
71
72
    public function testPipelineAndEnd()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testPipelineAndEnd()
Loading history...
73
    {
74
        $bar = m::mock("overload:BarMiddleware");
75
        $foo = m::mock("overload:FooMiddleware", Foo::class);
76
77
        $request  = m::mock(Request::class);
78
        $response = m::mock(Response::class);
79
80
        $e = new Exception();
81
82
        $handle = m::mock(Handle::class);
83
        $handle->shouldReceive('report')->with($e)->andReturnNull();
0 ignored issues
show
Bug introduced by
The method andReturnNull() 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

83
        $handle->shouldReceive('report')->with($e)->/** @scrutinizer ignore-call */ andReturnNull();

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...
84
        $handle->shouldReceive('render')->with($request, $e)->andReturn($response);
85
86
        $foo->shouldReceive('handle')->once()->andReturnUsing(function ($request, $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

86
        $foo->shouldReceive('handle')->once()->/** @scrutinizer ignore-call */ andReturnUsing(function ($request, $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...
87
            return $next($request);
88
        });
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...
89
        $bar->shouldReceive('handle')->once()->andReturnUsing(function ($request, $next) use ($e) {
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...
90
            $next($request);
91
            throw  $e;
92
        });
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...
93
94
        $foo->shouldReceive('end')->once()->with($response)->andReturnNull();
95
96
        $this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle);
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

96
        $this->app->/** @scrutinizer ignore-call */ 
97
                    shouldReceive('make')->with(Handle::class)->andReturn($handle);

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...
97
98
        $this->config->shouldReceive('get')->once()->with('middleware.priority', [])->andReturn(['FooMiddleware', 'BarMiddleware']);
99
100
        $this->middleware->import([function ($request, $next) {
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...
101
            return $next($request);
102
        }, 'BarMiddleware', 'FooMiddleware']);
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
        $this->assertInstanceOf(Pipeline::class, $pipeline = $this->middleware->pipeline());
105
106
        $pipeline->send($request)->then(function ($request) use ($e, $response) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

106
        $pipeline->send($request)->then(function (/** @scrutinizer ignore-unused */ $request) use ($e, $response) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The import $response is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

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...
107
            throw $e;
108
        });
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...
109
110
        $this->middleware->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\Middleware::end(). ( Ignorable by Annotation )

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

110
        $this->middleware->end(/** @scrutinizer ignore-type */ $response);
Loading history...
111
    }
112
}
113
114
class Foo
115
{
116
    public function end(Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

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

116
    public function end(/** @scrutinizer ignore-unused */ Response $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing doc comment for function end()
Loading history...
117
    {
118
    }
119
}
120