1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests; |
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
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function testShouldCallMiddleware() |
32
|
|
|
{ |
33
|
|
|
$view = ViewFactory::create(); |
34
|
|
|
|
35
|
|
|
$middleware = $this->createMock(MiddlewareInterface::class); |
36
|
|
|
$middleware |
37
|
|
|
->expects($this->once()) |
38
|
|
|
->method('process') |
39
|
|
|
->willReturn($view); |
40
|
|
|
|
41
|
|
|
$pipeline = new Pipeline([$middleware]); |
42
|
|
|
|
43
|
|
|
$pipeline->withRequest($this->request, function () use ($pipeline, $view) { |
44
|
|
|
$pipeline->process($view); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function testShouldRenderView() |
52
|
|
|
{ |
53
|
|
|
$pipeline = new Pipeline(); |
54
|
|
|
|
55
|
|
|
/** @var callable|MockObject $callback */ |
56
|
|
|
$callback = $this |
57
|
|
|
->getMockBuilder(stdClass::class) |
58
|
|
|
->setMethods(['__invoke']) |
59
|
|
|
->getMock(); |
60
|
|
|
|
61
|
|
|
$callback |
62
|
|
|
->expects($this->once()) |
63
|
|
|
->method('__invoke'); |
64
|
|
|
|
65
|
|
|
$view = ViewFactory::createWithCallback($callback); |
66
|
|
|
|
67
|
|
|
$pipeline->withRequest($this->request, function () use ($pipeline, $view) { |
68
|
|
|
$pipeline->process($view); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function testShouldThrowIfNoRequestWasSet() |
76
|
|
|
{ |
77
|
|
|
$pipeline = new Pipeline(); |
78
|
|
|
$view = ViewFactory::create(); |
79
|
|
|
|
80
|
|
|
$this->expectException(MissingRequestException::class); |
81
|
|
|
|
82
|
|
|
$pipeline->process($view); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|