|
1
|
|
|
<?php |
|
2
|
|
|
namespace Admingenerator\GeneratorBundle\Tests\Twig\Extension; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* |
|
6
|
|
|
* Base class to test extensions. Provide builtin functions to initialize |
|
7
|
|
|
* new Twig environment in order to assert a template and its rendered version |
|
8
|
|
|
* are coherent. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Stéphane Escandell |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class BaseExtensionTest extends \PHPUnit_Framework_TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Variables used for templates |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $twigVariables = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return \Twig_Extension |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract protected function getTestedExtension(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract protected function getTwigVariables(); |
|
30
|
|
|
|
|
31
|
|
|
public function setUp() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->twigVariables = $this->getTwigVariables(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function runTwigTests(array $templates, array $returns) |
|
37
|
|
|
{ |
|
38
|
|
|
if (array_diff(array_keys($templates), array_keys($returns))) { |
|
39
|
|
|
throw new \LogicException(sprintf( |
|
40
|
|
|
'Error: invalid test case. Templates and returns keys mismatch: templates:[%s], returns:[%s] => [%s]', |
|
41
|
|
|
implode(', ', array_keys($templates)), |
|
42
|
|
|
implode(', ', array_keys($returns)), |
|
43
|
|
|
implode(', ', array_diff(array_keys($templates), array_keys($returns))) |
|
44
|
|
|
)); |
|
45
|
|
|
} |
|
46
|
|
|
$twig = $this->getEnvironment($templates); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($templates as $name => $tpl) { |
|
49
|
|
|
$this->assertEquals( |
|
50
|
|
|
$returns[$name][0], |
|
51
|
|
|
$twig->loadTemplate($name)->render($this->twigVariables), |
|
52
|
|
|
$returns[$name][1] |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getEnvironment($templates, $options = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
$twig = new \Twig_Environment( |
|
|
|
|
|
|
60
|
|
|
new \Twig_Loader_Array($templates), |
|
|
|
|
|
|
61
|
|
|
array_merge( |
|
62
|
|
|
array( |
|
63
|
|
|
'debug' => true, |
|
64
|
|
|
'cache' => false, |
|
65
|
|
|
'autoescape' => false, |
|
66
|
|
|
), |
|
67
|
|
|
$options |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
$twig->addExtension($this->getTestedExtension()); |
|
71
|
|
|
|
|
72
|
|
|
return $twig; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.