Haanga /tests/templateTest.php

Language PHP Lines 137
MD5 Hash 5c6b12bc293f14e3d8a642af0a68b85d Estimated Cost $2,030 (why?)
Repository git://github.com/crodas/Haanga.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

class Foo_Bar {
    static $Bar = 'haanga';
    static $Arr = array('foo', 'Bar' => 'Foo');
    protected $foo = 'foo';
    public $bar = 'bar';

    static function something()
    {
        return 'something';
    }

    function method() {
        return $this->foo;
    }

    function bar() {
        return "something else";
    }
}

/**
 *  @runTestsInSeparateProcess
 */
class templateTest extends PHPUnit_Framework_TestCase
{
    public function testInit()
    {
        /* setup */
        @mkdir("tmp/");
        foreach (glob("tmp/*/*") as $file) {
            @unlink($file);
        }
        TestSuite::init();
    }

    public function init($test_file, &$expected)
    {
        if ($test_file == 'assert_templates/strip_whitespace.tpl') {
            Haanga_Compiler::setOption('strip_whitespace', TRUE);
            $expected = rtrim($expected). ' '; /* weird output */
        } else {
            Haanga_Compiler::setOption('strip_whitespace', FALSE);
        }
    }
    /** 
     * @dataProvider tplProvider
     */
    public function testRuntime($test_file, $data, $expected)
    {
        $this->init($test_file, $expected);
        $output = Haanga::Load($test_file, $data, TRUE);
        $this->assertEquals($output, $expected);
    }

    /** 
     * @dataProvider tplProvider
     */
    public function testLambda($test_file, $data, $expected)
    {
        $this->init($test_file, $expected);
        $callback = Haanga::compile(file_get_contents($test_file), $data);
        $output   = $callback($data);
        $this->assertEquals($output, $expected);
    }


    /** 
     * @dataProvider tplProvider
     */
    public function testIsCached($test_file, $data, $expected)
    {
        /* same as above, but we ensure that the file wasn't compiled */
        $this->init($test_file, $expected);
        $output = Haanga::Load($test_file, $data, TRUE);
        $this->assertEquals($output, $expected);
        $this->assertFalse(Haanga::$has_compiled);
    }

    /** 
     * @dataProvider tplProvider 
     * /
    public function testCLICompiler($test_file, $data, $expected)
    {
        TestSuite::init();
        $GLOBALS['argv'][1] = $test_file;
        $GLOBALS['argv'][2] = '--notags';

        ob_start();
        Haanga_Compiler::main_cli();
        $code = ob_get_clean();

        eval($code);

        $file     = basename($test_file);
        $pos      = strpos($file,'.');
        $function = substr($file, 0, $pos).'_template';
        $output   = call_user_func($function, $data, TRUE);

        $this->assertEquals($output, $expected);
    }
    /* */

    public function tplProvider()
    {
        $datas = array();
        foreach (glob("assert_templates/*.tpl") as $test_file) {
            $data = array();
            $data_file = substr($test_file, 0, -3)."php";
            $expected  = substr($test_file, 0, -3)."html";
            if (!is_file($expected)) {
                if (!is_file($expected.".php")) {
                    continue;
                } 
                $expected .= ".php";
                ob_start();
                require $expected;
                $expected = ob_get_clean();
            } else {
                $expected = file_get_contents($expected);
            }

            if (is_file($data_file)) {
                try {
                    include $data_file;
                } Catch (Exception $e) {
                    continue;
                }
            }
            $datas[] = array($test_file, $data, $expected);
        }

        return $datas;
    }
}
Back to Top