$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'); $value = 12; $t->is_deeply($template->escape($value), $value, '->escape() handles integers correctly'); $value = 3.1415; $t->is_deeply($template->escape($value), $value, '->escape() handles floats correctly'); $value = '<b>foo</b>'; $t->is_deeply($template->escape($value), htmlspecialchars($value), '->escape() handles strings correctly'); $value = true; $t->is_deeply($template->escape($value), $value, '->escape() handles booleans correctly'); $value = array(); $t->is_deeply($template->escape($value), $value, '->escape() handles arrays correctly'); $value = new stdClass(); $t->is_deeply($template->escape($value), $value, '->escape() handles objects correctly'); $t->diag('inheritance'); class layout extends greebo\conveniences\Template
$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; $t->is_deeply(Escaper::escape($value, $escaper), $value, '::escape() returns correctly escaped floats');