Passed
Push — master ( cdb9d8...eff708 )
by Felipe
02:04
created

ErrorHandlerMiddlewareTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Middleware;
12
13
use CoiSA\Http\Middleware\EchoBodyMiddleware;
14
use CoiSA\Http\Middleware\ErrorHandlerMiddleware;
15
use PHPUnit\Framework\TestCase;
16
use Prophecy\Argument;
17
use Prophecy\Prophecy\ObjectProphecy;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
/**
23
 * Class ErrorHandlerMiddlewareTest
24
 *
25
 * @package CoiSA\Http\Test\Middleware
26
 */
27
final class ErrorHandlerMiddlewareTest extends TestCase
28
{
29
    /** @var EchoBodyMiddleware */
30
    private $middleware;
31
32
    /** @var ObjectProphecy|RequestHandlerInterface */
33
    private $requestHandler;
34
35
    /** @var ObjectProphecy|ServerRequestInterface */
36
    private $serverRequest;
37
38
    /** @var ObjectProphecy|ResponseInterface */
39
    private $response;
40
41
    public function setUp(): void
42
    {
43
        $this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
44
        $this->serverRequest  = $this->prophesize(ServerRequestInterface::class);
45
        $this->response       = $this->prophesize(ResponseInterface::class);
46
        $this->middleware     = new ErrorHandlerMiddleware($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\ErrorHandlerMiddleware 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...
47
48
        $this->requestHandler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']);
49
        $this->serverRequest->withAttribute(ErrorHandlerMiddleware::class, Argument::type(\Throwable::class))->will([$this->serverRequest, 'reveal']);
50
    }
51
52
    public function testSuccessExecutionReturnHandlerResponse()
53
    {
54
        $handler = $this->prophesize(RequestHandlerInterface::class);
55
        $handlerResponse = $this->prophesize(ResponseInterface::class);
56
        $handler->handle($this->serverRequest->reveal())->will([$handlerResponse, 'reveal']);
57
58
        $response = $this->middleware->process($this->serverRequest->reveal(), $handler->reveal());
59
        $this->assertSame($handlerResponse->reveal(), $response);
60
        $this->assertNotSame($this->response->reveal(), $response);
61
    }
62
63
    public function testOnExceptionReturnErrorHandlerResponse()
64
    {
65
        $handler = $this->prophesize(RequestHandlerInterface::class);
66
        $handlerResponse = $this->prophesize(ResponseInterface::class);
67
        $handler->handle($this->serverRequest->reveal())->willThrow(\Exception::class);
68
69
        $response = $this->middleware->process($this->serverRequest->reveal(), $handler->reveal());
70
        $this->assertSame($this->response->reveal(), $response);
71
        $this->assertNotSame($handlerResponse->reveal(), $response);
72
    }
73
}
74