Phalcon\Test\Unit\Mvc\View\Engine\Volt\CompilerTest::testVoltCompiler PHP Method

testVoltCompiler() public method

public testVoltCompiler ( )
    public function testVoltCompiler()
    {
        $this->specify("Volt parser doesn't work as expected", function () {
            $volt = new Compiler();
            $compilation = $volt->compileString('');
            expect($compilation)->equals('');
            //Comments
            $compilation = $volt->compileString('{# hello #}');
            expect($compilation)->equals('');
            $compilation = $volt->compileString('{# hello #}{# other comment #}');
            expect($compilation)->equals('');
            //Common Expressions
            $compilation = $volt->compileString('hello');
            expect($compilation)->equals('hello');
            $compilation = $volt->compileString('{{ "hello" }}');
            expect($compilation)->equals("<?= 'hello' ?>");
            $compilation = $volt->compileString('{{ "hello" }}{{ "hello" }}');
            expect($compilation)->equals("<?= 'hello' ?><?= 'hello' ?>");
            $compilation = $volt->compileString('{{ "hello" }}-{{ "hello" }}');
            expect($compilation)->equals("<?= 'hello' ?>-<?= 'hello' ?>");
            $compilation = $volt->compileString('-{{ "hello" }}{{ "hello" }}-');
            expect($compilation)->equals("-<?= 'hello' ?><?= 'hello' ?>-");
            $compilation = $volt->compileString('-{{ "hello" }}-{{ "hello" }}-');
            expect($compilation)->equals("-<?= 'hello' ?>-<?= 'hello' ?>-");
            $compilation = $volt->compileString('Some = {{ 100+50 }}');
            expect($compilation)->equals("Some = <?= 100 + 50 ?>");
            $compilation = $volt->compileString('Some = {{ 100-50 }}');
            expect($compilation)->equals("Some = <?= 100 - 50 ?>");
            $compilation = $volt->compileString('Some = {{ 100*50 }}');
            expect($compilation)->equals("Some = <?= 100 * 50 ?>");
            $compilation = $volt->compileString('Some = {{ 100/50 }}');
            expect($compilation)->equals("Some = <?= 100 / 50 ?>");
            $compilation = $volt->compileString('Some = {{ 100%50 }}');
            expect($compilation)->equals("Some = <?= 100 % 50 ?>");
            $compilation = $volt->compileString('Some = {{ 100~50 }}');
            expect($compilation)->equals("Some = <?= 100 . 50 ?>");
            //Unary operators
            $compilation = $volt->compileString('{{ -10 }}');
            expect($compilation)->equals("<?= -10 ?>");
            $compilation = $volt->compileString('{{ !10 }}');
            expect($compilation)->equals("<?= !10 ?>");
            $compilation = $volt->compileString('{{ !a }}');
            expect($compilation)->equals('<?= !$a ?>');
            $compilation = $volt->compileString('{{ not a }}');
            expect($compilation)->equals('<?= !$a ?>');
            //Arrays
            $compilation = $volt->compileString("{% set a = [1, 2, 3, 4] %}");
            expect($compilation)->equals('<?php $a = [1, 2, 3, 4]; ?>');
            $compilation = $volt->compileString('{% set a = ["hello", 2, 1.3, false, true, null] %}');
            expect($compilation)->equals('<?php $a = [\'hello\', 2, 1.3, false, true, null]; ?>');
            $compilation = $volt->compileString('{% set a = ["hello", 2, 3, false, true, null, [1, 2, "hola"]] %}');
            expect($compilation)->equals('<?php $a = [\'hello\', 2, 3, false, true, null, [1, 2, \'hola\']]; ?>');
            $compilation = $volt->compileString("{% set a = ['first': 1, 'second': 2, 'third': 3] %}");
            expect($compilation)->equals('<?php $a = [\'first\' => 1, \'second\' => 2, \'third\' => 3]; ?>');
            //Array access
            $compilation = $volt->compileString('{{ a[0 ]}}');
            expect($compilation)->equals('<?= $a[0] ?>');
            $compilation = $volt->compileString('{{ a[0 ] [ 1]}}');
            expect($compilation)->equals('<?= $a[0][1] ?>');
            $compilation = $volt->compileString('{{ a[0]  [ "hello"] }}');
            expect($compilation)->equals('<?= $a[0][\'hello\'] ?>');
            $compilation = $volt->compileString('{{ a[0] [1.2] [false] [true] }}');
            expect($compilation)->equals('<?= $a[0][1.2][false][true] ?>');
            //Attribute access
            $compilation = $volt->compileString('{{ a.b }}');
            expect($compilation)->equals('<?= $a->b ?>');
            $compilation = $volt->compileString('{{ a.b.c }}');
            expect($compilation)->equals('<?= $a->b->c ?>');
            //Ranges
            $compilation = $volt->compileString('{{ 1..100 }}');
            expect($compilation)->equals('<?= range(1, 100) ?>');
            $compilation = $volt->compileString('{{ "Z".."A" }}');
            expect($compilation)->equals('<?= range(\'Z\', \'A\') ?>');
            $compilation = $volt->compileString("{{ 'a'..'z' }}");
            expect($compilation)->equals('<?= range(\'a\', \'z\') ?>');
            $compilation = $volt->compileString("{{ 'a' .. 'z' }}");
            expect($compilation)->equals('<?= range(\'a\', \'z\') ?>');
            //Calling functions
            $compilation = $volt->compileString("{{ content() }}");
            expect($compilation)->equals('<?= $this->getContent() ?>');
            $compilation = $volt->compileString("{{ get_content() }}");
            expect($compilation)->equals('<?= $this->getContent() ?>');
            $compilation = $volt->compileString("{{ partial('hello/x') }}");
            expect($compilation)->equals('<?= $this->partial(\'hello/x\') ?>');
            $compilation = $volt->compileString("{{ dump(a) }}");
            expect($compilation)->equals('<?= var_dump($a) ?>');
            $compilation = $volt->compileString("{{ date('Y-m-d', time()) }}");
            expect($compilation)->equals('<?= date(\'Y-m-d\', time()) ?>');
            $compilation = $volt->compileString("{{ robots.getPart(a) }}");
            expect($compilation)->equals('<?= $robots->getPart($a) ?>');
            //Phalcon\Tag helpers
            $compilation = $volt->compileString("{{ link_to('hello', 'some-link') }}");
            expect($compilation)->equals('<?= $this->tag->linkTo([\'hello\', \'some-link\']) ?>');
            $compilation = $volt->compileString("{{ form('action': 'save/products', 'method': 'post') }}");
            expect($compilation)->equals('<?= $this->tag->form([\'action\' => \'save/products\', \'method\' => \'post\']) ?>');
            $compilation = $volt->compileString("{{ stylesheet_link(config.cdn.css.bootstrap, config.cdn.local) }}");
            expect($compilation)->equals('<?= $this->tag->stylesheetLink($config->cdn->css->bootstrap, $config->cdn->local) ?>');
            $compilation = $volt->compileString("{{ javascript_include('js/some.js') }}");
            expect($compilation)->equals('<?= $this->tag->javascriptInclude(\'js/some.js\') ?>');
            $compilation = $volt->compileString("{{ image('img/logo.png', 'width': 80) }}");
            expect($compilation)->equals("<?= \$this->tag->image(['img/logo.png', 'width' => 80]) ?>");
            $compilation = $volt->compileString("{{ email_field('email', 'class': 'form-control', 'placeholder': 'Email Address') }}");
            expect($compilation)->equals("<?= \$this->tag->emailField(['email', 'class' => 'form-control', 'placeholder' => 'Email Address']) ?>");
            //Filters
            $compilation = $volt->compileString('{{ "hello"|e }}');
            expect($compilation)->equals('<?= $this->escaper->escapeHtml(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|escape }}');
            expect($compilation)->equals('<?= $this->escaper->escapeHtml(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|trim }}');
            expect($compilation)->equals('<?= trim(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|striptags }}');
            expect($compilation)->equals('<?= strip_tags(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|json_encode }}');
            expect($compilation)->equals('<?= json_encode(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|url_encode }}');
            expect($compilation)->equals('<?= urlencode(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|uppercase }}');
            expect($compilation)->equals('<?= Phalcon\\Text::upper(\'hello\') ?>');
            $compilation = $volt->compileString('{{ "hello"|lowercase }}');
            expect($compilation)->equals('<?= Phalcon\\Text::lower(\'hello\') ?>');
            $compilation = $volt->compileString('{{ ("hello" ~ "lol")|e|length }}');
            expect($compilation)->equals('<?= $this->length($this->escaper->escapeHtml((\'hello\' . \'lol\'))) ?>');
            //Filters with parameters
            $compilation = $volt->compileString('{{ "My name is %s, %s"|format(name, "thanks") }}');
            expect($compilation)->equals("<?= sprintf('My name is %s, %s', \$name, 'thanks') ?>");
            $compilation = $volt->compileString('{{ "some name"|convert_encoding("utf-8", "latin1") }}');
            expect($compilation)->equals("<?= \$this->convertEncoding('some name', 'utf-8', 'latin1') ?>");
            //if statement
            $compilation = $volt->compileString('{% if a==b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a == $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a!=b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a != $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is not b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a != $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a<b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a < $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a>b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a > $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a>=b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a >= $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a<=b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a <= $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a===b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a === $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a!==b %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a !== $b) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a==b and c==d %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a == $b && $c == $d) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a==b or c==d %} hello {% endif %}');
            expect($compilation)->equals('<?php if ($a == $b || $c == $d) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is odd %} hello {% endif %}');
            expect($compilation)->equals('<?php if (((($a) % 2) != 0)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is even %} hello {% endif %}');
            expect($compilation)->equals('<?php if (((($a) % 2) == 0)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is empty %} hello {% endif %}');
            expect($compilation)->equals('<?php if (empty($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is not empty %} hello {% endif %}');
            expect($compilation)->equals('<?php if (!empty($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is numeric %} hello {% endif %}');
            expect($compilation)->equals('<?php if (is_numeric($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is not numeric %} hello {% endif %}');
            expect($compilation)->equals('<?php if (!is_numeric($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is scalar %} hello {% endif %}');
            expect($compilation)->equals('<?php if (is_scalar($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is not scalar %} hello {% endif %}');
            expect($compilation)->equals('<?php if (!is_scalar($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is iterable %} hello {% endif %}');
            expect($compilation)->equals('<?php if ((is_array($a) || ($a) instanceof Traversable)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is not iterable %} hello {% endif %}');
            expect($compilation)->equals('<?php if (!(is_array($a) || ($a) instanceof Traversable)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is sameas(false) %} hello {% endif %}');
            expect($compilation)->equals('<?php if (($a) === (false)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is sameas(b) %} hello {% endif %}');
            expect($compilation)->equals('<?php if (($a) === ($b)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is divisibleby(3) %} hello {% endif %}');
            expect($compilation)->equals('<?php if (((($a) % (3)) == 0)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is divisibleby(b) %} hello {% endif %}');
            expect($compilation)->equals('<?php if (((($a) % ($b)) == 0)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is defined %} hello {% endif %}');
            expect($compilation)->equals('<?php if (isset($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is not defined %} hello {% endif %}');
            expect($compilation)->equals('<?php if (!isset($a)) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% if a==b %} hello {% else %} not hello {% endif %}');
            expect($compilation)->equals('<?php if ($a == $b) { ?> hello <?php } else { ?> not hello <?php } ?>');
            $compilation = $volt->compileString('{% if a==b %} {% if c==d %} hello {% endif %} {% else %} not hello {% endif %}');
            expect($compilation)->equals('<?php if ($a == $b) { ?> <?php if ($c == $d) { ?> hello <?php } ?> <?php } else { ?> not hello <?php } ?>');
            $compilation = $volt->compileString('{% if a==b %} {% if c==d %} hello {% else %} not hello {% endif %}{% endif %}');
            expect($compilation)->equals('<?php if ($a == $b) { ?> <?php if ($c == $d) { ?> hello <?php } else { ?> not hello <?php } ?><?php } ?>');
            $compilation = $volt->compileString('{% if a==b %} hello {% else %} {% if c==d %} not hello {% endif %} {% endif %}');
            expect($compilation)->equals('<?php if ($a == $b) { ?> hello <?php } else { ?> <?php if ($c == $d) { ?> not hello <?php } ?> <?php } ?>');
            $compilation = $volt->compileString('{% if a is empty or a is defined %} hello {% else %} not hello {% endif %}');
            expect($compilation)->equals('<?php if (empty($a) || isset($a)) { ?> hello <?php } else { ?> not hello <?php } ?>');
            $compilation = $volt->compileString('{% if a is even or b is odd %} hello {% else %} not hello {% endif %}');
            expect($compilation)->equals('<?php if (((($a) % 2) == 0) || ((($b) % 2) != 0)) { ?> hello <?php } else { ?> not hello <?php } ?>');
            //for statement
            $compilation = $volt->compileString('{% for a in b %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach ($b as $a) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% for a in b[0] %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach ($b[0] as $a) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% for a in b.c %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach ($b->c as $a) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% for key, value in [0, 1, 3, 5, 4] %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach ([0, 1, 3, 5, 4] as $key => $value) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% for key, value in [0, 1, 3, 5, 4] if key!=3 %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach ([0, 1, 3, 5, 4] as $key => $value) { if ($key != 3) { ?> hello <?php } ?><?php } ?>');
            $compilation = $volt->compileString('{% for a in 1..10 %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach (range(1, 10) as $a) { ?> hello <?php } ?>');
            $compilation = $volt->compileString('{% for a in 1..10 if a is even %} hello {% endfor %}');
            expect($compilation)->equals('<?php foreach (range(1, 10) as $a) { if (((($a) % 2) == 0)) { ?> hello <?php } ?><?php } ?>');
            $compilation = $volt->compileString('{% for a in 1..10 %} {% for b in 1..10 %} hello {% endfor %} {% endfor %}');
            expect($compilation)->equals('<?php foreach (range(1, 10) as $a) { ?> <?php foreach (range(1, 10) as $b) { ?> hello <?php } ?> <?php } ?>');
            $compilation = $volt->compileString('{% for a in 1..10 %}{% break %}{% endfor %}');
            expect($compilation)->equals('<?php foreach (range(1, 10) as $a) { ?><?php break; ?><?php } ?>');
            $compilation = $volt->compileString('{% for a in 1..10 %}{% continue %}{% endfor %}');
            expect($compilation)->equals('<?php foreach (range(1, 10) as $a) { ?><?php continue; ?><?php } ?>');
            //set statement
            $compilation = $volt->compileString('{% set a = 1 %}');
            expect($compilation)->equals('<?php $a = 1; ?>');
            $compilation = $volt->compileString('{% set a = a-1 %}');
            expect($compilation)->equals('<?php $a = $a - 1; ?>');
            $compilation = $volt->compileString('{% set a = 1.2 %}');
            expect($compilation)->equals('<?php $a = 1.2; ?>');
            $compilation = $volt->compileString('{% set a = 1.2+1*(20/b) and c %}');
            expect($compilation)->equals('<?php $a = 1.2 + 1 * (20 / $b) && $c; ?>');
            // Cache statement
            $compilation = $volt->compileString('{% cache somekey %} hello {% endcache %}');
            expect($compilation)->equals('<?php $_cache[$somekey] = $this->di->get(\'viewCache\'); $_cacheKey[$somekey] = $_cache[$somekey]->start($somekey); if ($_cacheKey[$somekey] === null) { ?> hello <?php $_cache[$somekey]->save($somekey); } else { echo $_cacheKey[$somekey]; } ?>');
            $compilation = $volt->compileString('{% set lifetime = 500 %}{% cache somekey lifetime %} hello {% endcache %}');
            expect($compilation)->equals('<?php $lifetime = 500; ?><?php $_cache[$somekey] = $this->di->get(\'viewCache\'); $_cacheKey[$somekey] = $_cache[$somekey]->start($somekey, $lifetime); if ($_cacheKey[$somekey] === null) { ?> hello <?php $_cache[$somekey]->save($somekey, null, $lifetime); } else { echo $_cacheKey[$somekey]; } ?>');
            $compilation = $volt->compileString('{% cache somekey 500 %} hello {% endcache %}');
            expect($compilation)->equals('<?php $_cache[$somekey] = $this->di->get(\'viewCache\'); $_cacheKey[$somekey] = $_cache[$somekey]->start($somekey, 500); if ($_cacheKey[$somekey] === null) { ?> hello <?php $_cache[$somekey]->save($somekey, null, 500); } else { echo $_cacheKey[$somekey]; } ?>');
            //Autoescape mode
            $compilation = $volt->compileString('{{ "hello" }}{% autoescape true %}{{ "hello" }}{% autoescape false %}{{ "hello" }}{% endautoescape %}{{ "hello" }}{% endautoescape %}{{ "hello" }}');
            expect($compilation)->equals("<?= 'hello' ?><?= \$this->escaper->escapeHtml('hello') ?><?= 'hello' ?><?= \$this->escaper->escapeHtml('hello') ?><?= 'hello' ?>");
            //Mixed
            $compilation = $volt->compileString('{# some comment #}{{ "hello" }}{# other comment }}');
            expect($compilation)->equals("<?= 'hello' ?>");
            //Autoescape from options
            $volt->setOption("autoescape", true);
            $compilation = $volt->compileString('{{ "hello" }}{% autoescape true %}{{ "hello" }}{% autoescape false %}{{ "hello" }}{% endautoescape %}{{ "hello" }}{% endautoescape %}{{ "hello" }}');
            expect($compilation)->equals("<?= \$this->escaper->escapeHtml('hello') ?><?= \$this->escaper->escapeHtml('hello') ?><?= 'hello' ?><?= \$this->escaper->escapeHtml('hello') ?><?= \$this->escaper->escapeHtml('hello') ?>");
        });
    }