1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests\Unit; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Shoot\Shoot\MiddlewareInterface; |
10
|
|
|
use Shoot\Shoot\MissingRequestException; |
11
|
|
|
use Shoot\Shoot\Pipeline; |
12
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
13
|
|
|
use stdClass; |
14
|
|
|
|
15
|
|
|
final class PipelineTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var ServerRequestInterface|MockObject */ |
18
|
|
|
private $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->request = $this->createMock(ServerRequestInterface::class); |
26
|
|
|
|
27
|
|
|
parent::setUp(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function testShouldCallMiddleware() |
34
|
|
|
{ |
35
|
|
|
$view = ViewFactory::create(); |
36
|
|
|
|
37
|
|
|
$middleware = $this->createMock(MiddlewareInterface::class); |
38
|
|
|
$middleware |
39
|
|
|
->expects($this->once()) |
40
|
|
|
->method('process') |
41
|
|
|
->willReturn($view); |
42
|
|
|
|
43
|
|
|
$pipeline = new Pipeline([$middleware]); |
44
|
|
|
|
45
|
|
|
$pipeline->withRequest($this->request, function () use ($pipeline, $view) { |
46
|
|
|
$pipeline->process($view); |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function testShouldRenderView() |
54
|
|
|
{ |
55
|
|
|
$pipeline = new Pipeline(); |
56
|
|
|
|
57
|
|
|
/** @var callable|MockObject $callback */ |
58
|
|
|
$callback = $this |
59
|
|
|
->getMockBuilder(stdClass::class) |
60
|
|
|
->setMethods(['__invoke']) |
61
|
|
|
->getMock(); |
62
|
|
|
|
63
|
|
|
$callback |
64
|
|
|
->expects($this->once()) |
65
|
|
|
->method('__invoke'); |
66
|
|
|
|
67
|
|
|
$view = ViewFactory::createWithCallback($callback); |
68
|
|
|
|
69
|
|
|
$pipeline->withRequest($this->request, function () use ($pipeline, $view) { |
70
|
|
|
$pipeline->process($view); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function testShouldThrowIfNoRequestWasSet() |
78
|
|
|
{ |
79
|
|
|
$pipeline = new Pipeline(); |
80
|
|
|
$view = ViewFactory::create(); |
81
|
|
|
|
82
|
|
|
$this->expectException(MissingRequestException::class); |
83
|
|
|
|
84
|
|
|
$pipeline->process($view); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|