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\Document; |
21
|
|
|
use Webuni\FrontMatter\FrontMatter; |
22
|
|
|
use Webuni\FrontMatter\FrontMatterInterface; |
23
|
|
|
use Webuni\FrontMatter\Twig\FrontMatterLoader; |
24
|
|
|
|
25
|
|
|
class FrontMatterLoaderTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
private $frontMatter; |
28
|
|
|
private $originalLoader; |
29
|
|
|
private $loader; |
30
|
|
|
|
31
|
|
|
protected function setUp(): void |
32
|
|
|
{ |
33
|
|
|
$this->frontMatter = FrontMatter::createYaml(); |
34
|
|
|
$this->originalLoader = $this->createMock(LoaderInterface::class); |
35
|
|
|
|
36
|
|
|
$this->loader = new FrontMatterLoader($this->frontMatter, $this->originalLoader); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetCacheKey(): void |
40
|
|
|
{ |
41
|
|
|
$this->originalLoader->method('getCacheKey')->with($name = 'name')->willReturn($name); |
42
|
|
|
$this->assertEquals($name, $this->loader->getCacheKey($name)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testIsFresh(): void |
46
|
|
|
{ |
47
|
|
|
$this->originalLoader->method('isFresh')->with($name = 'name', $time = time())->willReturn(true); |
48
|
|
|
$this->assertTrue($this->loader->isFresh($name, $time)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testExists(): void |
52
|
|
|
{ |
53
|
|
|
$loader = new FrontMatterLoader($this->frontMatter, $this->originalLoader); |
54
|
|
|
|
55
|
|
|
$this->originalLoader->method('exists')->with($name = 'name')->willReturn(true); |
56
|
|
|
$this->assertTrue($loader->exists($name)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @dataProvider getSource |
61
|
|
|
*/ |
62
|
|
|
public function testGetSourceContext(string $source, string $content, array $data, int $line = null): void |
63
|
|
|
{ |
64
|
|
|
$name = 'name'; |
65
|
|
|
$source = new Source($source, $name); |
66
|
|
|
|
67
|
|
|
$this->originalLoader |
68
|
|
|
->method('getSourceContext') |
69
|
|
|
->with($name) |
70
|
|
|
->willReturn($source) |
71
|
|
|
; |
72
|
|
|
$document = $this->frontMatter->parse($source->getCode()); |
73
|
|
|
$this->assertEquals($content, $document->getContent()); |
74
|
|
|
$this->assertEquals($data, $document->getData()); |
75
|
|
|
$this->assertEquals(($line ? "{% line $line %}" : '').$content, $this->loader->getSourceContext($name)->getCode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getSource(): array |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
["{{ foo }}", '{{ foo }}', []], |
82
|
|
|
["---\nfoo: bar\n---\n{{ foo }}", '{{ foo }}', ['foo' => 'bar'], 4], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|