Passed
Push — master ( e0d985...5866b4 )
by Woody
01:27
created

BrokerTest::testCannotHandleWithoutMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Broker;
5
6
use Eloquent\Phony\Mock\Handle\InstanceHandle;
7
use Eloquent\Phony\Phpunit\Phony;
8
use OutOfBoundsException;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\MiddlewareInterface;
13
use Psr\Http\Server\RequestHandlerInterface;
14
15
class BrokerTest extends TestCase
16
{
17
    /** @var Broker */
18
    private $broker;
19
20
    public function setUp(): void
21
    {
22
        $this->broker = new Broker();
23
    }
24
25
    public function testCanAppendMiddleware(): void
26
    {
27
        // Mock
28
        $mw1 = $this->mockMiddleware();
29
        $mw2 = $this->mockMiddleware();
30
31
        $this->broker->append($mw1->get(), $mw2->get());
32
33
        // Execute
34
        $response = $this->broker->process(
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
35
            $this->mockRequest()->get(),
36
            $this->mockHandler()->get()
37
        );
38
39
        // Verify
40
        Phony::inOrder(
41
            $mw1->process->once()->called(),
42
            $mw2->process->once()->called()
43
        );
44
    }
45
46
    public function testCanPrependMiddleware(): void
47
    {
48
        // Mock
49
        $mw1 = $this->mockMiddleware();
50
        $mw2 = $this->mockMiddleware();
51
52
        $this->broker->append($mw1->get());
53
        $this->broker->prepend($mw2->get());
54
55
        // Execute
56
        $response = $this->broker->process(
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
57
            $this->mockRequest()->get(),
58
            $this->mockHandler()->get()
59
        );
60
61
        // Verify
62
        Phony::inOrder(
63
            $mw2->process->once()->called(),
64
            $mw1->process->once()->called()
65
        );
66
    }
67
68
    public function testCannotHandleWithoutMiddleware(): void
69
    {
70
        // Expect
71
        $this->expectException(OutOfBoundsException::class);
72
73
        // Execute
74
        $this->broker->handle(
75
            $this->mockRequest()->get()
76
        );
77
    }
78
79
    private function mockHandler(): InstanceHandle
80
    {
81
        $handler = Phony::mock(RequestHandlerInterface::class);
82
        $handler->handle->returns($this->mockResponse());
83
84
        return $handler;
85
    }
86
87
    private function mockMiddleware(): InstanceHandle
88
    {
89
        $middleware = Phony::mock(MiddlewareInterface::class);
90
91
        $middleware->process->does(function ($request, $handler) {
92
            return $handler->handle($request);
93
        });
94
95
        return $middleware;
96
    }
97
98
    private function mockRequest(): InstanceHandle
99
    {
100
        return Phony::mock(ServerRequestInterface::class);
101
    }
102
103
    private function mockResponse(): InstanceHandle
104
    {
105
        return Phony::mock(ResponseInterface::class);
106
    }
107
}
108