1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Http\Tests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Prophecy\Argument; |
8
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Shoot\Http\Middleware; |
13
|
|
|
use Shoot\Shoot\PipelineInterface; |
14
|
|
|
|
15
|
|
|
final class MiddlewareTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var RequestHandlerInterface */ |
18
|
|
|
private $handler; |
19
|
|
|
|
20
|
|
|
/** @var ServerRequestInterface */ |
21
|
|
|
private $request; |
22
|
|
|
|
23
|
|
|
/** @var ResponseInterface */ |
24
|
|
|
private $response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
protected function setUp() |
30
|
|
|
{ |
31
|
|
|
$this->request = $this->prophesize(ServerRequestInterface::class)->reveal(); |
32
|
|
|
$this->response = $this->prophesize(ResponseInterface::class)->reveal(); |
33
|
|
|
|
34
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
35
|
|
|
$handler |
36
|
|
|
->handle(Argument::type(ServerRequestInterface::class)) |
37
|
|
|
->willReturn($this->response); |
38
|
|
|
|
39
|
|
|
$this->handler = $handler->reveal(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function testHttpContextShouldBeSetOnPipeline() |
46
|
|
|
{ |
47
|
|
|
$pipelineProphecy = $this->prophesize(PipelineInterface::class); |
48
|
|
|
|
49
|
|
|
/** @var MethodProphecy $withContextMethod */ |
50
|
|
|
$withContextMethod = $pipelineProphecy |
51
|
|
|
->withContext(Argument::type(ServerRequestInterface::class), Argument::type('callable')) |
52
|
|
|
->willReturn($this->response); |
53
|
|
|
|
54
|
|
|
/** @var PipelineInterface $pipeline */ |
55
|
|
|
$pipeline = $pipelineProphecy->reveal(); |
56
|
|
|
|
57
|
|
|
$middleware = new Middleware($pipeline); |
58
|
|
|
$middleware->process($this->request, $this->handler); |
59
|
|
|
|
60
|
|
|
$withContextMethod->shouldHaveBeenCalled(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|