From e341787d48a0cbb0fdcf0809feb21b590dff3e06 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 12 Dec 2014 14:47:27 +0000 Subject: [PATCH] Initial commit --- .gitignore | 6 ++ LICENSE | 21 ++++++ README.md | 46 ++++++++++++ composer.json | 29 ++++++++ phpunit.xml.dist | 9 +++ src/Comparison/Helpers.php | 34 +++++++++ src/Comparison/IfEqualHelper.php | 60 +++++++++++++++ src/Comparison/IfEvenHelper.php | 60 +++++++++++++++ src/Comparison/IfOddHelper.php | 60 +++++++++++++++ src/Comparison/UnlessEqualHelper.php | 60 +++++++++++++++ src/Date/FormatDateHelper.php | 58 +++++++++++++++ src/Date/Helpers.php | 31 ++++++++ src/Helpers.php | 38 ++++++++++ tests/Comparison/HelpersTest.php | 47 ++++++++++++ tests/Comparison/IfEqualHelperTest.php | 85 +++++++++++++++++++++ tests/Comparison/IfEvenHelperTest.php | 86 ++++++++++++++++++++++ tests/Comparison/IfOddHelperTest.php | 86 ++++++++++++++++++++++ tests/Comparison/UnlessEqualHelperTest.php | 84 +++++++++++++++++++++ tests/Date/FormatDateHelperTest.php | 86 ++++++++++++++++++++++ tests/Date/HelpersTest.php | 44 +++++++++++ tests/HelpersTest.php | 51 +++++++++++++ 21 files changed, 1081 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Comparison/Helpers.php create mode 100644 src/Comparison/IfEqualHelper.php create mode 100644 src/Comparison/IfEvenHelper.php create mode 100644 src/Comparison/IfOddHelper.php create mode 100644 src/Comparison/UnlessEqualHelper.php create mode 100644 src/Date/FormatDateHelper.php create mode 100644 src/Date/Helpers.php create mode 100644 src/Helpers.php create mode 100644 tests/Comparison/HelpersTest.php create mode 100644 tests/Comparison/IfEqualHelperTest.php create mode 100644 tests/Comparison/IfEvenHelperTest.php create mode 100644 tests/Comparison/IfOddHelperTest.php create mode 100644 tests/Comparison/UnlessEqualHelperTest.php create mode 100644 tests/Date/FormatDateHelperTest.php create mode 100644 tests/Date/HelpersTest.php create mode 100644 tests/HelpersTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..805f85a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Exclude dependencies +vendor + +# Exclude composer's files +composer.phar +composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e803c21 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Dmitriy Simushev + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6814968 --- /dev/null +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f937dba --- /dev/null +++ b/composer.json @@ -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/" + } + } +} \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..c3fee78 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,9 @@ + + + + + + tests + + + diff --git a/src/Comparison/Helpers.php b/src/Comparison/Helpers.php new file mode 100644 index 0000000..efe85b9 --- /dev/null +++ b/src/Comparison/Helpers.php @@ -0,0 +1,34 @@ + + * + * 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 + */ +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()); + } +} diff --git a/src/Comparison/IfEqualHelper.php b/src/Comparison/IfEqualHelper.php new file mode 100644 index 0000000..df2686d --- /dev/null +++ b/src/Comparison/IfEqualHelper.php @@ -0,0 +1,60 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Comparison/IfEvenHelper.php b/src/Comparison/IfEvenHelper.php new file mode 100644 index 0000000..a5ff5b2 --- /dev/null +++ b/src/Comparison/IfEvenHelper.php @@ -0,0 +1,60 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Comparison/IfOddHelper.php b/src/Comparison/IfOddHelper.php new file mode 100644 index 0000000..0af991f --- /dev/null +++ b/src/Comparison/IfOddHelper.php @@ -0,0 +1,60 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Comparison/UnlessEqualHelper.php b/src/Comparison/UnlessEqualHelper.php new file mode 100644 index 0000000..e5cea91 --- /dev/null +++ b/src/Comparison/UnlessEqualHelper.php @@ -0,0 +1,60 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Date/FormatDateHelper.php b/src/Date/FormatDateHelper.php new file mode 100644 index 0000000..d953632 --- /dev/null +++ b/src/Date/FormatDateHelper.php @@ -0,0 +1,58 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Date/Helpers.php b/src/Date/Helpers.php new file mode 100644 index 0000000..c175d49 --- /dev/null +++ b/src/Date/Helpers.php @@ -0,0 +1,31 @@ + + * + * 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 + */ +class Helpers extends BaseHelpers +{ + /** + * {@inheritdoc} + */ + protected function addDefaultHelpers() + { + parent::addDefaultHelpers(); + + $this->add('formatDate', new FormatDateHelper()); + } +} diff --git a/src/Helpers.php b/src/Helpers.php new file mode 100644 index 0000000..182c60d --- /dev/null +++ b/src/Helpers.php @@ -0,0 +1,38 @@ + + * + * 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 + */ +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()); + } +} diff --git a/tests/Comparison/HelpersTest.php b/tests/Comparison/HelpersTest.php new file mode 100644 index 0000000..5510019 --- /dev/null +++ b/tests/Comparison/HelpersTest.php @@ -0,0 +1,47 @@ + + * + * 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 + */ +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'), + ); + } +} diff --git a/tests/Comparison/IfEqualHelperTest.php b/tests/Comparison/IfEqualHelperTest.php new file mode 100644 index 0000000..bab2c5a --- /dev/null +++ b/tests/Comparison/IfEqualHelperTest.php @@ -0,0 +1,85 @@ + + * + * 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 + */ +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}}'), + ); + } +} diff --git a/tests/Comparison/IfEvenHelperTest.php b/tests/Comparison/IfEvenHelperTest.php new file mode 100644 index 0000000..40185e5 --- /dev/null +++ b/tests/Comparison/IfEvenHelperTest.php @@ -0,0 +1,86 @@ + + * + * 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 + */ +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}}'), + ); + } +} diff --git a/tests/Comparison/IfOddHelperTest.php b/tests/Comparison/IfOddHelperTest.php new file mode 100644 index 0000000..b4f12d6 --- /dev/null +++ b/tests/Comparison/IfOddHelperTest.php @@ -0,0 +1,86 @@ + + * + * 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 + */ +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}}'), + ); + } +} diff --git a/tests/Comparison/UnlessEqualHelperTest.php b/tests/Comparison/UnlessEqualHelperTest.php new file mode 100644 index 0000000..a5c06a1 --- /dev/null +++ b/tests/Comparison/UnlessEqualHelperTest.php @@ -0,0 +1,84 @@ + + * + * 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 + */ +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}}'), + ); + } +} diff --git a/tests/Date/FormatDateHelperTest.php b/tests/Date/FormatDateHelperTest.php new file mode 100644 index 0000000..fafa4ce --- /dev/null +++ b/tests/Date/FormatDateHelperTest.php @@ -0,0 +1,86 @@ + + * + * 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 + */ +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"}}'), + ); + } +} diff --git a/tests/Date/HelpersTest.php b/tests/Date/HelpersTest.php new file mode 100644 index 0000000..125a65f --- /dev/null +++ b/tests/Date/HelpersTest.php @@ -0,0 +1,44 @@ + + * + * 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 + */ +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'), + ); + } +} diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php new file mode 100644 index 0000000..bedcfaa --- /dev/null +++ b/tests/HelpersTest.php @@ -0,0 +1,51 @@ + + * + * 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 + */ +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'), + ); + } +}