Initial commit

This commit is contained in:
Dmitriy Simushev 2014-12-12 14:47:27 +00:00
commit e341787d48
21 changed files with 1081 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# Exclude dependencies
vendor
# Exclude composer's files
composer.phar
composer.lock

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

46
README.md Normal file
View File

@ -0,0 +1,46 @@
# Handlebars.php Helpers Set
Provides a set of helpers for [Handlebars.php](https://github.com/XaminProject/handlebars.php) template engine.
## Installation
Simply add a dependency on `justblackbird/handlebars.php-helpers` to your
project's `composer.json` file if you use [Composer](http://getcomposer.org/) to
manage the dependencies of your project.
## Usage
To use all helpers in your templates just create an instance of helpers set and
attach it to Handlebars engine.
```php
$helpers = new \JustBlackBird\HandlebarsHelpers\Helpers();
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
```
Want to use only subset of helpers? Fine. Just create an instance of appropriate
helpers set and attach it to Handlebars engine. Here is an example for Date
helpers:
```php
$helpers = new \JustBlackBird\HandlebarsHelpers\Date\Helpers();
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
```
Want to use only chosen helpers? No problem. Just add them manually to your
helpers set:
```php
$engine = new \Handlebars\Handlebars();
$engine->getHelpers()->add(
'ifEqual',
new \JustBlackBird\HandlebarsHelpers\Comparison\IfEqualHelper()
);
```
## License
[MIT](http://opensource.org/licenses/MIT) (c) Dmitriy Simushev

29
composer.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "justblackbird/handlebars.php-helpers",
"version": "0.1.0",
"description": "A set of helpers for Handlebars.php template engine.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Dmitriy Simushev",
"email": "simushevds@gmail.com"
}
],
"support": {
"issues": "https://github.com/JustBlackBird/handlebars.php-helpers/issues",
"source": "https://github.com/JustBlackBird/handlebars.php-helpers"
},
"require": {
"xamin/handlebars.php": "0.10.*"
},
"require-dev": {
"phpunit/phpunit": "~4.4",
"squizlabs/php_codesniffer": "~2.0"
},
"autoload": {
"psr-4": {
"JustBlackBird\\HandlebarsHelpers\\": "src/"
}
}
}

9
phpunit.xml.dist Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Handlebars.php Helpers Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

View 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\Comparison;
use Handlebars\Helpers as BaseHelpers;
/**
* Contains all comparison related helpers.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class Helpers extends BaseHelpers
{
/**
* {@inheritdoc}
*/
protected function addDefaultHelpers()
{
parent::addDefaultHelpers();
$this->add('ifEqual', new IfEqualHelper());
$this->add('ifEven', new IfEvenHelper());
$this->add('ifOdd', new IfOddHelper());
$this->add('unlessEqual', new UnlessEqualHelper());
}
}

View File

@ -0,0 +1,60 @@
<?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\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
/**
* Conditional helper that checks if two values are equal or not.
*
* Example of usage:
* ```handlebars
* {{#ifEqual first second}}
* The first argument is equal to the second one.
* {{else}}
* The arguments are not equal.
* {{/ifEqual}}
* ```
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfEqualHelper implements HelperInterface
{
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$parsed_args = $template->parseArguments($args);
if (count($parsed_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;
}
}

View File

@ -0,0 +1,60 @@
<?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\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
/**
* Conditional helper that checks if specified argument is even or not.
*
* Example of usage:
* ```handlebars
* {{#ifEven value}}
* The value is even.
* {{else}}
* The value is odd.
* {{/ifEven}}
* ```
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfEvenHelper 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(
'"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;
}
}

View File

@ -0,0 +1,60 @@
<?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\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
/**
* Conditional helper that checks if specified argument is odd or not.
*
* Example of usage:
* ```handlebars
* {{#ifOdd value}}
* The value is odd.
* {{else}}
* The value is even.
* {{/ifOdd}}
* ```
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfOddHelper 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(
'"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;
}
}

View File

@ -0,0 +1,60 @@
<?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\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
/**
* Conditional helper that checks if two values are equal or not.
*
* Example of usage:
* ```handlebars
* {{#unlessEqual first second}}
* The first argument is equal to the second one.
* {{else}}
* The arguments are not equal.
* {{/unlessEqual}}
* ```
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class UnlessEqualHelper implements HelperInterface
{
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$parsed_args = $template->parseArguments($args);
if (count($parsed_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;
}
}

View 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\Date;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
/**
* Format date using PHP's strftime format string.
*
* Usage:
* ```handlebars
* {{formatDate time format}}
* ```
*
* Arguments:
* - "time": Can be either an integer timestamp or an instance of \DateTime
* class.
* - "format": Format string. See
* {@link http://php.net/manual/en/function.strftime.php} for details about
* placeholders.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class FormatDateHelper implements HelperInterface
{
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$parsed_args = $template->parseArguments($args);
if (count($parsed_args) != 2) {
throw new \InvalidArgumentException(
'"formatDate" helper expects exactly two arguments.'
);
}
$raw_time = $context->get($parsed_args[0]);
if ($raw_time instanceof \DateTime) {
$timestamp = $raw_time->getTimestamp();
} else {
$timestamp = intval($raw_time);
}
$format = $context->get($parsed_args[1]);
return strftime($format, $timestamp);
}
}

31
src/Date/Helpers.php Normal file
View File

@ -0,0 +1,31 @@
<?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\Date;
use Handlebars\Helpers as BaseHelpers;
/**
* Contains all date related helpers.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class Helpers extends BaseHelpers
{
/**
* {@inheritdoc}
*/
protected function addDefaultHelpers()
{
parent::addDefaultHelpers();
$this->add('formatDate', new FormatDateHelper());
}
}

38
src/Helpers.php Normal file
View File

@ -0,0 +1,38 @@
<?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;
use Handlebars\Helpers as BaseHelpers;
/**
* Contains all helpers.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class Helpers extends BaseHelpers
{
/**
* {@inheritdoc}
*/
protected function addDefaultHelpers()
{
parent::addDefaultHelpers();
// Date helpers
$this->add('formatDate', new Date\FormatDateHelper());
// Comparison helpers
$this->add('ifEqual', new Comparison\IfEqualHelper());
$this->add('ifEven', new Comparison\IfEvenHelper());
$this->add('ifOdd', new Comparison\IfOddHelper());
$this->add('unlessEqual', new Comparison\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\Tests\Comparison;
use JustBlackBird\HandlebarsHelpers\Comparison\Helpers;
/**
* Test class for Comparison 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('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'),
array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
);
}
}

View File

@ -0,0 +1,85 @@
<?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\IfEqualHelper;
/**
* Test class for "ifEqual" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfEqualHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests conditions work as expected.
*
* @dataProvider conditionProvider
*/
public function testCondition($left, $right, $is_equal)
{
$helpers = new \Handlebars\Helpers(array('ifEqual' => new IfEqualHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#ifEqual left right}}true{{else}}false{{/ifEqual}}',
array('left' => $left, 'right' => $right)
),
$is_equal ? 'true' : 'false'
);
}
/**
* A data provider for testCondition method.
*/
public function conditionProvider()
{
return array(
// Same values
array(123, 123, true),
// Equal values but with different types
array(123, "123", true),
// One more type convertion check
array(0, false, true),
// Different values
array(123, false, 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('ifEqual' => new IfEqualHelper()));
$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('{{#ifEqual}}yes{{else}}no{{/ifEqual}}'),
array('{{#ifEqual 5}}yes{{else}}no{{/ifEqual}}'),
// Too much arguments
array('{{#ifEqual 2 4 8}}yes{{else}}no{{/ifEqual}}'),
);
}
}

View File

@ -0,0 +1,86 @@
<?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\IfEvenHelper;
/**
* Test class for "ifEven" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfEventHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests conditions work as expected.
*
* @dataProvider conditionProvider
*/
public function testCondition($value, $is_even)
{
$helpers = new \Handlebars\Helpers(array('ifEven' => new IfEvenHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#ifEven value}}true{{else}}false{{/ifEven}}',
array('value' => $value)
),
$is_even ? 'true' : 'false'
);
}
/**
* A data provider for testCondition method.
*/
public function conditionProvider()
{
return array(
// Even values but with different types
array(2, true),
array("8", true),
// Zero is even number
array(0, true),
// Null should be treated as zero so it's an even value too.
array(null, true),
// Odd values with different types
array(1, false),
array("17", 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('ifEven' => new IfEvenHelper()));
$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('{{#ifEven}}yes{{else}}no{{/ifEven}}'),
// Too much arguments
array('{{#ifEven 2 4}}yes{{else}}no{{/ifEven}}'),
);
}
}

View File

@ -0,0 +1,86 @@
<?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\IfOddHelper;
/**
* Test class for "ifOdd" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class IfOddHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests conditions work as expected.
*
* @dataProvider conditionProvider
*/
public function testCondition($value, $is_even)
{
$helpers = new \Handlebars\Helpers(array('ifOdd' => new IfOddHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#ifOdd value}}false{{else}}true{{/ifOdd}}',
array('value' => $value)
),
$is_even ? 'true' : 'false'
);
}
/**
* A data provider for testCondition method.
*/
public function conditionProvider()
{
return array(
// Even values but with different types
array(2, true),
array("8", true),
// Zero is even number
array(0, true),
// Null should be treated as zero so it's an even value too.
array(null, true),
// Odd values with different types
array(1, false),
array("17", 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('ifOdd' => new IfOddHelper()));
$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('{{#ifOdd}}yes{{else}}no{{/ifOdd}}'),
// Too much arguments
array('{{#ifOdd 2 4}}yes{{else}}no{{/ifOdd}}'),
);
}
}

View File

@ -0,0 +1,84 @@
<?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\UnlessEqualHelper;
/**
* Test class for "unlessEqual" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class UnlessEqualHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests conditions work as expected.
*
* @dataProvider conditionProvider
*/
public function testCondition($left, $right, $is_equal)
{
$helpers = new \Handlebars\Helpers(array('unlessEqual' => new UnlessEqualHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals(
$engine->render(
'{{#unlessEqual left right}}false{{else}}true{{/unlessEqual}}',
array('left' => $left, 'right' => $right)
),
$is_equal ? 'true' : 'false'
);
}
/**
* A data provider for testCondition method.
*/
public function conditionProvider()
{
return array(
// Same values
array(123, 123, true),
// Equal values but with different types
array(123, "123", true),
// One more type convertion check
array(0, false, true),
// Different values
array(123, false, 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('unlessEqual' => new UnlessEqualHelper()));
$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('{{#unlessEqual}}no{{else}}yes{{/unlessEqual}}'),
// Too much arguments
array('{{#unlessEqual 2 4 8}}no{{else}}yes{{/unlessEqual}}'),
);
}
}

View File

@ -0,0 +1,86 @@
<?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\Date;
use JustBlackBird\HandlebarsHelpers\Date\FormatDateHelper;
/**
* Test class for "formatDate" helper.
*
* @author Dmitriy Simushev <simushevds@gmail.com>
*/
class FormatDateHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests that date is formatted properly.
*
* @dataProvider formatProvider
*/
public function testFormat($time, $format, $result)
{
$helpers = new \Handlebars\Helpers(array('formatDate' => new FormatDateHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertEquals($engine->render(
'{{formatDate time format}}',
array(
'time' => $time,
'format' => $format,
)
), $result);
}
/**
* A data provider for testFormat method.
*/
public function formatProvider()
{
$now = new \DateTime();
$format = "%H:%M %d-%m-%Y";
$expected = strftime($format, $now->getTimestamp());
return array(
// DateTime object
array($now, $format, $expected),
// Integer timestamp
array($now->getTimestamp(), $format, $expected),
// String timestamp
array((string)$now->getTimestamp(), $format, $expected),
);
}
/**
* 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('formatDate' => new FormatDateHelper()));
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$engine->render($template, array());
}
/**
* A data provider for FormatDateHelperTest::testArgumentsCount() test.
*/
public function wrongArgumentsProvider()
{
return array(
// Not enough arguments
array('{{formatDate 658983600}}'),
// Too much arguments
array('{{formatDate 658983600 "%F" "test"}}'),
);
}
}

View File

@ -0,0 +1,44 @@
<?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\Date;
use JustBlackBird\HandlebarsHelpers\Date\Helpers;
/**
* Test class for Date 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('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'),
);
}
}

51
tests/HelpersTest.php Normal file
View File

@ -0,0 +1,51 @@
<?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;
use JustBlackBird\HandlebarsHelpers\Helpers;
/**
* Test class for Global 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(
// Date helpers
array('formatDate', '\\JustBlackBird\\HandlebarsHelpers\\Date\\FormatDateHelper'),
// Comparison helpers
array('ifEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEqualHelper'),
array('ifEven', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfEvenHelper'),
array('ifOdd', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\IfOddHelper'),
array('unlessEqual', '\\JustBlackBird\\HandlebarsHelpers\\Comparison\\UnlessEqualHelper'),
);
}
}