|
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\Extension; |
|
9
|
|
|
use Shoot\Shoot\Pipeline; |
|
10
|
|
|
use Shoot\Shoot\PresentationModel; |
|
11
|
|
|
use Shoot\Shoot\Tests\Fixtures\Middleware; |
|
12
|
|
|
use Shoot\Shoot\Tests\Fixtures\ViewFactory; |
|
13
|
|
|
use Twig_Filter as TwigFilter; |
|
14
|
|
|
use Twig_Test as TwigTest; |
|
15
|
|
|
|
|
16
|
|
|
final class ExtensionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ServerRequestInterface */ |
|
19
|
|
|
private $request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->request = $this->prophesize(ServerRequestInterface::class)->reveal(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testExtensionShouldDelegateProcessingToPipeline() |
|
30
|
|
|
{ |
|
31
|
|
|
$wasCalled = false; |
|
32
|
|
|
|
|
33
|
|
|
$pipeline = new Pipeline([ |
|
34
|
|
|
new Middleware(function () use (&$wasCalled) { |
|
35
|
|
|
$wasCalled = true; |
|
36
|
|
|
}), |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
$extension = new Extension($pipeline); |
|
40
|
|
|
|
|
41
|
|
|
$view = ViewFactory::create(); |
|
42
|
|
|
|
|
43
|
|
|
$pipeline->withRequest($this->request, function () use ($extension, $view) { |
|
44
|
|
|
$extension->process($view); |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertTrue($wasCalled); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testExtensionShouldIncludeVariablesFilter() |
|
54
|
|
|
{ |
|
55
|
|
|
$extension = new Extension(new Pipeline()); |
|
56
|
|
|
$presentationModel = new PresentationModel(['variable' => 'value']); |
|
57
|
|
|
|
|
58
|
|
|
/** @var TwigFilter[] $filters */ |
|
59
|
|
|
$filters = array_filter($extension->getFilters(), function (TwigFilter $filter): bool { |
|
60
|
|
|
return $filter->getName() === 'variables'; |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertCount(1, $filters); |
|
64
|
|
|
|
|
65
|
|
|
$filter = array_shift($filters); |
|
66
|
|
|
$callback = $filter->getCallable(); |
|
67
|
|
|
|
|
68
|
|
|
/** @var mixed[] $variables */ |
|
69
|
|
|
$variables = $callback($presentationModel); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertInternalType('array', $variables); |
|
72
|
|
|
$this->assertCount(1, $variables); |
|
73
|
|
|
$this->assertArrayHasKey('variable', $variables); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testExtensionShouldIncludeModelTest() |
|
80
|
|
|
{ |
|
81
|
|
|
$extension = new Extension(new Pipeline()); |
|
82
|
|
|
$presentationModel = new PresentationModel(); |
|
83
|
|
|
|
|
84
|
|
|
/** @var TwigTest[] $tests */ |
|
85
|
|
|
$tests = array_filter($extension->getTests(), function (TwigTest $test): bool { |
|
86
|
|
|
return $test->getName() === 'model'; |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertCount(1, $tests); |
|
90
|
|
|
|
|
91
|
|
|
$test = array_shift($tests); |
|
92
|
|
|
$callback = $test->getCallable(); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertTrue($callback($presentationModel)); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|