1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Evheniy\HTML5CacheBundle\Test\Twig; |
4
|
|
|
|
5
|
|
|
use Evheniy\HTML5CacheBundle\Twig\HTML5CacheExtension; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class HTML5CacheExtensionTest |
10
|
|
|
* |
11
|
|
|
* @package Evheniy\HTML5CacheBundle\Test\Twig |
12
|
|
|
*/ |
13
|
|
|
class HTML5CacheExtensionTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var HTML5CacheExtension |
17
|
|
|
*/ |
18
|
|
|
protected $extension; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->extension = new HTML5CacheExtension(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
public function testGetFunctions() |
32
|
|
|
{ |
33
|
|
|
$this->assertTrue(is_array($this->extension->getFunctions())); |
34
|
|
|
$this->assertNotEmpty($this->extension->getFunctions()); |
35
|
|
|
$functions = $this->extension->getFunctions(); |
36
|
|
|
$this->assertInstanceOf('\Twig_SimpleFunction', $functions[0]); |
37
|
|
|
$function = $functions[0]; |
38
|
|
|
$this->assertEquals('cache_manifest', $function->getName()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
public function testGetName() |
45
|
|
|
{ |
46
|
|
|
$this->assertNotEmpty($this->extension->getName()); |
47
|
|
|
$this->assertEquals('html5_cache_extension', $this->extension->getName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
public function testInitRuntime() |
54
|
|
|
{ |
55
|
|
|
$reflectionClass = new \ReflectionClass('\Evheniy\HTML5CacheBundle\Twig\HTML5CacheExtension'); |
56
|
|
|
|
57
|
|
|
$this->extension->initRuntime(new \Twig_Environment(new \Twig_Loader_Array(), array('debug' => false))); |
58
|
|
|
$environment = $reflectionClass->getProperty('environment'); |
59
|
|
|
$environment->setAccessible(true); |
60
|
|
|
/** @var \Twig_Environment $environmentData */ |
61
|
|
|
$environmentData = $environment->getValue($this->extension); |
62
|
|
|
$this->assertInstanceOf('\Twig_Environment', $environmentData); |
63
|
|
|
$this->assertFalse($environmentData->isDebug()); |
64
|
|
|
|
65
|
|
|
$this->extension->initRuntime(new \Twig_Environment(new \Twig_Loader_Array(), array('debug' => true))); |
66
|
|
|
$environment = $reflectionClass->getProperty('environment'); |
67
|
|
|
$environment->setAccessible(true); |
68
|
|
|
/** @var \Twig_Environment $environmentData */ |
69
|
|
|
$environmentData = $environment->getValue($this->extension); |
70
|
|
|
$this->assertInstanceOf('\Twig_Environment', $environmentData); |
71
|
|
|
$this->assertTrue($environmentData->isDebug()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function testGetCacheManifest() |
78
|
|
|
{ |
79
|
|
|
$environment = new \Twig_Environment(new \Twig_Loader_Array(), array('debug' => false)); |
80
|
|
|
$this->extension->initRuntime($environment); |
81
|
|
|
$this->assertEquals($this->extension->getCacheManifest(), ' manifest="/cache.manifest"'); |
82
|
|
|
|
83
|
|
|
$environment = new \Twig_Environment(new \Twig_Loader_Array(), array('debug' => true)); |
84
|
|
|
$this->extension->initRuntime($environment); |
85
|
|
|
$this->assertEquals($this->extension->getCacheManifest(), ''); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
public function testTwigRender() |
92
|
|
|
{ |
93
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Array(array('test' => '{{ cache_manifest()|raw }}')), array('debug' => false)); |
94
|
|
|
$this->extension->initRuntime($twig); |
95
|
|
|
$twig->addExtension($this->extension); |
96
|
|
|
$this->assertEquals($twig->render('test'), ' manifest="/cache.manifest"'); |
97
|
|
|
|
98
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Array(array('test' => '{{ cache_manifest()|raw }}')), array('debug' => true)); |
99
|
|
|
$this->extension->initRuntime($twig); |
100
|
|
|
$twig->addExtension($this->extension); |
101
|
|
|
$this->assertEquals($twig->render('test'), ''); |
102
|
|
|
} |
103
|
|
|
} |