/vendor/justblackbird/handlebars.php-helpers/tests/Date/FormatDateHelperTest.php

https://gitlab.com/fabiorf/chat · PHP · 86 lines · 42 code · 9 blank · 35 comment · 0 complexity · 91a9b716a822fb44676933f0fe037b3f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Handlebars.php Helpers Set
  4. *
  5. * (c) Dmitriy Simushev <simushevds@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace JustBlackBird\HandlebarsHelpers\Tests\Date;
  11. use JustBlackBird\HandlebarsHelpers\Date\FormatDateHelper;
  12. /**
  13. * Test class for "formatDate" helper.
  14. *
  15. * @author Dmitriy Simushev <simushevds@gmail.com>
  16. */
  17. class FormatDateHelperTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Tests that date is formatted properly.
  21. *
  22. * @dataProvider formatProvider
  23. */
  24. public function testFormat($time, $format, $result)
  25. {
  26. $helpers = new \Handlebars\Helpers(array('formatDate' => new FormatDateHelper()));
  27. $engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
  28. $this->assertEquals($engine->render(
  29. '{{formatDate time format}}',
  30. array(
  31. 'time' => $time,
  32. 'format' => $format,
  33. )
  34. ), $result);
  35. }
  36. /**
  37. * A data provider for testFormat method.
  38. */
  39. public function formatProvider()
  40. {
  41. $now = new \DateTime();
  42. $format = "%H:%M %d-%m-%Y";
  43. $expected = strftime($format, $now->getTimestamp());
  44. return array(
  45. // DateTime object
  46. array($now, $format, $expected),
  47. // Integer timestamp
  48. array($now->getTimestamp(), $format, $expected),
  49. // String timestamp
  50. array((string)$now->getTimestamp(), $format, $expected),
  51. );
  52. }
  53. /**
  54. * Tests that exception is thrown if wrong number of arguments is used.
  55. *
  56. * @expectedException InvalidArgumentException
  57. * @dataProvider wrongArgumentsProvider
  58. */
  59. public function testArgumentsCount($template)
  60. {
  61. $helpers = new \Handlebars\Helpers(array('formatDate' => new FormatDateHelper()));
  62. $engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
  63. $engine->render($template, array());
  64. }
  65. /**
  66. * A data provider for FormatDateHelperTest::testArgumentsCount() test.
  67. */
  68. public function wrongArgumentsProvider()
  69. {
  70. return array(
  71. // Not enough arguments
  72. array('{{formatDate 658983600}}'),
  73. // Too much arguments
  74. array('{{formatDate 658983600 "%F" "test"}}'),
  75. );
  76. }
  77. }