aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/Twig/Test
diff options
context:
space:
mode:
Diffstat (limited to 'inc/Twig/Test')
-rw-r--r--inc/Twig/Test/Function.php35
-rw-r--r--inc/Twig/Test/IntegrationTestCase.php154
-rw-r--r--inc/Twig/Test/Method.php37
-rw-r--r--inc/Twig/Test/Node.php37
-rw-r--r--inc/Twig/Test/NodeTestCase.php58
5 files changed, 321 insertions, 0 deletions
diff --git a/inc/Twig/Test/Function.php b/inc/Twig/Test/Function.php
new file mode 100644
index 00000000..4be6b9b9
--- /dev/null
+++ b/inc/Twig/Test/Function.php
@@ -0,0 +1,35 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12/**
13 * Represents a function template test.
14 *
15 * @author Fabien Potencier <fabien@symfony.com>
16 * @deprecated since 1.12 (to be removed in 2.0)
17 */
18class Twig_Test_Function extends Twig_Test
19{
20 protected $function;
21
22 public function __construct($function, array $options = array())
23 {
24 $options['callable'] = $function;
25
26 parent::__construct($options);
27
28 $this->function = $function;
29 }
30
31 public function compile()
32 {
33 return $this->function;
34 }
35}
diff --git a/inc/Twig/Test/IntegrationTestCase.php b/inc/Twig/Test/IntegrationTestCase.php
new file mode 100644
index 00000000..724f0941
--- /dev/null
+++ b/inc/Twig/Test/IntegrationTestCase.php
@@ -0,0 +1,154 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12/**
13 * Integration test helper
14 *
15 * @author Fabien Potencier <fabien@symfony.com>
16 * @author Karma Dordrak <drak@zikula.org>
17 */
18abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
19{
20 abstract protected function getExtensions();
21 abstract protected function getFixturesDir();
22
23 /**
24 * @dataProvider getTests
25 */
26 public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
27 {
28 $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
29 }
30
31 public function getTests()
32 {
33 $fixturesDir = realpath($this->getFixturesDir());
34 $tests = array();
35
36 foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
37 if (!preg_match('/\.test$/', $file)) {
38 continue;
39 }
40
41 $test = file_get_contents($file->getRealpath());
42
43 if (preg_match('/
44 --TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
45 $message = $match[1];
46 $condition = $match[2];
47 $templates = $this->parseTemplates($match[3]);
48 $exception = $match[5];
49 $outputs = array(array(null, $match[4], null, ''));
50 } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
51 $message = $match[1];
52 $condition = $match[2];
53 $templates = $this->parseTemplates($match[3]);
54 $exception = false;
55 preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
56 } else {
57 throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
58 }
59
60 $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
61 }
62
63 return $tests;
64 }
65
66 protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
67 {
68 if ($condition) {
69 eval('$ret = '.$condition.';');
70 if (!$ret) {
71 $this->markTestSkipped($condition);
72 }
73 }
74
75 $loader = new Twig_Loader_Array($templates);
76
77 foreach ($outputs as $match) {
78 $config = array_merge(array(
79 'cache' => false,
80 'strict_variables' => true,
81 ), $match[2] ? eval($match[2].';') : array());
82 $twig = new Twig_Environment($loader, $config);
83 $twig->addGlobal('global', 'global');
84 foreach ($this->getExtensions() as $extension) {
85 $twig->addExtension($extension);
86 }
87
88 try {
89 $template = $twig->loadTemplate('index.twig');
90 } catch (Exception $e) {
91 if (false !== $exception) {
92 $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
93
94 return;
95 }
96
97 if ($e instanceof Twig_Error_Syntax) {
98 $e->setTemplateFile($file);
99
100 throw $e;
101 }
102
103 throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
104 }
105
106 try {
107 $output = trim($template->render(eval($match[1].';')), "\n ");
108 } catch (Exception $e) {
109 if (false !== $exception) {
110 $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
111
112 return;
113 }
114
115 if ($e instanceof Twig_Error_Syntax) {
116 $e->setTemplateFile($file);
117 } else {
118 $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
119 }
120
121 $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
122 }
123
124 if (false !== $exception) {
125 list($class, ) = explode(':', $exception);
126 $this->assertThat(NULL, new PHPUnit_Framework_Constraint_Exception($class));
127 }
128
129 $expected = trim($match[3], "\n ");
130
131 if ($expected != $output) {
132 echo 'Compiled template that failed:';
133
134 foreach (array_keys($templates) as $name) {
135 echo "Template: $name\n";
136 $source = $loader->getSource($name);
137 echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
138 }
139 }
140 $this->assertEquals($expected, $output, $message.' (in '.$file.')');
141 }
142 }
143
144 protected static function parseTemplates($test)
145 {
146 $templates = array();
147 preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
148 foreach ($matches as $match) {
149 $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
150 }
151
152 return $templates;
153 }
154}
diff --git a/inc/Twig/Test/Method.php b/inc/Twig/Test/Method.php
new file mode 100644
index 00000000..17c6c041
--- /dev/null
+++ b/inc/Twig/Test/Method.php
@@ -0,0 +1,37 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12/**
13 * Represents a method template test.
14 *
15 * @author Fabien Potencier <fabien@symfony.com>
16 * @deprecated since 1.12 (to be removed in 2.0)
17 */
18class Twig_Test_Method extends Twig_Test
19{
20 protected $extension;
21 protected $method;
22
23 public function __construct(Twig_ExtensionInterface $extension, $method, array $options = array())
24 {
25 $options['callable'] = array($extension, $method);
26
27 parent::__construct($options);
28
29 $this->extension = $extension;
30 $this->method = $method;
31 }
32
33 public function compile()
34 {
35 return sprintf('$this->env->getExtension(\'%s\')->%s', $this->extension->getName(), $this->method);
36 }
37}
diff --git a/inc/Twig/Test/Node.php b/inc/Twig/Test/Node.php
new file mode 100644
index 00000000..c832a57b
--- /dev/null
+++ b/inc/Twig/Test/Node.php
@@ -0,0 +1,37 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 2010 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12/**
13 * Represents a template test as a Node.
14 *
15 * @author Fabien Potencier <fabien@symfony.com>
16 * @deprecated since 1.12 (to be removed in 2.0)
17 */
18class Twig_Test_Node extends Twig_Test
19{
20 protected $class;
21
22 public function __construct($class, array $options = array())
23 {
24 parent::__construct($options);
25
26 $this->class = $class;
27 }
28
29 public function getClass()
30 {
31 return $this->class;
32 }
33
34 public function compile()
35 {
36 }
37}
diff --git a/inc/Twig/Test/NodeTestCase.php b/inc/Twig/Test/NodeTestCase.php
new file mode 100644
index 00000000..b15c85ff
--- /dev/null
+++ b/inc/Twig/Test/NodeTestCase.php
@@ -0,0 +1,58 @@
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
12{
13 abstract public function getTests();
14
15 /**
16 * @dataProvider getTests
17 */
18 public function testCompile($node, $source, $environment = null)
19 {
20 $this->assertNodeCompilation($source, $node, $environment);
21 }
22
23 public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null)
24 {
25 $compiler = $this->getCompiler($environment);
26 $compiler->compile($node);
27
28 $this->assertEquals($source, trim($compiler->getSource()));
29 }
30
31 protected function getCompiler(Twig_Environment $environment = null)
32 {
33 return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
34 }
35
36 protected function getEnvironment()
37 {
38 return new Twig_Environment();
39 }
40
41 protected function getVariableGetter($name)
42 {
43 if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
44 return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
45 }
46
47 return sprintf('$this->getContext($context, "%s")', $name);
48 }
49
50 protected function getAttributeGetter()
51 {
52 if (function_exists('twig_template_get_attributes')) {
53 return 'twig_template_get_attributes($this, ';
54 }
55
56 return '$this->getAttribute(';
57 }
58}