summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
blob: 2f64360e1069881c3dd4f6b84bc6859114a57aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Twig_Function as TwigFunction;
use Twig_Node as TwigNode;

abstract class ExtensionTest extends TestCase
{
    use ArraySubsetAsserts;

    /**
     * Assert that a twig filter was registered
     *
     * @param string         $name
     * @param callable       $callback
     * @param TwigFunction[] $functions
     */
    protected function assertFilterExists($name, $callback, $functions)
    {
        foreach ($functions as $function) {
            if ($function->getName() != $name) {
                continue;
            }

            $this->assertEquals($callback, $function->getCallable());
            return;
        }

        $this->fail(sprintf('Filter %s not found', $name));
    }

    /**
     * Assert that a twig function was registered
     *
     * @param string         $name
     * @param callable       $callback
     * @param TwigFunction[] $functions
     * @param array $options
     */
    protected function assertExtensionExists($name, $callback, $functions, $options = [])
    {
        foreach ($functions as $function) {
            if ($function->getName() != $name) {
                continue;
            }

            $this->assertEquals($callback, $function->getCallable());

            if (isset($options['is_save'])) {
                /** @var TwigNode|MockObject $twigNode */
                $twigNode = $this->createMock(TwigNode::class);

                $this->assertArraySubset($options['is_save'], $function->getSafe($twigNode));
            }

            return;
        }

        $this->fail(sprintf('Function %s not found', $name));
    }

    /**
     * Assert that a global exists
     *
     * @param string  $name
     * @param mixed   $value
     * @param mixed[] $globals
     */
    protected function assertGlobalsExists($name, $value, $globals)
    {
        if (isset($globals[$name])) {
            $this->assertArraySubset([$name => $value], $globals);

            return;
        }

        $this->fail(sprintf('Global %s not found', $name));
    }

    /**
     * Assert that a token parser was set
     *
     * @param $tokenParser
     * @param $tokenParsers
     */
    protected function assertTokenParserExists($tokenParser, $tokenParsers)
    {
        $this->assertArraySubset(
            [$tokenParser],
            $tokenParsers,
            sprintf('Token parser %s not found', get_class($tokenParser))
        );
    }
}