Skip to content

Commit

Permalink
Fixed Volt::callMacro (phalcon#11765)
Browse files Browse the repository at this point in the history
* Fixed issue phalcon#11745

* Amended Volt tests

* Small cleanup
  • Loading branch information
sergeyklay authored and andresgutierrez committed May 10, 2016
1 parent 517596f commit c2f3dc9
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# [2.0.12](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.12) (????-??-??)
- Fixed regression changes for `Phalcon\Mvc\View\Engine\Volt::callMacro` [#11745](https://github.com/phalcon/cphalcon/issues/11745)

# [2.0.11](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.11) (2016-05-04)
- Fix Model magic set functionality to maintain variable visibility and utilize setter methods.[#11286](https://github.com/phalcon/cphalcon/issues/11286)
Expand Down
7 changes: 3 additions & 4 deletions phalcon/mvc/view/engine/volt.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
Expand Down Expand Up @@ -121,7 +121,6 @@ class Volt extends Engine implements EngineInterface

if mustClean {
this->_view->setContent(ob_get_contents());
//ob_clean();
}
}

Expand Down Expand Up @@ -285,14 +284,14 @@ class Volt extends Engine implements EngineInterface
/**
* Checks if a macro is defined and calls it
*/
public function callMacro(string! name, array arguments)
public function callMacro(string! name, array arguments = []) -> var
{
var macro;

if !fetch macro, this->_macros[name] {
throw new Exception("Macro '" . name . "' does not exist");
}

return call_user_func_array(macro, arguments);
return call_user_func(macro, arguments);
}
}
1 change: 0 additions & 1 deletion unit-tests/TODO.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- Tests for foreign keys cascade in the ORM
- Tests for many-to-many relations
- Tests for macros in Volt
- Tests for +=, -=, *=, /=, ++, -- in Volt
114 changes: 113 additions & 1 deletion unit-tests/ViewEnginesVoltTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) |
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
Expand All @@ -19,6 +19,12 @@
*/

use Phalcon\Mvc\View\Engine\Volt\Compiler;
use Phalcon\Mvc\View\Engine\Volt;
use Phalcon\Mvc\View;
use Phalcon\Escaper;
use Phalcon\Mvc\Url;
use Phalcon\Tag;
use Phalcon\Di;

class SomeObject implements Iterator, Countable
{
Expand Down Expand Up @@ -1120,12 +1126,17 @@ public function testVoltUsersFunctions()
return 'str_shuffle(' . $arguments . ')';
});

$volt->addFunction('strtotime', 'strtotime');

$compilation = $volt->compileString('{{ random() }}');
$this->assertEquals($compilation, '<?php echo mt_rand(); ?>');

$compilation = $volt->compileString('{{ shuffle("hello") }}');
$this->assertEquals($compilation, '<?php echo str_shuffle(\'hello\'); ?>');

$compilation = $volt->compileString('{{ strtotime("now") }}');
$this->assertEquals("<?php echo strtotime('now'); ?>", $compilation);

}

public function testVoltUsersFilters()
Expand Down Expand Up @@ -1379,4 +1390,105 @@ public function testVoltEngineBuiltInFunctions()

$this->assertEquals($view->getContent(), 'Length Array: 4Length Object: 4Length String: 5Length No String: 4Slice Array: 1,2,3,4Slice Array: 2,3Slice Array: 1,2,3Slice Object: 2,3,4Slice Object: 2,3Slice Object: 1,2Slice String: helSlice String: elSlice String: lloSlice No String: 123Slice No String: 23Slice No String: 34');
}

public function testVoltMacros()
{
$this->removeFiles([
'unit-tests/views/macro/hello.volt.php',
'unit-tests/views/macro/conditionaldate.volt.php',
'unit-tests/views/macro/my_input.volt.php',
'unit-tests/views/macro/error_messages.volt.php',
'unit-tests/views/macro/related_links.volt.php',
'unit-tests/views/macro/strtotime.volt.php',
]);

Di::reset();

$view = new View;
$di = new Di;

$di->set('escaper', function() { return new Escaper; });
$di->set('tag', function() { return new Tag; });
$di->set('url', function() { return (new Url)->setBaseUri('/'); });

$view->setDI($di);
$view->setViewsDir('unit-tests/views/');
$view->registerEngines(array(
'.volt' => function ($view, $di) {
$volt = new Volt($view, $di);
$compiler = $volt->getCompiler();
$compiler->addFunction('strtotime', 'strtotime');

return $volt;
}
));

$view->start();
$view->render('macro', 'hello');
$view->finish();

$this->assertEquals('Hello World', $view->getContent());

$view->start();
$view->render('macro', 'conditionaldate');
$view->finish();

$this->assertEquals(sprintf('from <br/>%s, %s UTC', date('Y-m-d'), date('H:i')), $view->getContent());

$view->start();
$view->render('macro', 'my_input');
$view->finish();

$this->assertEquals('<p><input type="text" id="name" name="name" class="input-text" /></p>', $view->getContent());

$view->start();
$view->render('macro', 'error_messages');
$view->finish();

$this->assertEquals('<div><span class="error-type">Invalid</span><span class="error-field">name</span><span class="error-message">The name is invalid</span></div>', $view->getContent());

$view->setVar('links', array((object) array('url' => 'localhost', 'text' => 'Menu item', 'title' => 'Menu title')));

$view->start();
$view->render('macro', 'related_links');
$view->finish();

$this->assertEquals('<ul><li><a href="/localhost" title="Menu title">Menu item</a></li></ul>', $view->getContent());

$view->setVar('date', new DateTime());

$view->start();
$view->render('macro', 'strtotime');
$view->finish();

$content = $view->getContent();
$content = explode('%', $content);

$this->assertEquals(3, count($content));
$this->assertEquals($content[0], $content[1]);
$this->assertEquals($content[1], $content[2]);
$this->assertEquals($content[2], $content[0]);

$this->removeFiles([
'unit-tests/views/macro/hello.volt.php',
'unit-tests/views/macro/conditionaldate.volt.php',
'unit-tests/views/macro/my_input.volt.php',
'unit-tests/views/macro/error_messages.volt.php',
'unit-tests/views/macro/related_links.volt.php',
'unit-tests/views/macro/strtotime.volt.php',
]);
}

protected function removeFiles($files)
{
if (!is_array($files)) {
$files = array($files);
}

foreach ($files as $file) {
if (file_exists($file) && is_readable($file)) {
@unlink($file);
}
}
}
}
9 changes: 9 additions & 0 deletions unit-tests/views/macro/conditionaldate.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{%- macro conditionaldate(condition, tdate, ttime, tz) -%}
{%- if condition -%} from <br/>{{ tdate }}, {{ ttime }} {{ tz }}{%- endif -%}
{%- endmacro -%}

{%- set tdate = date('Y-m-d') -%}
{%- set ttime = date('H:i') -%}
{%- set tz = 'UTC' -%}

{{- conditionaldate(true, tdate, ttime, tz) -}}
5 changes: 5 additions & 0 deletions unit-tests/views/macro/error_messages.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{%- macro error_messages(message, field, type) -%}
<div><span class="error-type">{{ type }}</span><span class="error-field">{{ field }}</span><span class="error-message">{{ message }}</span></div>
{%- endmacro -%}

{{- error_messages('type': 'Invalid', 'message': 'The name is invalid', 'field': 'name') -}}
5 changes: 5 additions & 0 deletions unit-tests/views/macro/hello.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{%- macro hello(name) -%}
{%- return "Hello " ~ name -%}
{%- endmacro -%}

{{- hello('World') -}}
5 changes: 5 additions & 0 deletions unit-tests/views/macro/my_input.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{%- macro my_input(name, class="input-text") -%}
{%- return text_field(name, 'class': class) -%}
{%- endmacro -%}

{{- '<p>' ~ my_input('name') ~ '</p>' -}}
9 changes: 9 additions & 0 deletions unit-tests/views/macro/related_links.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{%- macro related_bar(related_links) -%}
<ul>
{%- for link in related_links -%}
<li><a href="{{ url(link.url) }}" title="{{ link.title|striptags }}">{{ link.text }}</a></li>
{%- endfor -%}
</ul>
{%- endmacro -%}

{{- related_bar(links) -}}
1 change: 1 addition & 0 deletions unit-tests/views/macro/strtotime.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- date.getTimeStamp() -}} % {{- strtotime('now') -}} % {{- strtotime(date.format('Y-m-d H:i:s')) -}}

0 comments on commit c2f3dc9

Please sign in to comment.