Add ifMore helper

This commit is contained in:
Dmitriy Simushev 2016-08-16 07:35:26 +00:00
parent 1791469eda
commit ca30d602f8
4 changed files with 139 additions and 0 deletions

View File

@ -31,6 +31,7 @@ class Helpers extends BaseHelpers
$this->add('ifEven', new IfEvenHelper());
$this->add('ifOdd', new IfOddHelper());
$this->add('ifLess', new IfLessHelper());
$this->add('ifMore', new IfMoreHelper());
$this->add('unlessEqual', new UnlessEqualHelper());
}
}

View 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]);
}
}

View File

@ -43,6 +43,7 @@ class HelpersTest extends \PHPUnit_Framework_TestCase
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('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
);
}

View 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}}'),
);
}
}