<?php require_once __DIR__ . '/../../bootstrap.php'; use greebo\essence\Event; use greebo\conveniences\Template; $t = new lime_test(18); $event = new Event(); $template = new Template($event); $t->diag('slot assignment'); $template->foo = 'bar'; $t->is_deeply($template->foo, 'bar', '->__get(), ->__set() works properly'); $template->baz = 'foo'; $t->ok(isset($template->baz) && !isset($template->bar), '->__isset() works properly'); unset($template->baz); $t->is_deeply(isset($template->baz), false, '->__unset() works properly'); $t->is_deeply($template->raw('foo'), 'bar', '->raw() returns assigned slot properly'); $t->is_deeply($template->raw('bar'), null, '->raw() returns unassigned slot properly'); $template->rec('bar'); ?> Testing bar slot <?php $template->stop(); $t->is_deeply($template->bar, "Testing bar slot\n", '->rec(), ->stop() works properly'); $template->assign(array('t1' => 'foo', 't2' => 'bar')); $t->ok($template->t1 === 'foo' && $template->t2 === 'bar', '->assign() mass assignment works properly'); $t->diag('escaper'); $escaper = function ($v) { return htmlspecialchars($v); }; $template->escaper($escaper); $t->is_deeply($template->escaper(), $escaper, '->escaper() works properly');
$t->is_deeply(Escaper::escape($value, $escaper), $value, '::escape() handles strings correctly'); $value = true; $t->is_deeply(Escaper::escape($value, $escaper), $value, '::escape() handles booleans correctly'); $value = array(); $t->ok(Escaper::escape($value, $escaper) instanceof ArrayEscaper, '::escape() handles arrays correctly'); $value = new stdClass(); $t->ok(Escaper::escape($value, $escaper) instanceof ObjectEscaper, '::escape() handles objects correctly'); try { Escaper::escape(fopen('/dev/null', 'r'), $escaper); $t->fail('::escape() throws exception on resources'); } catch (greebo\guards\EscaperException $e) { $t->pass('::escape() throws exception on resources'); } $t->diag('::register()'); $event = new Event(); $template = new Template($event); $template->escaper($escaper); Escaper::register($event); $event->connect('template.slots', function ($template, $slots) use($t) { $escaped = $slots['foo'] instanceof ArrayEscaper && $slots['bar'] instanceof ArrayEscaper; $t->ok($escaped, '::register() works properly'); }); $slots = array('foo' => array(), 'bar' => new ArrayEscaper(array(), $escaper)); $event->filter('template.slots', $template, $slots); $t->diag('HTML escaper'); $escaper = function ($value) { return htmlspecialchars($value, ENT_QUOTES); }; $value = 12; $t->is_deeply(Escaper::escape($value, $escaper), $value, '::escape() returns correctly escaped integers'); $value = 3.1415;