mirror of
https://github.com/Mibew/handlebars.php-helpers.git
synced 2025-03-14 09:44:11 +03:00
Merge pull request #1 from JustBlackBird/master
Make the fork up to date with the main repo
This commit is contained in:
commit
6d965edfba
@ -3,6 +3,8 @@ php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- hhvm
|
||||
install: composer install
|
||||
script: "./vendor/bin/phpunit && ./vendor/bin/phpcs --standard=PSR2 -n src/"
|
||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Dmitriy Simushev <simushevds@gmail.com>
|
||||
Copyright (c) 2014 - 2016 Dmitriy Simushev <simushevds@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "justblackbird/handlebars.php-helpers",
|
||||
"version": "0.2.1",
|
||||
"version": "1.2.0",
|
||||
"description": "A set of helpers for Handlebars.php template engine.",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
@ -15,7 +15,7 @@
|
||||
"source": "https://github.com/JustBlackBird/handlebars.php-helpers"
|
||||
},
|
||||
"require": {
|
||||
"xamin/handlebars.php": "0.10.*"
|
||||
"xamin/handlebars.php": "~0.10.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.4",
|
||||
|
52
src/Collection/CountHelper.php
Normal file
52
src/Collection/CountHelper.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?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\Collection;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Returns count of items of the collection.
|
||||
*
|
||||
* Usage:
|
||||
* ```handlebars
|
||||
* {{count collection}}
|
||||
* ```
|
||||
*
|
||||
* Arguments:
|
||||
* - "collection": an array or an instance of \Countable which elements should
|
||||
* be counted.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class CountHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"last" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$collection = $context->get($parsed_args[0]);
|
||||
if (!is_array($collection) && !($collection instanceof \Countable)) {
|
||||
throw new \InvalidArgumentException('Wrong type of the argument in the "count" helper.');
|
||||
}
|
||||
|
||||
return count($collection);
|
||||
}
|
||||
}
|
65
src/Collection/FirstHelper.php
Normal file
65
src/Collection/FirstHelper.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?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\Collection;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Returns the first item of the collection.
|
||||
*
|
||||
* If the passed in collection is empty boolean false will be returned.
|
||||
*
|
||||
* Usage:
|
||||
* ```handlebars
|
||||
* {{first collection}}
|
||||
* ```
|
||||
*
|
||||
* Arguments:
|
||||
* - "collection": an array or an instance of \Traversable which first element
|
||||
* should be returned.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class FirstHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"first" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$collection = $context->get($parsed_args[0]);
|
||||
if (!is_array($collection) && !($collection instanceof \Traversable)) {
|
||||
throw new \InvalidArgumentException('Wrong type of the argument in the "first" helper.');
|
||||
}
|
||||
|
||||
if (is_array($collection)) {
|
||||
return reset($collection);
|
||||
}
|
||||
|
||||
// "reset" function does not work with \Traversable in HHVM. Thus we
|
||||
// need to get the element manually.
|
||||
while ($collection instanceof \IteratorAggregate) {
|
||||
$collection = $collection->getIterator();
|
||||
}
|
||||
$collection->rewind();
|
||||
|
||||
return $collection->valid() ? $collection->current() : false;
|
||||
}
|
||||
}
|
33
src/Collection/Helpers.php
Normal file
33
src/Collection/Helpers.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?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\Collection;
|
||||
|
||||
use Handlebars\Helpers as BaseHelpers;
|
||||
|
||||
/**
|
||||
* Contains all collections related helpers.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class Helpers extends BaseHelpers
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function addDefaultHelpers()
|
||||
{
|
||||
parent::addDefaultHelpers();
|
||||
|
||||
$this->add('count', new CountHelper());
|
||||
$this->add('first', new FirstHelper());
|
||||
$this->add('last', new LastHelper());
|
||||
}
|
||||
}
|
71
src/Collection/LastHelper.php
Normal file
71
src/Collection/LastHelper.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?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\Collection;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Returns the last item of the collection.
|
||||
*
|
||||
* If the passed in collection is empty boolean false will be returned.
|
||||
*
|
||||
* Usage:
|
||||
* ```handlebars
|
||||
* {{last collection}}
|
||||
* ```
|
||||
*
|
||||
* Arguments:
|
||||
* - "collection": an array or an instance of \Traversable which last element
|
||||
* should be returned.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class LastHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"last" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$collection = $context->get($parsed_args[0]);
|
||||
if (!is_array($collection) && !($collection instanceof \Traversable)) {
|
||||
throw new \InvalidArgumentException('Wrong type of the argument in the "last" helper.');
|
||||
}
|
||||
|
||||
if (is_array($collection)) {
|
||||
return end($collection);
|
||||
}
|
||||
|
||||
// "end" function does not work with \Traversable in HHVM. Thus we
|
||||
// need to get the element manually.
|
||||
while ($collection instanceof \IteratorAggregate) {
|
||||
$collection = $collection->getIterator();
|
||||
}
|
||||
|
||||
$collection->rewind();
|
||||
$item = false;
|
||||
while ($collection->valid()) {
|
||||
$item = $collection->current();
|
||||
$collection->next();
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
49
src/Comparison/AbstractComparisonHelper.php
Normal file
49
src/Comparison/AbstractComparisonHelper.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Contains base functionality for all helpers related with comparison.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
abstract class AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$resolved_args = array();
|
||||
foreach ($template->parseArguments($args) as $arg) {
|
||||
$resolved_args[] = $context->get($arg);
|
||||
}
|
||||
|
||||
if ($this->evaluateCondition($resolved_args)) {
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates condition based on helper's arguments.
|
||||
*
|
||||
* This is a template method which must be overriden in inherited helpers.
|
||||
*
|
||||
* @param array $args List of resolved arguments passed to the helper.
|
||||
* @return bool Indicates if the condition is true or false.
|
||||
*/
|
||||
abstract protected function evaluateCondition($args);
|
||||
}
|
@ -30,6 +30,12 @@ class Helpers extends BaseHelpers
|
||||
$this->add('ifEqual', new IfEqualHelper());
|
||||
$this->add('ifEven', new IfEvenHelper());
|
||||
$this->add('ifOdd', new IfOddHelper());
|
||||
$this->add('ifLess', new IfLessHelper());
|
||||
$this->add('ifMore', new IfMoreHelper());
|
||||
$this->add('ifBetween', new IfBetweenHelper());
|
||||
$this->add('ifBetweenClosed', new IfBetweenClosedHelper());
|
||||
$this->add('ifBetweenLeftClosed', new IfBetweenLeftClosedHelper());
|
||||
$this->add('ifBetweenRightClosed', new IfBetweenRightClosedHelper());
|
||||
$this->add('unlessEqual', new UnlessEqualHelper());
|
||||
}
|
||||
}
|
||||
|
@ -10,10 +10,8 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\String;
|
||||
use Handlebars\Template;
|
||||
use Handlebars\StringWrapper;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if at least one argumet can be treated as
|
||||
@ -30,48 +28,33 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfAnyHelper implements HelperInterface
|
||||
class IfAnyHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) == 0) {
|
||||
if (count($args) == 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifAny" helper expects at least one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = false;
|
||||
foreach ($parsed_args as $parsed_arg) {
|
||||
$value = $context->get($parsed_arg);
|
||||
|
||||
if ($value instanceof String) {
|
||||
// Casting any object of \Handlebars\String will have false
|
||||
// positive result even for those with empty internal strings.
|
||||
// Thus we need to check internal string of such objects.
|
||||
foreach ($args as $value) {
|
||||
if ($value instanceof StringWrapper) {
|
||||
// Casting any object of \Handlebars\StringWrapper will have
|
||||
// false positive result even for those with empty internal
|
||||
// strings. Thus we need to check internal string of such
|
||||
// objects.
|
||||
$value = $value->getString();
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
$condition = true;
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$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;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
48
src/Comparison/IfBetweenClosedHelper.php
Normal file
48
src/Comparison/IfBetweenClosedHelper.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\Comparison;
|
||||
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if one value is between two another values.
|
||||
*
|
||||
* Borders of the interval are included into comparison. That's why in cases
|
||||
* where the value under comparision is equal to one of the borders the result
|
||||
* of "between closed" operator is true.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifBetweenClosed value leftBorder rightBorder}}
|
||||
* The first argument is between borders.
|
||||
* {{else}}
|
||||
* The first argument is not between the borders.
|
||||
* {{/ifBetweenClosed}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenClosedHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifBetweenClosed" helper expects exactly three arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] >= $args[1]) && ($args[0] <= $args[2]);
|
||||
}
|
||||
}
|
48
src/Comparison/IfBetweenHelper.php
Normal file
48
src/Comparison/IfBetweenHelper.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\Comparison;
|
||||
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if one value is between two another values.
|
||||
*
|
||||
* Borders of the interval are excluded from comparison. That's why in cases
|
||||
* where the value under comparision is equal to one of the borders the result
|
||||
* of "between" operator is false.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifBetween value leftBorder rightBorder}}
|
||||
* The first argument is between borders.
|
||||
* {{else}}
|
||||
* The first argument is not between the borders.
|
||||
* {{/ifBetween}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifBetween" helper expects exactly three arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] > $args[1]) && ($args[0] < $args[2]);
|
||||
}
|
||||
}
|
49
src/Comparison/IfBetweenLeftClosedHelper.php
Normal file
49
src/Comparison/IfBetweenLeftClosedHelper.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if one value is between two another values.
|
||||
*
|
||||
* Only left border of the interval is included into comparison. That's why in
|
||||
* cases where the value under comparision is equal to left border the result
|
||||
* of "between left closed" operator is true but when the value under compatison
|
||||
* is equal to right border the result of "between left closed" is false.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifBetweenLeftClosed value leftBorder rightBorder}}
|
||||
* The first argument is between borders.
|
||||
* {{else}}
|
||||
* The first argument is not between the borders.
|
||||
* {{/ifBetweenLeftClosed}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenLeftClosedHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifBetweenLeftClosed" helper expects exactly three arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] >= $args[1]) && ($args[0] < $args[2]);
|
||||
}
|
||||
}
|
49
src/Comparison/IfBetweenRightClosedHelper.php
Normal file
49
src/Comparison/IfBetweenRightClosedHelper.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if one value is between two another values.
|
||||
*
|
||||
* Only right border of the interval is included into comparison. That's why in
|
||||
* cases where the value under comparision is equal to right border the result
|
||||
* of "between left closed" operator is true but when the value under compatison
|
||||
* is equal to left border the result of "between left closed" is false.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifBetweenRightClosed value leftBorder rightBorder}}
|
||||
* The first argument is between borders.
|
||||
* {{else}}
|
||||
* The first argument is not between the borders.
|
||||
* {{/ifBetweenRightClosed}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenRightClosedHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifBetweenRightClosed" helper expects exactly three arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] > $args[1]) && ($args[0] <= $args[2]);
|
||||
}
|
||||
}
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if two values are equal or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfEqualHelper implements HelperInterface
|
||||
class IfEqualHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
if (count($args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifEqual" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
||||
|
||||
if ($condition) {
|
||||
$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;
|
||||
return ($args[0] == $args[1]);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if specified argument is even or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfEvenHelper implements HelperInterface
|
||||
class IfEvenHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
if (count($args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifEven" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) % 2 == 0);
|
||||
|
||||
if ($condition) {
|
||||
$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;
|
||||
return ($args[0] % 2 == 0);
|
||||
}
|
||||
}
|
||||
|
47
src/Comparison/IfLessHelper.php
Normal file
47
src/Comparison/IfLessHelper.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if one value is less than another one.
|
||||
*
|
||||
* By "less" strict inequality is meant. That's why in cases where two equal
|
||||
* values are compared the result of the "less" operation is false.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifLess first second}}
|
||||
* The first argument is less than the second one.
|
||||
* {{else}}
|
||||
* The first argument is more or equal to the second one.
|
||||
* {{/ifLess}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfLessHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifLess" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] < $args[1]);
|
||||
}
|
||||
}
|
47
src/Comparison/IfMoreHelper.php
Normal file
47
src/Comparison/IfMoreHelper.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if one value is more than another one.
|
||||
*
|
||||
* By "more" strict inequality is meant. That's why in cases where two equal
|
||||
* values are compared the result of the "more" operation is false.
|
||||
*
|
||||
* Example of usage:
|
||||
* ```handlebars
|
||||
* {{#ifMore first second}}
|
||||
* The first argument is more than the second one.
|
||||
* {{else}}
|
||||
* The first argument is less or equal to the second one.
|
||||
* {{/ifMore}}
|
||||
* ```
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfMoreHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
if (count($args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifMore" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
return ($args[0] > $args[1]);
|
||||
}
|
||||
}
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if specified argument is odd or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfOddHelper implements HelperInterface
|
||||
class IfOddHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
if (count($args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifOdd" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) % 2 == 1);
|
||||
|
||||
if ($condition) {
|
||||
$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;
|
||||
return ($args[0] % 2 == 1);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,7 @@
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Comparison;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if two values are equal or not.
|
||||
@ -28,33 +26,19 @@ use Handlebars\Template;
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class UnlessEqualHelper implements HelperInterface
|
||||
class UnlessEqualHelper extends AbstractComparisonHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
protected function evaluateCondition($args)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
if (count($args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"unlessEqual" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
||||
|
||||
if (!$condition) {
|
||||
$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;
|
||||
return ($args[0] != $args[1]);
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,11 @@ class Helpers extends BaseHelpers
|
||||
// Date helpers
|
||||
$this->add('formatDate', new Date\FormatDateHelper());
|
||||
|
||||
// Collection helpers
|
||||
$this->add('count', new Collection\CountHelper());
|
||||
$this->add('first', new Collection\FirstHelper());
|
||||
$this->add('last', new Collection\LastHelper());
|
||||
|
||||
// Comparison helpers
|
||||
$this->add('ifAny', new Comparison\IfAnyHelper());
|
||||
$this->add('ifEqual', new Comparison\IfEqualHelper());
|
||||
@ -36,12 +41,13 @@ class Helpers extends BaseHelpers
|
||||
$this->add('ifOdd', new Comparison\IfOddHelper());
|
||||
$this->add('unlessEqual', new Comparison\UnlessEqualHelper());
|
||||
|
||||
// String helpers
|
||||
$this->add('lowercase', new String\LowercaseHelper());
|
||||
$this->add('uppercase', new String\UppercaseHelper());
|
||||
$this->add('repeat', new String\RepeatHelper());
|
||||
$this->add('replace', new String\ReplaceHelper());
|
||||
$this->add('truncate', new String\TruncateHelper());
|
||||
// Text helpers
|
||||
$this->add('lowercase', new Text\LowercaseHelper());
|
||||
$this->add('uppercase', new Text\UppercaseHelper());
|
||||
$this->add('repeat', new Text\RepeatHelper());
|
||||
$this->add('replace', new Text\ReplaceHelper());
|
||||
$this->add('truncate', new Text\TruncateHelper());
|
||||
$this->add('ellipsis', new Text\EllipsisHelper());
|
||||
|
||||
// Layout helpers
|
||||
$storage = new Layout\BlockStorage();
|
||||
|
76
src/Text/EllipsisHelper.php
Normal file
76
src/Text/EllipsisHelper.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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\Text;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Truncates a string to specified length in words.
|
||||
*
|
||||
* Usage:
|
||||
* ```handlebars
|
||||
* {{#ellipsis string length append}}
|
||||
* ```
|
||||
*
|
||||
* Arguments:
|
||||
* - "string": A string that must be truncated.
|
||||
* - "length": A number of words to limit the string.
|
||||
* - "append": A string to append if charaters are omitted. Default value is an
|
||||
* empty string.
|
||||
*
|
||||
* @author Matteo Merola <mattmezza@gmail.com>
|
||||
*/
|
||||
class EllipsisHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) < 2 || count($parsed_args) > 3) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ellipsis" helper expects two or three arguments.'
|
||||
);
|
||||
}
|
||||
$var_content = (string)$context->get($parsed_args[0]);
|
||||
$limit = intval($context->get($parsed_args[1]));
|
||||
$ellipsis = isset($parsed_args[2]) ? (string)$context->get($parsed_args[2]) : '';
|
||||
if ($limit === 0) {
|
||||
return $ellipsis;
|
||||
}
|
||||
if ($limit < 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'The second argument of "ellipsis" helper has to be greater than or equal to 0.'
|
||||
);
|
||||
}
|
||||
$words = str_word_count($var_content, 2);
|
||||
$value = "";
|
||||
if (count($words) > $limit) {
|
||||
$permitted = array_slice($words, 0, $limit, true);
|
||||
end($permitted);
|
||||
$last_word_position = key($permitted);
|
||||
$last_word = $permitted[$last_word_position];
|
||||
$last_word_length = strlen($last_word);
|
||||
$real_limit = $last_word_position + $last_word_length;
|
||||
$value = substr($var_content, 0, $real_limit);
|
||||
} else {
|
||||
$value .= $var_content;
|
||||
}
|
||||
if ($ellipsis) {
|
||||
$value .= $ellipsis;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||
|
||||
use Handlebars\Helpers as BaseHelpers;
|
||||
|
||||
@ -31,5 +31,6 @@ class Helpers extends BaseHelpers
|
||||
$this->add('repeat', new RepeatHelper());
|
||||
$this->add('replace', new ReplaceHelper());
|
||||
$this->add('truncate', new TruncateHelper());
|
||||
$this->add('ellipsis', new EllipsisHelper());
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
@ -8,7 +8,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
@ -8,7 +8,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
@ -8,7 +8,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
@ -8,7 +8,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Text;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
111
tests/Collection/CountHelperTest.php
Normal file
111
tests/Collection/CountHelperTest.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?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\Collection;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Collection\CountHelper;
|
||||
|
||||
/**
|
||||
* Test class for "count" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class CountHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that count is calculated properly.
|
||||
*
|
||||
* @dataProvider collectionsProvider
|
||||
*/
|
||||
public function testCount($collection, $result)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('count' => new CountHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{count collection}}',
|
||||
array('collection' => $collection)
|
||||
),
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCount method.
|
||||
*/
|
||||
public function collectionsProvider()
|
||||
{
|
||||
return array(
|
||||
// Test arrays with numeric keys
|
||||
array(array('a', 'b', 'c'), '3'),
|
||||
// Test associative arrays
|
||||
array(array('a' => '10'), '1'),
|
||||
// Test \Countable instance
|
||||
array(new \ArrayIterator(array('a', 'b')), '2'),
|
||||
// Test empty collections
|
||||
array(array(), '0'),
|
||||
array(new \ArrayIterator(array()), '0'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsCountProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('count' => new CountHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render($template, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testArgumentsCount method.
|
||||
*/
|
||||
public function wrongArgumentsCountProvider()
|
||||
{
|
||||
return array(
|
||||
// Not enough arguments
|
||||
array('{{count}}'),
|
||||
// Too much arguments
|
||||
array('{{count "Arg" "ANOTHER ARG"}}'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests invalid arguments type.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider invalidArgumentsProvider
|
||||
*/
|
||||
public function testInvalidArguments($collection)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('count' => new CountHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render('{{count collection}}', array('collection' => $collection));
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testInvalidArguments method.
|
||||
*/
|
||||
public function invalidArgumentsProvider()
|
||||
{
|
||||
return array(
|
||||
array('a string'),
|
||||
array(42),
|
||||
array(new \stdClass()),
|
||||
);
|
||||
}
|
||||
}
|
114
tests/Collection/FirstHelperTest.php
Normal file
114
tests/Collection/FirstHelperTest.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?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\Collection;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Collection\FirstHelper;
|
||||
|
||||
/**
|
||||
* Test class for "first" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class FirstHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that the first item returned.
|
||||
*
|
||||
* @dataProvider collectionsProvider
|
||||
*/
|
||||
public function testFirstItem($collection, $result)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('first' => new FirstHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{first collection}}',
|
||||
array('collection' => $collection)
|
||||
),
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testFirstItem method.
|
||||
*/
|
||||
public function collectionsProvider()
|
||||
{
|
||||
return array(
|
||||
// Test arrays with numeric keys
|
||||
array(array('a', 'b', 'c'), 'a'),
|
||||
array(array('z'), 'z'),
|
||||
// Test associative arrays
|
||||
array(array('a' => '10', 'b' => '11', 'c' => '12'), '10'),
|
||||
array(array('f' => '15'), '15'),
|
||||
// Test \Traversable instance
|
||||
array(new \ArrayIterator(array('a', 'b', 'c')), 'a'),
|
||||
array(new \ArrayIterator(array('z')), 'z'),
|
||||
// Test empty collections
|
||||
array(array(), false),
|
||||
array(new \ArrayIterator(array()), false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsCountProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('first' => new FirstHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render($template, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testArgumentsCount method.
|
||||
*/
|
||||
public function wrongArgumentsCountProvider()
|
||||
{
|
||||
return array(
|
||||
// Not enough arguments
|
||||
array('{{first}}'),
|
||||
// Too much arguments
|
||||
array('{{first "Arg" "ANOTHER ARG"}}'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests invalid arguments type.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider invalidArgumentsProvider
|
||||
*/
|
||||
public function testInvalidArguments($collection)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('first' => new FirstHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render('{{first collection}}', array('collection' => $collection));
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testInvalidArguments method.
|
||||
*/
|
||||
public function invalidArgumentsProvider()
|
||||
{
|
||||
return array(
|
||||
array('a string'),
|
||||
array(42),
|
||||
array(new \stdClass()),
|
||||
);
|
||||
}
|
||||
}
|
46
tests/Collection/HelpersTest.php
Normal file
46
tests/Collection/HelpersTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?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\Collection;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Collection\Helpers;
|
||||
|
||||
/**
|
||||
* Test class for Collection 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('count', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\CountHelper'),
|
||||
array('first', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\FirstHelper'),
|
||||
array('last', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\LastHelper'),
|
||||
);
|
||||
}
|
||||
}
|
114
tests/Collection/LastHelperTest.php
Normal file
114
tests/Collection/LastHelperTest.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?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\Collection;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Collection\LastHelper;
|
||||
|
||||
/**
|
||||
* Test class for "last" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class LastHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that the last item returned.
|
||||
*
|
||||
* @dataProvider collectionsProvider
|
||||
*/
|
||||
public function testLastItem($collection, $result)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('last' => new LastHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{last collection}}',
|
||||
array('collection' => $collection)
|
||||
),
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testLastItem method.
|
||||
*/
|
||||
public function collectionsProvider()
|
||||
{
|
||||
return array(
|
||||
// Test arrays with numeric keys
|
||||
array(array('a', 'b', 'c'), 'c'),
|
||||
array(array('z'), 'z'),
|
||||
// Test associative arrays
|
||||
array(array('a' => '10', 'b' => '11', 'c' => '12'), '12'),
|
||||
array(array('f' => '15'), '15'),
|
||||
// Test \Traversable instance
|
||||
array(new \ArrayIterator(array('a', 'b', 'c')), 'c'),
|
||||
array(new \ArrayIterator(array('z')), 'z'),
|
||||
// Test empty collections
|
||||
array(array(), false),
|
||||
array(new \ArrayIterator(array()), false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsCountProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('last' => new LastHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render($template, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testArgumentsCount method.
|
||||
*/
|
||||
public function wrongArgumentsCountProvider()
|
||||
{
|
||||
return array(
|
||||
// Not enough arguments
|
||||
array('{{last}}'),
|
||||
// Too much arguments
|
||||
array('{{last "Arg" "ANOTHER ARG"}}'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests invalid arguments type.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider invalidArgumentsProvider
|
||||
*/
|
||||
public function testInvalidArguments($collection)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('last' => new LastHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render('{{last collection}}', array('collection' => $collection));
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testInvalidArguments method.
|
||||
*/
|
||||
public function invalidArgumentsProvider()
|
||||
{
|
||||
return array(
|
||||
array('a string'),
|
||||
array(42),
|
||||
array(new \stdClass()),
|
||||
);
|
||||
}
|
||||
}
|
@ -42,6 +42,12 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'),
|
||||
array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
|
||||
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
|
||||
array('ifLess', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfLessHelper'),
|
||||
array('ifMore', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfMoreHelper'),
|
||||
array('ifBetween', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenHelper'),
|
||||
array('ifBetweenClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenClosedHelper'),
|
||||
array('ifBetweenLeftClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenLeftClosedHelper'),
|
||||
array('ifBetweenRightClosed', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfBetweenRightClosedHelper'),
|
||||
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
|
||||
);
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ class IfAnyHelperTest extends \PHPUnit_Framework_TestCase
|
||||
'b' => null,
|
||||
'c' => array(),
|
||||
'd' => '',
|
||||
'e' => new \Handlebars\String(''),
|
||||
'e' => new \Handlebars\StringWrapper(''),
|
||||
),
|
||||
'false',
|
||||
),
|
||||
|
93
tests/Comparison/IfBetweenClosedHelperTest.php
Normal file
93
tests/Comparison/IfBetweenClosedHelperTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenClosedHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifBetweenClosed" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenClosedHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $left, $right, $is_between)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenClosed' => new IfBetweenClosedHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifBetweenClosed value left right}}true{{else}}false{{/ifBetweenClosed}}',
|
||||
array(
|
||||
'value' => $value,
|
||||
'left' => $left,
|
||||
'right' => $right,
|
||||
)
|
||||
),
|
||||
$is_between ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCondition method.
|
||||
*/
|
||||
public function conditionProvider()
|
||||
{
|
||||
return array(
|
||||
// The value is less than left border.
|
||||
array(2, 10, 12, false),
|
||||
// The value equals to the left border.
|
||||
array(0, 0, 42, true),
|
||||
// The value equals to the right border.
|
||||
array(9, 0, 9, true),
|
||||
// The value is more than the right border.
|
||||
array(75, 10, 12, false),
|
||||
// The value is between borders.
|
||||
array(58, 11, 134, true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenClosed' => new IfBetweenClosedHelper()));
|
||||
$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('{{#ifBetweenClosed}}yes{{else}}no{{/ifBetweenClosed}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifBetweenClosed 1}}yes{{else}}no{{/ifBetweenClosed}}'),
|
||||
array('{{#ifBetweenClosed 1 2}}yes{{else}}no{{/ifBetweenClosed}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifBetweenClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenClosed}}'),
|
||||
);
|
||||
}
|
||||
}
|
93
tests/Comparison/IfBetweenHelperTest.php
Normal file
93
tests/Comparison/IfBetweenHelperTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifBetween" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $left, $right, $is_between)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetween' => new IfBetweenHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifBetween value left right}}true{{else}}false{{/ifBetween}}',
|
||||
array(
|
||||
'value' => $value,
|
||||
'left' => $left,
|
||||
'right' => $right,
|
||||
)
|
||||
),
|
||||
$is_between ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCondition method.
|
||||
*/
|
||||
public function conditionProvider()
|
||||
{
|
||||
return array(
|
||||
// The value is less than left border.
|
||||
array(2, 10, 12, false),
|
||||
// The value equals to the left border.
|
||||
array(0, 0, 42, false),
|
||||
// The value equals to the right border.
|
||||
array(9, 0, 9, false),
|
||||
// The value is more than the right border.
|
||||
array(75, 10, 12, false),
|
||||
// The value is between borders.
|
||||
array(58, 11, 134, true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetween' => new IfBetweenHelper()));
|
||||
$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('{{#ifBetween}}yes{{else}}no{{/ifBetween}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifBetween 1}}yes{{else}}no{{/ifBetween}}'),
|
||||
array('{{#ifBetween 1 2}}yes{{else}}no{{/ifBetween}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifBetween 1 2 3 4}}yes{{else}}no{{/ifBetween}}'),
|
||||
);
|
||||
}
|
||||
}
|
93
tests/Comparison/IfBetweenLeftClosedHelperTest.php
Normal file
93
tests/Comparison/IfBetweenLeftClosedHelperTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenLeftClosedHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifBetweenLeftClosed" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenLeftClosedHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $left, $right, $is_between)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenLeftClosed' => new IfBetweenLeftClosedHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifBetweenLeftClosed value left right}}true{{else}}false{{/ifBetweenLeftClosed}}',
|
||||
array(
|
||||
'value' => $value,
|
||||
'left' => $left,
|
||||
'right' => $right,
|
||||
)
|
||||
),
|
||||
$is_between ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCondition method.
|
||||
*/
|
||||
public function conditionProvider()
|
||||
{
|
||||
return array(
|
||||
// The value is less than left border.
|
||||
array(2, 10, 12, false),
|
||||
// The value equals to the left border.
|
||||
array(0, 0, 42, true),
|
||||
// The value equals to the right border.
|
||||
array(9, 0, 9, false),
|
||||
// The value is more than the right border.
|
||||
array(75, 10, 12, false),
|
||||
// The value is between borders.
|
||||
array(58, 11, 134, true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenLeftClosed' => new IfBetweenLeftClosedHelper()));
|
||||
$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('{{#ifBetweenLeftClosed}}yes{{else}}no{{/ifBetweenLeftClosed}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifBetweenLeftClosed 1}}yes{{else}}no{{/ifBetweenLeftClosed}}'),
|
||||
array('{{#ifBetweenLeftClosed 1 2}}yes{{else}}no{{/ifBetweenLeftClosed}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifBetweenLeftClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenLeftClosed}}'),
|
||||
);
|
||||
}
|
||||
}
|
93
tests/Comparison/IfBetweenRightClosedHelperTest.php
Normal file
93
tests/Comparison/IfBetweenRightClosedHelperTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfBetweenRightClosedHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifBetweenRightClosed" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfBetweenRightClosedHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $left, $right, $is_between)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenRightClosed' => new IfBetweenRightClosedHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifBetweenRightClosed value left right}}true{{else}}false{{/ifBetweenRightClosed}}',
|
||||
array(
|
||||
'value' => $value,
|
||||
'left' => $left,
|
||||
'right' => $right,
|
||||
)
|
||||
),
|
||||
$is_between ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCondition method.
|
||||
*/
|
||||
public function conditionProvider()
|
||||
{
|
||||
return array(
|
||||
// The value is less than left border.
|
||||
array(2, 10, 12, false),
|
||||
// The value equals to the left border.
|
||||
array(0, 0, 42, false),
|
||||
// The value equals to the right border.
|
||||
array(9, 0, 9, true),
|
||||
// The value is more than the right border.
|
||||
array(75, 10, 12, false),
|
||||
// The value is between borders.
|
||||
array(58, 11, 134, true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifBetweenRightClosed' => new IfBetweenRightClosedHelper()));
|
||||
$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('{{#ifBetweenRightClosed}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifBetweenRightClosed 1}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
array('{{#ifBetweenRightClosed 1 2}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifBetweenRightClosed 1 2 3 4}}yes{{else}}no{{/ifBetweenRightClosed}}'),
|
||||
);
|
||||
}
|
||||
}
|
90
tests/Comparison/IfLessHelperTest.php
Normal file
90
tests/Comparison/IfLessHelperTest.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfLessHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifLess" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfLessHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $border, $is_less)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifLess' => new IfLessHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifLess value border}}true{{else}}false{{/ifLess}}',
|
||||
array(
|
||||
'value' => $value,
|
||||
'border' => $border,
|
||||
)
|
||||
),
|
||||
$is_less ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCondition method.
|
||||
*/
|
||||
public function conditionProvider()
|
||||
{
|
||||
return array(
|
||||
// Less values with different types.
|
||||
array(2, 10, true),
|
||||
array("8", "12", true),
|
||||
// Equal values with different types.
|
||||
array(0, 0, false),
|
||||
array("42", "42", false),
|
||||
// Greater values with different types.
|
||||
array(75, 10, false),
|
||||
array("17", "2", false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifLess' => new IfLessHelper()));
|
||||
$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('{{#ifLess}}yes{{else}}no{{/ifLess}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifLess 1}}yes{{else}}no{{/ifLess}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifLess 1 2 3}}yes{{else}}no{{/ifLess}}'),
|
||||
);
|
||||
}
|
||||
}
|
90
tests/Comparison/IfMoreHelperTest.php
Normal file
90
tests/Comparison/IfMoreHelperTest.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?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\Comparison;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Comparison\IfMoreHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ifMore" helper.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
class IfMoreHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests conditions work as expected.
|
||||
*
|
||||
* @dataProvider conditionProvider
|
||||
*/
|
||||
public function testCondition($value, $border, $is_less)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifMore' => new IfMoreHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals(
|
||||
$engine->render(
|
||||
'{{#ifMore value border}}true{{else}}false{{/ifMore}}',
|
||||
array(
|
||||
'value' => $value,
|
||||
'border' => $border,
|
||||
)
|
||||
),
|
||||
$is_less ? 'true' : 'false'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testCondition method.
|
||||
*/
|
||||
public function conditionProvider()
|
||||
{
|
||||
return array(
|
||||
// Less values with different types.
|
||||
array(3, 18, false),
|
||||
array("42", "314", false),
|
||||
// Equal values with different types.
|
||||
array(0, 0, false),
|
||||
array("42", "42", false),
|
||||
// More values with different types.
|
||||
array(89, 1, true),
|
||||
array("34", "33", true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ifMore' => new IfMoreHelper()));
|
||||
$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('{{#ifMore}}yes{{else}}no{{/ifMore}}'),
|
||||
// Still not enough arguments
|
||||
array('{{#ifMore 1}}yes{{else}}no{{/ifMore}}'),
|
||||
// Too much arguments
|
||||
array('{{#ifMore 1 2 3}}yes{{else}}no{{/ifMore}}'),
|
||||
);
|
||||
}
|
||||
}
|
@ -41,6 +41,11 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
// Date helpers
|
||||
array('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'),
|
||||
|
||||
// Collection helpers
|
||||
array('count', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\CountHelper'),
|
||||
array('first', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\FirstHelper'),
|
||||
array('last', '\\JustBlackBird\\HandlebarsHelpers\\Collection\\LastHelper'),
|
||||
|
||||
// Comparison helpers
|
||||
array('ifAny', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfAnyHelper'),
|
||||
array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'),
|
||||
@ -48,12 +53,13 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
|
||||
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
|
||||
|
||||
// String helpers
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
|
||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\String\\ReplaceHelper'),
|
||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
|
||||
// Text helpers
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\UppercaseHelper'),
|
||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
|
||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
|
||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
|
||||
array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'),
|
||||
|
||||
// Layout helpers
|
||||
array('block', '\\JustBlackBird\\HandlebarsHelpers\\Layout\\BlockHelper'),
|
||||
|
103
tests/Text/EllipsisHelperTest.php
Normal file
103
tests/Text/EllipsisHelperTest.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?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\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\Text\EllipsisHelper;
|
||||
|
||||
/**
|
||||
* Test class for "ellipsis" helper.
|
||||
*
|
||||
* @author Matteo Merola <mattmezza@gmail.com>
|
||||
*/
|
||||
class EllipsisTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that strings are repeated properly.
|
||||
*
|
||||
* @dataProvider truncateProvider
|
||||
*/
|
||||
public function testEllipsis($template, $data, $result)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$this->assertEquals($engine->render($template, $data), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testEllipsis method.
|
||||
*/
|
||||
public function truncateProvider()
|
||||
{
|
||||
return array(
|
||||
// No truncate
|
||||
array('{{ellipsis a len}}', array('a' => '123', 'len' => 5), '123'),
|
||||
// Simple truncates
|
||||
array('{{ellipsis "prova matteo ciao" 2}}', array(), 'prova matteo'),
|
||||
array('{{ellipsis "prova merola hello" 0}}', array(), ''),
|
||||
// Truncate with ellipsis
|
||||
array('{{ellipsis "prova matt" 1 "..."}}', array(), 'prova...'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if wrong number of arguments is used.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider wrongArgumentsSetProvider
|
||||
*/
|
||||
public function testArgumentsCount($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render($template, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testArgumentsCount method.
|
||||
*/
|
||||
public function wrongArgumentsSetProvider()
|
||||
{
|
||||
return array(
|
||||
// Not enough arguments
|
||||
array('{{ellipsis}}'),
|
||||
array('{{ellipsis "abc"}}'),
|
||||
// Too much arguments
|
||||
array('{{ellipsis "abc" 30 "..." "xyz"}}'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that exception is thrown if arguments are invalid.
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
* @dataProvider invalidArgumentsProvider
|
||||
*/
|
||||
public function testInvalidArguments($template)
|
||||
{
|
||||
$helpers = new \Handlebars\Helpers(array('ellipsis' => new EllipsisHelper()));
|
||||
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
||||
|
||||
$engine->render($template, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* A data provider for testInvalidArguments method.
|
||||
*/
|
||||
public function invalidArgumentsProvider()
|
||||
{
|
||||
return array(
|
||||
// Negative target length.
|
||||
array('{{ellipsis "abc" -10}}'),
|
||||
);
|
||||
}
|
||||
}
|
@ -8,12 +8,12 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\String\Helpers;
|
||||
use JustBlackBird\HandlebarsHelpers\Text\Helpers;
|
||||
|
||||
/**
|
||||
* Test class for String Helpers Set.
|
||||
* Test class for Text Helpers Set.
|
||||
*
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
*/
|
||||
@ -38,11 +38,12 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
|
||||
public function helpersProvider()
|
||||
{
|
||||
return array(
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\String\\UppercaseHelper'),
|
||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\String\\RepeatHelper'),
|
||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\String\\ReplaceHelper'),
|
||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\String\\TruncateHelper'),
|
||||
array('lowercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\LowercaseHelper'),
|
||||
array('uppercase', '\\JustBlackBird\\HandlebarsHelpers\\Text\\UppercaseHelper'),
|
||||
array('repeat', '\\JustBlackBird\\HandlebarsHelpers\\Text\\RepeatHelper'),
|
||||
array('replace', '\\JustBlackBird\\HandlebarsHelpers\\Text\\ReplaceHelper'),
|
||||
array('truncate', '\\JustBlackBird\\HandlebarsHelpers\\Text\\TruncateHelper'),
|
||||
array('ellipsis', '\\JustBlackBird\\HandlebarsHelpers\\Text\\EllipsisHelper'),
|
||||
);
|
||||
}
|
||||
}
|
@ -8,9 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\String\LowercaseHelper;
|
||||
use JustBlackBird\HandlebarsHelpers\Text\LowercaseHelper;
|
||||
|
||||
/**
|
||||
* Test class for "lowercase" helper.
|
@ -8,9 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\String\RepeatHelper;
|
||||
use JustBlackBird\HandlebarsHelpers\Text\RepeatHelper;
|
||||
|
||||
/**
|
||||
* Test class for "repeat" helper.
|
@ -8,9 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\String\ReplaceHelper;
|
||||
use JustBlackBird\HandlebarsHelpers\Text\ReplaceHelper;
|
||||
|
||||
/**
|
||||
* Test class for "replace" helper.
|
@ -8,9 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\String\TruncateHelper;
|
||||
use JustBlackBird\HandlebarsHelpers\Text\TruncateHelper;
|
||||
|
||||
/**
|
||||
* Test class for "truncate" helper.
|
@ -8,9 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\String;
|
||||
namespace JustBlackBird\HandlebarsHelpers\Tests\Text;
|
||||
|
||||
use JustBlackBird\HandlebarsHelpers\String\UppercaseHelper;
|
||||
use JustBlackBird\HandlebarsHelpers\Text\UppercaseHelper;
|
||||
|
||||
/**
|
||||
* Test class for "uppercase" helper.
|
Loading…
Reference in New Issue
Block a user