File: ChainTest.php

package info (click to toggle)
php-twig 3.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,940 kB
  • sloc: php: 23,320; makefile: 110; sh: 43
file content (115 lines) | stat: -rw-r--r-- 4,000 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Twig\Tests\Loader;

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use PHPUnit\Framework\TestCase;
use Twig\Error\LoaderError;
use Twig\Loader\ArrayLoader;
use Twig\Loader\ChainLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;

class ChainTest extends TestCase
{
    public function testGetSourceContext()
    {
        $path = __DIR__.'/../Fixtures';
        $loader = new ChainLoader([
            new ArrayLoader(['foo' => 'bar']),
            new ArrayLoader(['errors/index.html' => 'baz']),
            new FilesystemLoader([$path]),
        ]);

        $this->assertEquals('foo', $loader->getSourceContext('foo')->getName());
        $this->assertSame('', $loader->getSourceContext('foo')->getPath());

        $this->assertEquals('errors/index.html', $loader->getSourceContext('errors/index.html')->getName());
        $this->assertSame('', $loader->getSourceContext('errors/index.html')->getPath());
        $this->assertEquals('baz', $loader->getSourceContext('errors/index.html')->getCode());

        $this->assertEquals('errors/base.html', $loader->getSourceContext('errors/base.html')->getName());
        $this->assertEquals(realpath($path.'/errors/base.html'), realpath($loader->getSourceContext('errors/base.html')->getPath()));
        $this->assertNotEquals('baz', $loader->getSourceContext('errors/base.html')->getCode());
    }

    public function testGetSourceContextWhenTemplateDoesNotExist()
    {
        $loader = new ChainLoader([]);

        $this->expectException(LoaderError::class);
        $loader->getSourceContext('foo');
    }

    public function testGetCacheKey()
    {
        $loader = new ChainLoader([
            new ArrayLoader(['foo' => 'bar']),
            new ArrayLoader(['foo' => 'foobar', 'bar' => 'foo']),
        ]);

        $this->assertEquals('foo:bar', $loader->getCacheKey('foo'));
        $this->assertEquals('bar:foo', $loader->getCacheKey('bar'));
    }

    public function testGetCacheKeyWhenTemplateDoesNotExist()
    {
        $loader = new ChainLoader([]);

        $this->expectException(LoaderError::class);
        $loader->getCacheKey('foo');
    }

    public function testAddLoader()
    {
        $fooLoader = new ArrayLoader(['foo' => 'foo:code']);
        $barLoader = new ArrayLoader(['bar' => 'bar:code']);
        $bazLoader = new ArrayLoader(['baz' => 'baz:code']);
        $quxLoader = new ArrayLoader(['qux' => 'qux:code']);

        $loader = new ChainLoader((static function () use ($fooLoader, $barLoader): \Generator {
            yield $fooLoader;
            yield $barLoader;
        })());

        $loader->addLoader($bazLoader);
        $loader->addLoader($quxLoader);

        $this->assertEquals('foo:code', $loader->getSourceContext('foo')->getCode());
        $this->assertEquals('bar:code', $loader->getSourceContext('bar')->getCode());
        $this->assertEquals('baz:code', $loader->getSourceContext('baz')->getCode());
        $this->assertEquals('qux:code', $loader->getSourceContext('qux')->getCode());

        $this->assertEquals([
            $fooLoader,
            $barLoader,
            $bazLoader,
            $quxLoader,
        ], $loader->getLoaders());
    }

    public function testExists()
    {
        $loader1 = $this->createMock(LoaderInterface::class);
        $loader1->expects($this->once())->method('exists')->willReturn(false);
        $loader1->expects($this->never())->method('getSourceContext');

        $loader2 = $this->createMock(LoaderInterface::class);
        $loader2->expects($this->once())->method('exists')->willReturn(true);
        $loader2->expects($this->never())->method('getSourceContext');

        $loader = new ChainLoader();
        $loader->addLoader($loader1);
        $loader->addLoader($loader2);

        $this->assertTrue($loader->exists('foo'));
    }
}