ExtensionTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 32
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testExtensionShouldDelegateProcessingToPipeline() 0 18 1
A testExtensionShouldIncludeVariablesFilter() 0 21 1
A testExtensionShouldIncludeModelTest() 0 16 1
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\Extension;
10
use Shoot\Shoot\MiddlewareInterface;
11
use Shoot\Shoot\Pipeline;
12
use Shoot\Shoot\PresentationModel;
13
use Shoot\Shoot\Tests\Fixtures\ViewFactory;
14
use Twig\TwigFilter;
15
use Twig\TwigTest;
16
17
final class ExtensionTest extends TestCase
18
{
19
    public function testExtensionShouldDelegateProcessingToPipeline(): void
20
    {
21
        /** @var ServerRequestInterface|MockObject $request */
22
        $request = $this->createMock(ServerRequestInterface::class);
23
        $view = ViewFactory::create();
24
25
        $middleware = $this->createMock(MiddlewareInterface::class);
26
        $middleware
27
            ->expects($this->once())
28
            ->method('process')
29
            ->with($this->equalTo($view))
30
            ->willReturn($view);
31
32
        $pipeline = new Pipeline([$middleware]);
33
        $extension = new Extension($pipeline);
34
35
        $pipeline->withRequest($request, function () use ($extension, $view) {
36
            $extension->process($view);
37
        });
38
    }
39
40
    public function testExtensionShouldIncludeVariablesFilter(): void
41
    {
42
        $extension = new Extension(new Pipeline());
43
        $presentationModel = new PresentationModel(['variable' => 'variable']);
44
45
        /** @var TwigFilter[] $filters */
46
        $filters = array_filter($extension->getFilters(), function (TwigFilter $filter): bool {
47
            return $filter->getName() === 'variables';
48
        });
49
50
        $this->assertCount(1, $filters);
51
52
        list($filter) = $filters;
53
        $callback = $filter->getCallable();
54
55
        /** @var mixed[] $variables */
56
        $variables = $callback($presentationModel);
57
58
        $this->assertIsArray($variables);
59
        $this->assertCount(1, $variables);
60
        $this->assertArrayHasKey('variable', $variables);
61
    }
62
63
    public function testExtensionShouldIncludeModelTest(): void
64
    {
65
        $extension = new Extension(new Pipeline());
66
        $presentationModel = new PresentationModel();
67
68
        /** @var TwigTest[] $tests */
69
        $tests = array_filter($extension->getTests(), function (TwigTest $test): bool {
70
            return $test->getName() === 'model';
71
        });
72
73
        $this->assertCount(1, $tests);
74
75
        list($test) = $tests;
76
        $callback = $test->getCallable();
77
78
        $this->assertTrue($callback($presentationModel));
79
    }
80
}
81