|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\Integration\Factories; |
|
6
|
|
|
|
|
7
|
|
|
use Twig_Error_Loader; |
|
8
|
|
|
use Twig_LoaderInterface; |
|
9
|
|
|
use WMDE\Fundraising\Frontend\Factories\TwigFactory; |
|
10
|
|
|
use WMDE\Fundraising\Frontend\Presentation\FilePrefixer; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @covers WMDE\Fundraising\Frontend\Factories\TwigFactory |
|
14
|
|
|
* |
|
15
|
|
|
* @licence GNU GPL v2+ |
|
16
|
|
|
* @author Gabriel Birke < [email protected] > |
|
17
|
|
|
*/ |
|
18
|
|
|
class TwigFactoryTest extends \PHPUnit_Framework_TestCase { |
|
19
|
|
|
|
|
20
|
|
|
public function testTwigInstanceUsesDollarPlaceholdersForVariables() { |
|
21
|
|
|
$factory = new TwigFactory( [ |
|
22
|
|
|
'enable-cache' => false, |
|
23
|
|
|
'loaders' => [ |
|
24
|
|
|
'array' => [ 'variableReplacement.twig' => '{$ testvar $}' ] |
|
25
|
|
|
] |
|
26
|
|
|
], '/tmp/fun' ); |
|
27
|
|
|
$twig = $factory->create( [ $factory->newArrayLoader() ], [], [] ); |
|
28
|
|
|
$result = $twig->render( 'variableReplacement.twig', [ 'testvar' => 'Meeow!' ] ); |
|
29
|
|
|
$this->assertSame( 'Meeow!', $result ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testTwigInstancesTryAllLoadersUntilTemplateIsFound() { |
|
33
|
|
|
$loaderException = new Twig_Error_Loader( 'not found' ); |
|
34
|
|
|
$firstLoader = $this->createMock( Twig_LoaderInterface::class ); |
|
35
|
|
|
$firstLoader->method( 'getSource' )->willThrowException( $loaderException ); |
|
36
|
|
|
$firstLoader->method( 'isFresh' )->willThrowException( $loaderException ); |
|
37
|
|
|
$firstLoader->method( 'getCacheKey' )->willThrowException( $loaderException ); |
|
38
|
|
|
|
|
39
|
|
|
$secondLoader = $this->createMock( Twig_LoaderInterface::class ); |
|
40
|
|
|
$secondLoader->method( 'getSource' )->willReturn( 'Meeow!' ); |
|
41
|
|
|
$secondLoader->method( 'isFresh' )->willReturn( true ); |
|
42
|
|
|
$secondLoader->method( 'getCacheKey' )->willReturn( 'Canis_silvestris' ); |
|
43
|
|
|
|
|
44
|
|
|
$thirdLoader = $this->createMock( Twig_LoaderInterface::class ); |
|
45
|
|
|
$thirdLoader->expects( $this->never() )->method( $this->anything() ); |
|
46
|
|
|
|
|
47
|
|
|
$factory = new TwigFactory( [ 'enable-cache' => false ], '/tmp/fun' ); |
|
48
|
|
|
$twig = $factory->create( [ $firstLoader, $secondLoader, $thirdLoader ], [], [] ); |
|
49
|
|
|
$result = $twig->render( 'Canis_silvestris' ); |
|
50
|
|
|
$this->assertSame( 'Meeow!', $result ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testFilesystemLoaderConvertsStringPathToArray() { |
|
54
|
|
|
$factory = new TwigFactory( [ |
|
55
|
|
|
'loaders' => [ |
|
56
|
|
|
'filesystem' => [ |
|
57
|
|
|
'template-dir' => __DIR__ . '/../../templates' |
|
58
|
|
|
] |
|
59
|
|
|
] |
|
60
|
|
|
], '/tmp/fun' ); |
|
61
|
|
|
$loader = $factory->newFileSystemLoader(); |
|
62
|
|
|
$this->assertSame( [ __DIR__ . '/../../templates' ], $loader->getPaths() ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testFilesystemLoaderPrependsRelativePathsToArray() { |
|
66
|
|
|
$factory = new TwigFactory( [ |
|
67
|
|
|
'loaders' => [ |
|
68
|
|
|
'filesystem' => [ |
|
69
|
|
|
'template-dir' => 'tests/templates' |
|
70
|
|
|
] |
|
71
|
|
|
] |
|
72
|
|
|
], '/tmp/fun' ); |
|
73
|
|
|
$loader = $factory->newFileSystemLoader(); |
|
74
|
|
|
$realPath = realpath( $loader->getPaths()[0] ); |
|
75
|
|
|
$this->assertFalse( $realPath === false, 'path does not exist' ); |
|
76
|
|
|
$this->assertSame( $realPath, realpath( __DIR__ . '/../../templates' ) ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testFilePrefixerIsCalledInTemplate() { |
|
80
|
|
|
$prefixer = $this->getMockBuilder( FilePrefixer::class )->disableOriginalConstructor()->getMock(); |
|
81
|
|
|
$prefixer->expects( $this->once() ) |
|
82
|
|
|
->method( 'prefixFile' ) |
|
83
|
|
|
->willReturn( 'baaaaad.testfile.js' ) |
|
84
|
|
|
->with( 'testfile.js' ); |
|
85
|
|
|
|
|
86
|
|
|
$factory = new TwigFactory( [ |
|
87
|
|
|
'enable-cache' => false, |
|
88
|
|
|
'loaders' => [ |
|
89
|
|
|
'array' => [ 'filePrefix.twig' => '{$ "testfile.js"|prefix_file $}' ] |
|
90
|
|
|
] |
|
91
|
|
|
], '/tmp/fun' ); |
|
92
|
|
|
|
|
93
|
|
|
$filters = [ $factory->newFilePrefixFilter( $prefixer ) ]; |
|
94
|
|
|
$twig = $factory->create( [ $factory->newArrayLoader() ], [], $filters ); |
|
95
|
|
|
$result = $twig->render( 'filePrefix.twig' ); |
|
96
|
|
|
$this->assertSame( 'baaaaad.testfile.js', $result ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|