Project

General

Profile

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
 */
11
abstract 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, $isPattern = false)
19
    {
20
        $this->assertNodeCompilation($source, $node, $environment, $isPattern);
21
    }
22

    
23
    public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
24
    {
25
        $compiler = $this->getCompiler($environment);
26
        $compiler->compile($node);
27

    
28
        if ($isPattern) {
29
            $this->assertStringMatchesFormat($source, trim($compiler->getSource()));
30
        } else {
31
            $this->assertEquals($source, trim($compiler->getSource()));
32
        }
33
    }
34

    
35
    protected function getCompiler(Twig_Environment $environment = null)
36
    {
37
        return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
38
    }
39

    
40
    protected function getEnvironment()
41
    {
42
        return new Twig_Environment(new Twig_Loader_Array(array()));
43
    }
44

    
45
    protected function getVariableGetter($name, $line = false)
46
    {
47
        $line = $line > 0 ? "// line {$line}\n" : '';
48

    
49
        if (PHP_VERSION_ID >= 50400) {
50
            return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
51
        }
52

    
53
        return sprintf('%s$this->getContext($context, "%s")', $line, $name);
54
    }
55

    
56
    protected function getAttributeGetter()
57
    {
58
        if (function_exists('twig_template_get_attributes')) {
59
            return 'twig_template_get_attributes($this, ';
60
        }
61

    
62
        return '$this->getAttribute(';
63
    }
64
}
(5-5/5)