Test Failed
Push — master ( b89417...fc8a73 )
by Felipe
01:53
created

RequestHandlerMiddlewareTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 33
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testProcessReturnResponseFromDependencyHandler() 0 6 1
A testProcessExecuteBothHandlersInOrder() 0 8 1
A testImplementPsrInterfaces() 0 4 1
A testHandleReturnHandlerResponse() 0 6 1
A setUp() 0 18 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of coisa/http.
4
 *
5
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace CoiSA\Http\Test;
12
13
use CoiSA\Http\Middleware\EchoBodyMiddleware;
14
use CoiSA\Http\Middleware\RequestHandlerMiddleware;
15
use PHPUnit\Framework\TestCase;
16
use Prophecy\Prophecy\ObjectProphecy;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
/**
23
 * Class RequestHandlerMiddlewareTest
24
 *
25
 * @package CoiSA\Http\Test
26
 */
27
final class RequestHandlerMiddlewareTest extends TestCase
28
{
29
    /** @var EchoBodyMiddleware */
30
    private $middleware;
31
32
    /** @var ObjectProphecy|RequestHandlerInterface */
33
    private $requestHandler;
34
35
    /** @var ObjectProphecy|RequestHandlerInterface */
36
    private $next;
37
38
    /** @var ObjectProphecy|ServerRequestInterface */
39
    private $serverRequest;
40
41
    /** @var ObjectProphecy|ResponseInterface */
42
    private $response;
43
44
    /** @var array */
45
    private $execution = [];
46
47
    public function setUp(): void
48
    {
49
        $this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
50
        $this->next           = $this->prophesize(RequestHandlerInterface::class);
51
        $this->serverRequest  = $this->prophesize(ServerRequestInterface::class);
52
        $this->response       = $this->prophesize(ResponseInterface::class);
53
54
        $this->middleware     = new RequestHandlerMiddleware($this->requestHandler->reveal());
0 ignored issues
show
Documentation Bug introduced by
It seems like new CoiSA\Http\Middlewar...questHandler->reveal()) of type CoiSA\Http\Middleware\RequestHandlerMiddleware is incompatible with the declared type CoiSA\Http\Middleware\EchoBodyMiddleware of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
56
        $testClass = $this;
57
        $callback  = function () use ($testClass) {
58
            $testClass->execution[] = \spl_object_hash($this);
59
60
            return $testClass->response->reveal();
61
        };
62
63
        $this->requestHandler->handle($this->serverRequest->reveal())->will($callback);
64
        $this->next->handle($this->serverRequest->reveal())->will($callback);
65
    }
66
67
    public function testImplementPsrInterfaces(): void
68
    {
69
        $this->assertInstanceOf(RequestHandlerInterface::class, $this->middleware);
70
        $this->assertInstanceOf(MiddlewareInterface::class, $this->middleware);
71
    }
72
73
    public function testHandleReturnHandlerResponse(): void
74
    {
75
        $serverRequest  = $this->serverRequest->reveal();
76
        $response       = $this->middleware->handle($serverRequest);
0 ignored issues
show
Bug introduced by
The method handle() does not exist on CoiSA\Http\Middleware\EchoBodyMiddleware. ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        $response       = $this->middleware->handle($serverRequest);

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...
77
        $requestHandler = $this->requestHandler->reveal();
78
        $this->assertEquals($requestHandler->handle($serverRequest), $response);
79
    }
80
81
    public function testProcessExecuteBothHandlersInOrder(): void
82
    {
83
        $expected = [
84
            \spl_object_hash($this->requestHandler),
85
            \spl_object_hash($this->next),
86
        ];
87
        $this->middleware->process($this->serverRequest->reveal(), $this->next->reveal());
88
        $this->assertEquals($expected, $this->execution);
89
    }
90
91
    public function testProcessReturnResponseFromDependencyHandler(): void
92
    {
93
        $serverRequest  = $this->serverRequest->reveal();
94
        $response       = $this->middleware->process($serverRequest, $this->next->reveal());
95
        $requestHandler = $this->requestHandler->reveal();
96
        $this->assertEquals($requestHandler->handle($serverRequest), $response);
97
    }
98
}
99