|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Tests; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Shoot\Shoot\MissingRequestException; |
|
9
|
|
|
use Shoot\Shoot\Pipeline; |
|
10
|
|
|
use Shoot\Shoot\Tests\Fixtures\Middleware; |
|
11
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
|
12
|
|
|
|
|
13
|
|
|
final class PipelineTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ServerRequestInterface */ |
|
16
|
|
|
private $request; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->request = $this->prophesize(ServerRequestInterface::class)->reveal(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testProcessShouldCallMiddleware() |
|
30
|
|
|
{ |
|
31
|
|
|
$wasCalled = false; |
|
32
|
|
|
|
|
33
|
|
|
$pipeline = new Pipeline([ |
|
34
|
|
|
new Middleware(function () use (&$wasCalled) { |
|
35
|
|
|
$wasCalled = true; |
|
36
|
|
|
}), |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
$view = ViewFactory::create(); |
|
40
|
|
|
|
|
41
|
|
|
$pipeline->withRequest($this->request, function () use ($pipeline, $view) { |
|
42
|
|
|
$pipeline->process($view); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertTrue($wasCalled); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testProcessShouldRenderView() |
|
52
|
|
|
{ |
|
53
|
|
|
$wasCalled = false; |
|
54
|
|
|
|
|
55
|
|
|
$pipeline = new Pipeline(); |
|
56
|
|
|
|
|
57
|
|
|
$view = ViewFactory::create(null, function () use (&$wasCalled) { |
|
58
|
|
|
$wasCalled = true; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$pipeline->withRequest($this->request, function () use ($pipeline, $view) { |
|
62
|
|
|
$pipeline->process($view); |
|
63
|
|
|
}); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertTrue($wasCalled); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testProcessShouldThrowIfNoRequestWasSet() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->expectException(MissingRequestException::class); |
|
74
|
|
|
|
|
75
|
|
|
$pipeline = new Pipeline(); |
|
76
|
|
|
|
|
77
|
|
|
$view = ViewFactory::create(); |
|
78
|
|
|
|
|
79
|
|
|
$pipeline->process($view); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|