FrontMatterLoaderTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This is part of the webuni/front-matter package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\FrontMatter\Tests\Twig;
14
15
use PHPUnit\Framework\TestCase;
16
use Twig\Loader\ExistsLoaderInterface;
17
use Twig\Loader\LoaderInterface;
18
use Twig\Loader\SourceContextLoaderInterface;
19
use Twig\Source;
20
use Webuni\FrontMatter\FrontMatter;
21
use Webuni\FrontMatter\Twig\FrontMatterLoader;
22
23
class FrontMatterLoaderTest extends TestCase
24
{
25
    private $frontMatter;
26
    private $originalLoader;
27
    private $loader;
28
29
    protected function setUp(): void
30
    {
31
        $this->frontMatter = FrontMatter::createYaml();
32
        $this->originalLoader = $this->createMock(LoaderInterface::class);
33
34
        $this->loader = new FrontMatterLoader($this->frontMatter, $this->originalLoader);
35
    }
36
37
    public function testGetCacheKey(): void
38
    {
39
        $this->originalLoader->method('getCacheKey')->with($name = 'name')->willReturn($name);
40
        $this->assertEquals($name, $this->loader->getCacheKey($name));
41
    }
42
43
    public function testIsFresh(): void
44
    {
45
        $this->originalLoader->method('isFresh')->with($name = 'name', $time = time())->willReturn(true);
46
        $this->assertTrue($this->loader->isFresh($name, $time));
47
    }
48
49
    public function testExists(): void
50
    {
51
        $loader = new FrontMatterLoader($this->frontMatter, $this->originalLoader);
52
53
        $this->originalLoader->method('exists')->with($name = 'name')->willReturn(true);
54
        $this->assertTrue($loader->exists($name));
55
    }
56
57
    /**
58
     * @dataProvider getSource
59
     */
60
    public function testGetSourceContext(string $source, string $content, array $data, int $line = null): void
61
    {
62
        $name = 'name';
63
        $source = new Source($source, $name);
64
65
        $this->originalLoader
66
            ->method('getSourceContext')
67
            ->with($name)
68
            ->willReturn($source)
69
        ;
70
        $document = $this->frontMatter->parse($source->getCode());
71
        $this->assertEquals($content, $document->getContent());
72
        $this->assertEquals($data, $document->getData());
73
        $this->assertEquals(($line ? "{% line $line %}\n" : '').$content, $this->loader->getSourceContext($name)->getCode());
74
    }
75
76
    public function getSource(): array
77
    {
78
        return [
79
            ["{{ foo }}", '{{ foo }}', []],
80
            ["---\nfoo: bar\n---\n{{ foo }}", '{{ foo }}', ['foo' => 'bar'], 3],
81
        ];
82
    }
83
}
84