mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-22 13:41:27 +03:00
Add layout helpers
This commit is contained in:
parent
6da6ab2fe5
commit
56c2ddae82
@ -41,5 +41,13 @@ class Helpers extends BaseHelpers
|
|||||||
$this->add('uppercase', new String\UppercaseHelper());
|
$this->add('uppercase', new String\UppercaseHelper());
|
||||||
$this->add('repeat', new String\RepeatHelper());
|
$this->add('repeat', new String\RepeatHelper());
|
||||||
$this->add('truncate', new String\TruncateHelper());
|
$this->add('truncate', new String\TruncateHelper());
|
||||||
|
|
||||||
|
// Layout helpers
|
||||||
|
$storage = new Layout\BlockStorage();
|
||||||
|
$this->add('block', new Layout\BlockHelper($storage));
|
||||||
|
$this->add('extends', new Layout\ExtendsHelper($storage));
|
||||||
|
$this->add('override', new Layout\OverrideHelper($storage));
|
||||||
|
$this->add('ifOverridden', new Layout\IfOverriddenHelper($storage));
|
||||||
|
$this->add('unlessOverridden', new Layout\UnlessOverriddenHelper($storage));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
src/Layout/AbstractBlockHelper.php
Normal file
34
src/Layout/AbstractBlockHelper.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains base functionality for all helpers related with blocks overriding.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
abstract class AbstractBlockHelper
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var BlockStorage
|
||||||
|
*/
|
||||||
|
protected $blocksStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper's constructor.
|
||||||
|
*
|
||||||
|
* @param BlockStorage $storage A Blocks context instance
|
||||||
|
*/
|
||||||
|
public function __construct(BlockStorage $storage)
|
||||||
|
{
|
||||||
|
$this->blocksStorage = $storage;
|
||||||
|
}
|
||||||
|
}
|
58
src/Layout/BlockHelper.php
Normal file
58
src/Layout/BlockHelper.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
use Handlebars\Context;
|
||||||
|
use Handlebars\Helper as HelperInterface;
|
||||||
|
use Handlebars\Template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A helper for defining default content of a block.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```handlebars
|
||||||
|
* {{#block name}}
|
||||||
|
* Default content for the block
|
||||||
|
* {{/block}}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Arguments:
|
||||||
|
* - "name": Name of the block.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class BlockHelper extends AbstractBlockHelper implements HelperInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function execute(Template $template, Context $context, $args, $source)
|
||||||
|
{
|
||||||
|
// Get block name
|
||||||
|
$parsed_args = $template->parseArguments($args);
|
||||||
|
if (count($parsed_args) != 1) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'"block" helper expects exactly one argument.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$block_name = $context->get(array_shift($parsed_args));
|
||||||
|
|
||||||
|
// If the block is not overridden render and show the default value
|
||||||
|
if (!$this->blocksStorage->has($block_name)) {
|
||||||
|
return $template->render($context);
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $this->blocksStorage->get($block_name);
|
||||||
|
|
||||||
|
// Show overridden content
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
}
|
78
src/Layout/BlockStorage.php
Normal file
78
src/Layout/BlockStorage.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A storage for overridable blocks' content.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class BlockStorage
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Associative array of blocks. The keys are blocks names and the values are
|
||||||
|
* blocks content.
|
||||||
|
*
|
||||||
|
* @type string[]
|
||||||
|
*/
|
||||||
|
protected $blocks = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets content of a block.
|
||||||
|
*
|
||||||
|
* @param string $name Block's name.
|
||||||
|
* @return string Block's content.
|
||||||
|
*/
|
||||||
|
public function get($name)
|
||||||
|
{
|
||||||
|
return isset($this->blocks[$name]) ? $this->blocks[$name] : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets content of a block.
|
||||||
|
*
|
||||||
|
* @param string $name Block's name.
|
||||||
|
* @param string $content Block's content.
|
||||||
|
*/
|
||||||
|
public function set($name, $content)
|
||||||
|
{
|
||||||
|
$this->blocks[$name] = $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a block exists in the storage.
|
||||||
|
*
|
||||||
|
* @param string $name Block's name
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function has($name)
|
||||||
|
{
|
||||||
|
return isset($this->blocks[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes block from the storage.
|
||||||
|
*
|
||||||
|
* @param string $name Block's name.
|
||||||
|
*/
|
||||||
|
public function remove($name)
|
||||||
|
{
|
||||||
|
unset($this->blocks[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all blocks from the storage.
|
||||||
|
*/
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->blocks = array();
|
||||||
|
}
|
||||||
|
}
|
79
src/Layout/ExtendsHelper.php
Normal file
79
src/Layout/ExtendsHelper.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
use Handlebars\Context;
|
||||||
|
use Handlebars\Helper as HelperInterface;
|
||||||
|
use Handlebars\Template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A helper for templates extending.
|
||||||
|
*
|
||||||
|
* This helper works as a wrapper for one or more "override" blocks which
|
||||||
|
* defines name of the parent template.
|
||||||
|
*
|
||||||
|
* Example of usage:
|
||||||
|
* ```handlebars
|
||||||
|
* {{#extends parentTemplateName}}
|
||||||
|
* {{#override "blockName"}}
|
||||||
|
* Overridden block content
|
||||||
|
* {{/override}}
|
||||||
|
* {{/extends}}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Arguments:
|
||||||
|
* - "parentTemplateName": Name of the template that should be extended.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class ExtendsHelper extends AbstractBlockHelper implements HelperInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The current inheritance level of the templates.
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $level = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function execute(Template $template, Context $context, $args, $source)
|
||||||
|
{
|
||||||
|
// Get name of the parent template
|
||||||
|
$parsed_args = $template->parseArguments($args);
|
||||||
|
if (count($parsed_args) != 1) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'"extends" helper expects exactly one argument.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$parent_template = $context->get(array_shift($parsed_args));
|
||||||
|
|
||||||
|
// Render content inside "extends" block to override blocks
|
||||||
|
$template->render($context);
|
||||||
|
|
||||||
|
// We need another instance of \Handlebars\Template to render parent
|
||||||
|
// template. It can be got from Handlebars engine, so get the engine.
|
||||||
|
$handlebars = $template->getEngine();
|
||||||
|
|
||||||
|
// Render the parent template
|
||||||
|
$this->level++;
|
||||||
|
$buffer = $handlebars->render($parent_template, $context);
|
||||||
|
$this->level--;
|
||||||
|
|
||||||
|
if ($this->level == 0) {
|
||||||
|
// The template and all its parents are rendered. Clean up the
|
||||||
|
// storage.
|
||||||
|
$this->blocksStorage->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $buffer;
|
||||||
|
}
|
||||||
|
}
|
36
src/Layout/Helpers.php
Normal file
36
src/Layout/Helpers.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
use Handlebars\Helpers as BaseHelpers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all layout helpers.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class Helpers extends BaseHelpers
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function addDefaultHelpers()
|
||||||
|
{
|
||||||
|
parent::addDefaultHelpers();
|
||||||
|
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$this->add('block', new BlockHelper($storage));
|
||||||
|
$this->add('extends', new ExtendsHelper($storage));
|
||||||
|
$this->add('override', new OverrideHelper($storage));
|
||||||
|
$this->add('ifOverridden', new IfOverriddenHelper($storage));
|
||||||
|
$this->add('unlessOverridden', new UnlessOverriddenHelper($storage));
|
||||||
|
}
|
||||||
|
}
|
64
src/Layout/IfOverriddenHelper.php
Normal file
64
src/Layout/IfOverriddenHelper.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
use Handlebars\Context;
|
||||||
|
use Handlebars\Helper as HelperInterface;
|
||||||
|
use Handlebars\Template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conditional helper that checks if block overridden or not.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```handlebars
|
||||||
|
* {{#ifOverridden name}}
|
||||||
|
* The block was overridden
|
||||||
|
* {{else}}
|
||||||
|
* The block was not overridden
|
||||||
|
* {{/ifOverridden}}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Arguments:
|
||||||
|
* - "name": Name of the block.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class IfOverriddenHelper extends AbstractBlockHelper implements HelperInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function execute(Template $template, Context $context, $args, $source)
|
||||||
|
{
|
||||||
|
// Get block name
|
||||||
|
$parsed_args = $template->parseArguments($args);
|
||||||
|
if (count($parsed_args) != 1) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'"ifOverridden" helper expects exactly one argument.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$block_name = $context->get(array_shift($parsed_args));
|
||||||
|
|
||||||
|
// Check condition and render blocks
|
||||||
|
if ($this->blocksStorage->has($block_name)) {
|
||||||
|
$template->setStopToken('else');
|
||||||
|
$buffer = $template->render($context);
|
||||||
|
$template->setStopToken(false);
|
||||||
|
} else {
|
||||||
|
$template->setStopToken('else');
|
||||||
|
$template->discard();
|
||||||
|
$template->setStopToken(false);
|
||||||
|
$buffer = $template->render($context);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $buffer;
|
||||||
|
}
|
||||||
|
}
|
59
src/Layout/OverrideHelper.php
Normal file
59
src/Layout/OverrideHelper.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
use Handlebars\Context;
|
||||||
|
use Handlebars\Helper as HelperInterface;
|
||||||
|
use Handlebars\Template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A helper for overriding content of a block.
|
||||||
|
*
|
||||||
|
* One or more "override" blocks must be wrapped with
|
||||||
|
* {@link \JustBlackBird\HandlebarsHelpers\Layout\ExtendsHelper} helper.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```handlebars
|
||||||
|
* {{#override blockName}}
|
||||||
|
* Overridden content of the block.
|
||||||
|
* {{/override}}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Arguments:
|
||||||
|
* - blockName: Name of the block which should be overridden.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class OverrideHelper extends AbstractBlockHelper implements HelperInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function execute(Template $template, Context $context, $args, $source)
|
||||||
|
{
|
||||||
|
// Get block name
|
||||||
|
$parsed_args = $template->parseArguments($args);
|
||||||
|
if (count($parsed_args) != 1) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'"override" helper expects exactly one argument.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$block_name = $context->get(array_shift($parsed_args));
|
||||||
|
|
||||||
|
// We need to provide unlimited inheritence level. Rendering is started
|
||||||
|
// from the deepest level template. If the content is in the block
|
||||||
|
// storage it is related with the deepest level template. Thus we do not
|
||||||
|
// need to override it.
|
||||||
|
if (!$this->blocksStorage->has($block_name)) {
|
||||||
|
$this->blocksStorage->set($block_name, $template->render($context));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
64
src/Layout/UnlessOverriddenHelper.php
Normal file
64
src/Layout/UnlessOverriddenHelper.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Layout;
|
||||||
|
|
||||||
|
use Handlebars\Context;
|
||||||
|
use Handlebars\Helper as HelperInterface;
|
||||||
|
use Handlebars\Template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Conditional helper that checks if block overridden or not.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```handlebars
|
||||||
|
* {{#unlessOverridden name}}
|
||||||
|
* The block was not overridden
|
||||||
|
* {{else}}
|
||||||
|
* The block was overridden
|
||||||
|
* {{/unlessOverridden}}
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Arguments:
|
||||||
|
* - "name": Name of the block.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class UnlessOverriddenHelper extends AbstractBlockHelper implements HelperInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function execute(Template $template, Context $context, $args, $source)
|
||||||
|
{
|
||||||
|
// Get block name
|
||||||
|
$parsed_args = $template->parseArguments($args);
|
||||||
|
if (count($parsed_args) != 1) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'"unlessOverridden" helper expects exactly one argument.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$block_name = $context->get(array_shift($parsed_args));
|
||||||
|
|
||||||
|
// Check condition and render blocks
|
||||||
|
if (!$this->blocksStorage->has($block_name)) {
|
||||||
|
$template->setStopToken('else');
|
||||||
|
$buffer = $template->render($context);
|
||||||
|
$template->setStopToken(false);
|
||||||
|
} else {
|
||||||
|
$template->setStopToken('else');
|
||||||
|
$template->discard();
|
||||||
|
$template->setStopToken(false);
|
||||||
|
$buffer = $template->render($context);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $buffer;
|
||||||
|
}
|
||||||
|
}
|
@ -53,6 +53,13 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
|
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
|
||||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
|
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
|
||||||
|
|
||||||
|
// Layout helpers
|
||||||
|
array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'),
|
||||||
|
array('extends', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\ExtendsHelper'),
|
||||||
|
array('override', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\OverrideHelper'),
|
||||||
|
array('ifOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\IfOverriddenHelper'),
|
||||||
|
array('unlessOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\UnlessOverriddenHelper'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
50
tests/Layout/BlockHelperTest.php
Normal file
50
tests/Layout/BlockHelperTest.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockHelper;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for "block" helper.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class BlockHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that exception is thrown if wrong number of arguments is used.
|
||||||
|
*
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @dataProvider wrongArgumentsProvider
|
||||||
|
*/
|
||||||
|
public function testArgumentsCount($template)
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array('block' => new BlockHelper($storage)));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
$engine->render($template, array());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testArgumentsCount method.
|
||||||
|
*/
|
||||||
|
public function wrongArgumentsProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
// Not enough arguments
|
||||||
|
array('{{#block}}content{{/block}}'),
|
||||||
|
// Too much arguments
|
||||||
|
array('{{#block "arg1" "arg2"}}content{{/block}}'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
42
tests/Layout/BlockStorageTest.php
Normal file
42
tests/Layout/BlockStorageTest.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for BlockStorage.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class BlockStorageTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests basic operations with storage.
|
||||||
|
*/
|
||||||
|
public function testBasicOperations()
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$this->assertFalse($storage->has('test'));
|
||||||
|
$storage->set('test', 'content');
|
||||||
|
$this->assertTrue($storage->has('test'));
|
||||||
|
$this->assertEquals($storage->get('test'), 'content');
|
||||||
|
$storage->set('test', 'another content');
|
||||||
|
$this->assertEquals($storage->get('test'), 'another content');
|
||||||
|
$storage->remove('test');
|
||||||
|
$this->assertFalse($storage->has('test'));
|
||||||
|
$storage->set('test', 'content');
|
||||||
|
$storage->set('another_test', 'content');
|
||||||
|
$storage->clear();
|
||||||
|
$this->assertFalse($storage->has('test'));
|
||||||
|
$this->assertFalse($storage->has('another_test'));
|
||||||
|
}
|
||||||
|
}
|
50
tests/Layout/ExtendsHelperTest.php
Normal file
50
tests/Layout/ExtendsHelperTest.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\ExtendsHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for "extends" helper.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class ExtendsHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that exception is thrown if wrong number of arguments is used.
|
||||||
|
*
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @dataProvider wrongArgumentsProvider
|
||||||
|
*/
|
||||||
|
public function testArgumentsCount($template)
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array('extends' => new ExtendsHelper($storage)));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
$engine->render($template, array());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testArgumentsCount method.
|
||||||
|
*/
|
||||||
|
public function wrongArgumentsProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
// Not enough arguments
|
||||||
|
array('{{#extends}}content{{/extends}}'),
|
||||||
|
// Too much arguments
|
||||||
|
array('{{#extends "arg1" "arg2"}}content{{/extends}}'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
48
tests/Layout/HelpersTest.php
Normal file
48
tests/Layout/HelpersTest.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\Helpers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for Layout Helpers Set.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that all helpers in the set exist and have valid classes.
|
||||||
|
*
|
||||||
|
* @dataProvider helpersProvider
|
||||||
|
*/
|
||||||
|
public function testHelper($name, $class)
|
||||||
|
{
|
||||||
|
$helpers = new Helpers();
|
||||||
|
|
||||||
|
$this->assertTrue($helpers->has($name), sprintf('There is no "%s" helper', $name));
|
||||||
|
$this->assertInstanceOf($class, $helpers->{$name});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testHelper method.
|
||||||
|
*/
|
||||||
|
public function helpersProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'),
|
||||||
|
array('extends', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\ExtendsHelper'),
|
||||||
|
array('override', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\OverrideHelper'),
|
||||||
|
array('ifOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\IfOverriddenHelper'),
|
||||||
|
array('unlessOverridden', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\UnlessOverriddenHelper'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
50
tests/Layout/IfOverriddenHelperTest.php
Normal file
50
tests/Layout/IfOverriddenHelperTest.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\IfOverriddenHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for "ifOverridden" helper.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class IfOverriddenHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that exception is thrown if wrong number of arguments is used.
|
||||||
|
*
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @dataProvider wrongArgumentsProvider
|
||||||
|
*/
|
||||||
|
public function testArgumentsCount($template)
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array('ifOverridden' => new IfOverriddenHelper($storage)));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
$engine->render($template, array());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testArgumentsCount method.
|
||||||
|
*/
|
||||||
|
public function wrongArgumentsProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
// Not enough arguments
|
||||||
|
array('{{#ifOverridden}}true{{else}}false{{/ifOverridden}}'),
|
||||||
|
// Too much arguments
|
||||||
|
array('{{#ifOverridden "arg1" "arg2"}}true{{else}}false{{/ifOverridden}}'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
98
tests/Layout/IntegrationTest.php
Normal file
98
tests/Layout/IntegrationTest.php
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockHelper;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\ExtendsHelper;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\IfOverriddenHelper;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\OverrideHelper;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\UnlessOverriddenHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for all layout helpers.
|
||||||
|
*
|
||||||
|
* Layout helpers must work together thus combined tests should be used.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class IntegrationTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests how inheritance helpers works together.
|
||||||
|
*/
|
||||||
|
public function testInheritance()
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array(
|
||||||
|
'block' => new BlockHelper($storage),
|
||||||
|
'extends' => new ExtendsHelper($storage),
|
||||||
|
'override' => new OverrideHelper($storage),
|
||||||
|
));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
// Test simple inheritance
|
||||||
|
$engine->setLoader(new \Handlebars\Loader\ArrayLoader(array(
|
||||||
|
'parent' => '{{#block "name"}}parent{{/block}} template',
|
||||||
|
'child' => '{{#extends "parent"}}{{#override "name"}}child{{/override}}{{/extends}}',
|
||||||
|
'grandchild' => '{{#extends "child"}}{{#override "name"}}grandchild{{/override}}{{/extends}}',
|
||||||
|
)));
|
||||||
|
$this->assertEquals($engine->render('parent', array()), 'parent template');
|
||||||
|
$this->assertEquals($engine->render('child', array()), 'child template');
|
||||||
|
$this->assertEquals($engine->render('grandchild', array()), 'grandchild template');
|
||||||
|
|
||||||
|
// Test inheritance with nested blocks
|
||||||
|
$engine->setLoader(new \Handlebars\Loader\ArrayLoader(array(
|
||||||
|
'parent' => '{{#block "title"}}{{#block "name"}}parent{{/block}} template{{/block}}',
|
||||||
|
'child' => '{{#extends "parent"}}{{#override "name"}}child{{/override}}{{/extends}}',
|
||||||
|
'newbie' => '{{#extends "parent"}}{{#override "title"}}Newbie!{{/override}}{{/extends}}',
|
||||||
|
)));
|
||||||
|
$this->assertEquals($engine->render('parent', array()), 'parent template');
|
||||||
|
$this->assertEquals($engine->render('child', array()), 'child template');
|
||||||
|
$this->assertEquals($engine->render('newbie', array()), 'Newbie!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that conditions related with inheritance works as expected.
|
||||||
|
*/
|
||||||
|
public function testConditions()
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array(
|
||||||
|
'block' => new BlockHelper($storage),
|
||||||
|
'extends' => new ExtendsHelper($storage),
|
||||||
|
'override' => new OverrideHelper($storage),
|
||||||
|
'ifOverridden' => new IfOverriddenHelper($storage),
|
||||||
|
'unlessOverridden' => new UnlessOverriddenHelper($storage),
|
||||||
|
));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
// Test "ifOverridden" helper
|
||||||
|
$engine->setLoader(new \Handlebars\Loader\ArrayLoader(array(
|
||||||
|
'parent' => '{{#block "name"}}{{/block}}{{#ifOverridden "name"}}true{{else}}false{{/ifOverridden}}',
|
||||||
|
'child' => '{{#extends "parent"}}{{#override "name"}}{{/override}}{{/extends}}',
|
||||||
|
'another_child' => '{{#extends "parent"}}{{/extends}}',
|
||||||
|
)));
|
||||||
|
$this->assertEquals($engine->render('parent', array()), 'false');
|
||||||
|
$this->assertEquals($engine->render('child', array()), 'true');
|
||||||
|
$this->assertEquals($engine->render('another_child', array()), 'false');
|
||||||
|
|
||||||
|
// Test "unlessOverridden" helper
|
||||||
|
$engine->setLoader(new \Handlebars\Loader\ArrayLoader(array(
|
||||||
|
'parent' => '{{#block "name"}}{{/block}}{{#unlessOverridden "name"}}false{{else}}true{{/unlessOverridden}}',
|
||||||
|
'child' => '{{#extends "parent"}}{{#override "name"}}{{/override}}{{/extends}}',
|
||||||
|
'another_child' => '{{#extends "parent"}}{{/extends}}',
|
||||||
|
)));
|
||||||
|
$this->assertEquals($engine->render('parent', array()), 'false');
|
||||||
|
$this->assertEquals($engine->render('child', array()), 'true');
|
||||||
|
$this->assertEquals($engine->render('another_child', array()), 'false');
|
||||||
|
}
|
||||||
|
}
|
50
tests/Layout/OverrideHelperTest.php
Normal file
50
tests/Layout/OverrideHelperTest.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\OverrideHelper;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for "override" helper.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class OverrideHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that exception is thrown if wrong number of arguments is used.
|
||||||
|
*
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @dataProvider wrongArgumentsProvider
|
||||||
|
*/
|
||||||
|
public function testArgumentsCount($template)
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array('override' => new OverrideHelper($storage)));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
$engine->render($template, array());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testArgumentsCount method.
|
||||||
|
*/
|
||||||
|
public function wrongArgumentsProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
// Not enough arguments
|
||||||
|
array('{{#override}}content{{/override}}'),
|
||||||
|
// Too much arguments
|
||||||
|
array('{{#override "arg1" "arg2"}}content{{/override}}'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
50
tests/Layout/UnlessOverriddenHelperTest.php
Normal file
50
tests/Layout/UnlessOverriddenHelperTest.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Handlebars.php Helpers Set
|
||||||
|
*
|
||||||
|
* (c) Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace JustBlackBird\HandlebarsHelpers\Tests\Layout;
|
||||||
|
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\BlockStorage;
|
||||||
|
use JustBlackBird\HandlebarsHelpers\Layout\UnlessOverriddenHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for "unlessOverridden" helper.
|
||||||
|
*
|
||||||
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||||
|
*/
|
||||||
|
class UnlessOverriddenHelperTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests that exception is thrown if wrong number of arguments is used.
|
||||||
|
*
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
* @dataProvider wrongArgumentsProvider
|
||||||
|
*/
|
||||||
|
public function testArgumentsCount($template)
|
||||||
|
{
|
||||||
|
$storage = new BlockStorage();
|
||||||
|
$helpers = new \Handlebars\Helpers(array('unlessOverridden' => new UnlessOverriddenHelper($storage)));
|
||||||
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||||
|
|
||||||
|
$engine->render($template, array());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data provider for testArgumentsCount method.
|
||||||
|
*/
|
||||||
|
public function wrongArgumentsProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
// Not enough arguments
|
||||||
|
array('{{#unlessOverridden}}false{{else}}true{{/unlessOverridden}}'),
|
||||||
|
// Too much arguments
|
||||||
|
array('{{#unlessOverridden "arg1" "arg2"}}false{{else}}true{{/unlessOverridden}}'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user