Passed
Pull Request — master (#2)
by Victor
02:09
created

testExtensionShouldDelegateProcessingToPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
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\MiddlewareInterface;
10
use Shoot\Shoot\Pipeline;
11
use Shoot\Shoot\PresentationModel;
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
    /**
19
     * @return void
20
     */
21
    public function testExtensionShouldDelegateProcessingToPipeline()
22
    {
23
        $request = $this->createMock(ServerRequestInterface::class);
24
        $view = ViewFactory::create();
25
26
        $middleware = $this->createMock(MiddlewareInterface::class);
27
        $middleware
28
            ->expects($this->once())
29
            ->method('process')
30
            ->with($this->equalTo($view))
31
            ->willReturn($view);
32
33
        $pipeline = new Pipeline([$middleware]);
34
        $extension = new Extension($pipeline);
35
36
        $pipeline->withRequest($request, function () use ($extension, $view) {
37
            $extension->process($view);
38
        });
39
    }
40
41
    /**
42
     * @return void
43
     */
44
    public function testExtensionShouldIncludeVariablesFilter()
45
    {
46
        $extension = new Extension(new Pipeline());
47
        $presentationModel = new PresentationModel(['variable' => 'value']);
48
49
        /** @var TwigFilter[] $filters */
50
        $filters = array_filter($extension->getFilters(), function (TwigFilter $filter): bool {
51
            return $filter->getName() === 'variables';
52
        });
53
54
        $this->assertCount(1, $filters);
55
56
        list($filter) = $filters;
57
        $callback = $filter->getCallable();
58
59
        /** @var mixed[] $variables */
60
        $variables = $callback($presentationModel);
61
62
        $this->assertInternalType('array', $variables);
63
        $this->assertCount(1, $variables);
64
        $this->assertArrayHasKey('variable', $variables);
65
    }
66
67
    /**
68
     * @return void
69
     */
70
    public function testExtensionShouldIncludeModelTest()
71
    {
72
        $extension = new Extension(new Pipeline());
73
        $presentationModel = new PresentationModel();
74
75
        /** @var TwigTest[] $tests */
76
        $tests = array_filter($extension->getTests(), function (TwigTest $test): bool {
77
            return $test->getName() === 'model';
78
        });
79
80
        $this->assertCount(1, $tests);
81
82
        list($test) = $tests;
83
        $callback = $test->getCallable();
84
85
        $this->assertTrue($callback($presentationModel));
86
    }
87
}
88