Passed
Push — master ( 882e9c...b89417 )
by Felipe
01:54
created

testProcessReturnResponseFromDependencyHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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
use CoiSA\Http\Middleware\EchoBodyMiddleware;
12
use CoiSA\Http\Middleware\RequestHandlerMiddleware;
13
use PHPUnit\Framework\TestCase;
14
use Prophecy\Prophecy\ObjectProphecy;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
20
/**
21
 * Class RequestHandlerMiddlewareTest
22
 */
23
final class RequestHandlerMiddlewareTest extends TestCase
24
{
25
    /** @var EchoBodyMiddleware */
26
    private $middleware;
27
28
    /** @var ObjectProphecy|RequestHandlerInterface */
29
    private $requestHandler;
30
31
    /** @var ObjectProphecy|RequestHandlerInterface */
32
    private $next;
33
34
    /** @var ObjectProphecy|ServerRequestInterface */
35
    private $serverRequest;
36
37
    /** @var ObjectProphecy|ResponseInterface */
38
    private $response;
39
40
    /** @var array */
41
    private $execution = [];
42
43
    public function setUp(): void
44
    {
45
        $this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
46
        $this->next           = $this->prophesize(RequestHandlerInterface::class);
47
        $this->serverRequest  = $this->prophesize(ServerRequestInterface::class);
48
        $this->response       = $this->prophesize(ResponseInterface::class);
49
50
        $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...
51
52
        $testClass = $this;
53
        $callback = function () use ($testClass) {
54
            $testClass->execution[] = spl_object_hash($this);
55
            return $testClass->response->reveal();
56
        };
57
58
        $this->requestHandler->handle($this->serverRequest->reveal())->will($callback);
59
        $this->next->handle($this->serverRequest->reveal())->will($callback);
60
    }
61
62
    public function testImplementPsrInterfaces(): void
63
    {
64
        $this->assertInstanceOf(RequestHandlerInterface::class, $this->middleware);
65
        $this->assertInstanceOf(MiddlewareInterface::class, $this->middleware);
66
    }
67
68
    public function testHandleReturnHandlerResponse()
69
    {
70
        $serverRequest = $this->serverRequest->reveal();
71
        $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

71
        /** @scrutinizer ignore-call */ 
72
        $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...
72
        $requestHandler = $this->requestHandler->reveal();
73
        $this->assertEquals($requestHandler->handle($serverRequest), $response);
74
    }
75
76
    public function testProcessExecuteBothHandlersInOrder()
77
    {
78
        $expected = [
79
            spl_object_hash($this->requestHandler),
80
            spl_object_hash($this->next),
81
        ];
82
        $this->middleware->process($this->serverRequest->reveal(), $this->next->reveal());
83
        $this->assertEquals($expected, $this->execution);
84
    }
85
86
    public function testProcessReturnResponseFromDependencyHandler()
87
    {
88
        $serverRequest = $this->serverRequest->reveal();
89
        $response = $this->middleware->process($serverRequest, $this->next->reveal());
90
        $requestHandler = $this->requestHandler->reveal();
91
        $this->assertEquals($requestHandler->handle($serverRequest), $response);
92
    }
93
}
94