1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace JaySDe\HandlebarsBundle\Tests\Loader; |
7
|
|
|
|
8
|
|
|
use JaySDe\HandlebarsBundle\Loader\FilesystemLoader; |
9
|
|
|
use JaySDe\HandlebarsBundle\Tests\TestCase; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
11
|
|
|
|
12
|
|
|
class FilesystemLoaderTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testGetSource() |
15
|
|
|
{ |
16
|
|
|
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock(); |
17
|
|
|
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock(); |
18
|
|
|
$locator |
19
|
|
|
->expects($this->once()) |
20
|
|
|
->method('locate') |
21
|
|
|
->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.hbs')) |
22
|
|
|
; |
23
|
|
|
$loader = new FilesystemLoader($locator, $parser); |
24
|
|
|
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views', 'namespace'); |
25
|
|
|
// Twig-style |
26
|
|
|
$this->assertEquals("This is a layout\n", $loader->getSource('@namespace/layout.html.hbs')); |
27
|
|
|
// Symfony-style |
28
|
|
|
$this->assertEquals("This is a layout\n", $loader->getSource('HandlebarsBundle::layout.html.hbs')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testExists() |
32
|
|
|
{ |
33
|
|
|
// should return true for templates that Handlebars does not find, but Symfony does |
34
|
|
|
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock(); |
35
|
|
|
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock(); |
36
|
|
|
$locator |
37
|
|
|
->expects($this->once()) |
38
|
|
|
->method('locate') |
39
|
|
|
->will($this->returnValue($template = __DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.hbs')) |
40
|
|
|
; |
41
|
|
|
$loader = new FilesystemLoader($locator, $parser); |
42
|
|
|
|
43
|
|
|
$this->assertTrue($loader->exists($template)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @expectedException \JaySDe\HandlebarsBundle\Error\LoaderException |
48
|
|
|
*/ |
49
|
|
|
public function testErrorIfLocatorThrowsInvalid() |
50
|
|
|
{ |
51
|
|
|
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock(); |
52
|
|
|
$parser |
53
|
|
|
->expects($this->once()) |
54
|
|
|
->method('parse') |
55
|
|
|
->with('name.format.engine') |
56
|
|
|
->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine'))) |
57
|
|
|
; |
58
|
|
|
|
59
|
|
|
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock(); |
60
|
|
|
$locator |
61
|
|
|
->expects($this->once()) |
62
|
|
|
->method('locate') |
63
|
|
|
->will($this->throwException(new \InvalidArgumentException('Unable to find template "NonExistent".'))) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$loader = new FilesystemLoader($locator, $parser); |
67
|
|
|
$loader->getCacheKey('name.format.engine'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @expectedException \JaySDe\HandlebarsBundle\Error\LoaderException |
72
|
|
|
*/ |
73
|
|
|
public function testErrorIfLocatorReturnsFalse() |
74
|
|
|
{ |
75
|
|
|
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock(); |
76
|
|
|
$parser |
77
|
|
|
->expects($this->once()) |
78
|
|
|
->method('parse') |
79
|
|
|
->with('name.format.engine') |
80
|
|
|
->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine'))) |
81
|
|
|
; |
82
|
|
|
|
83
|
|
|
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock(); |
84
|
|
|
$locator |
85
|
|
|
->expects($this->once()) |
86
|
|
|
->method('locate') |
87
|
|
|
->will($this->returnValue(false)) |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
$loader = new FilesystemLoader($locator, $parser); |
91
|
|
|
$loader->getCacheKey('name.format.engine'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @expectedException \JaySDe\HandlebarsBundle\Error\LoaderException |
96
|
|
|
* @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*Tests.Loader.\.\..DependencyInjection.Fixtures.Resources.views\)/ |
97
|
|
|
*/ |
98
|
|
|
public function testErrorIfTemplateDoesNotExist() |
99
|
|
|
{ |
100
|
|
|
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock(); |
101
|
|
|
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock(); |
102
|
|
|
|
103
|
|
|
$loader = new FilesystemLoader($locator, $parser); |
104
|
|
|
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views'); |
105
|
|
|
|
106
|
|
|
$method = new \ReflectionMethod('JaySDe\HandlebarsBundle\Loader\FilesystemLoader', 'findTemplate'); |
107
|
|
|
$method->setAccessible(true); |
108
|
|
|
$method->invoke($loader, 'name.format.engine'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|