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
|
|
View Code Duplication |
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( |
|
|
|
|
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
|
|
View Code Duplication |
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( |
|
|
|
|
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.