You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once __DIR__.'/../../../../lib/Twig/Extensions/Extension/Date.php';
  11. /**
  12. * @author Robin van der Vleuten <robinvdvleuten@gmail.com>
  13. */
  14. class Twig_Tests_Extension_DateTest extends PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var TwigEnvironment
  18. */
  19. private $env;
  20. public static function setUpBeforeClass()
  21. {
  22. if (!class_exists('Twig_Extensions_Extension_Date')) {
  23. self::markTestSkipped('Unable to find class Twig_Extensions_Extension_Date.');
  24. }
  25. }
  26. public function setUp()
  27. {
  28. $timezone = new DateTimeZone(date_default_timezone_get());
  29. $coreExtension = $this->getMock('Twig_Extension_Core');
  30. $coreExtension
  31. ->expects($this->any())
  32. ->method('getTimezone')
  33. ->will($this->returnValue($timezone));
  34. $this->env = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
  35. $this->env
  36. ->expects($this->any())
  37. ->method('getExtension')
  38. ->with('core')
  39. ->will($this->returnValue($coreExtension))
  40. ;
  41. }
  42. /**
  43. * @dataProvider getDiffTestData()
  44. */
  45. public function testDiffWithStringsFromGivenNow($expected, $translated, $date, $now)
  46. {
  47. $extension = new Twig_Extensions_Extension_Date();
  48. $this->assertEquals($expected, $extension->diff($this->env, $date, $now));
  49. }
  50. public function testDiffWithStringsFromNow()
  51. {
  52. $extension = new Twig_Extensions_Extension_Date();
  53. $this->assertRegExp('/^[0-9]+ (second|minute|hour|day|month|year)s* ago$/', $extension->diff($this->env, '24-07-2014'));
  54. }
  55. /**
  56. * @dataProvider getDiffTestData()
  57. */
  58. public function testDiffWithDateTimeFromGivenNow($expected, $translated, $date, $now)
  59. {
  60. $extension = new Twig_Extensions_Extension_Date();
  61. $this->assertEquals($expected, $extension->diff($this->env, new DateTime($date), new DateTime($now)));
  62. }
  63. public function testDiffWithDateTimeFromNow()
  64. {
  65. $extension = new Twig_Extensions_Extension_Date();
  66. $this->assertRegExp('/^[0-9]+ (second|minute|hour|day|month|year)s* ago$/', $extension->diff($this->env, new DateTime('24-07-2014')));
  67. }
  68. /**
  69. * @dataProvider getDiffTestData()
  70. */
  71. public function testDiffCanReturnTranslatableString($expected, $translated, $date, $now)
  72. {
  73. $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
  74. $translator
  75. ->expects($this->once())
  76. ->method('transChoice')
  77. ->with($translated);
  78. $extension = new Twig_Extensions_Extension_Date($translator);
  79. $extension->diff($this->env, $date, $now);
  80. }
  81. public function getDiffTestData()
  82. {
  83. return array_merge($this->getDiffAgoTestData(), $this->getDiffInTestData());
  84. }
  85. public function getDiffAgoTestData()
  86. {
  87. return array(
  88. array('1 second ago', 'diff.ago.second', '24-07-2014 17:28:01', '24-07-2014 17:28:02'),
  89. array('5 seconds ago', 'diff.ago.second', '24-07-2014 17:28:01', '24-07-2014 17:28:06'),
  90. array('1 minute ago', 'diff.ago.minute', '24-07-2014 17:28:01', '24-07-2014 17:29:01'),
  91. array('5 minutes ago', 'diff.ago.minute', '24-07-2014 17:28:01', '24-07-2014 17:33:03'),
  92. array('1 hour ago', 'diff.ago.hour', '24-07-2014 17:28:01', '24-07-2014 18:29:01'),
  93. array('9 hours ago', 'diff.ago.hour', '24-07-2014 17:28:01', '25-07-2014 02:33:03'),
  94. array('1 day ago', 'diff.ago.day', '23-07-2014', '24-07-2014'),
  95. array('5 days ago', 'diff.ago.day', '19-07-2014', '24-07-2014'),
  96. array('1 month ago', 'diff.ago.month', '23-07-2014', '24-08-2014'),
  97. array('6 months ago', 'diff.ago.month', '19-07-2014', '24-01-2015'),
  98. array('1 year ago', 'diff.ago.year', '19-07-2014', '20-08-2015'),
  99. array('3 years ago', 'diff.ago.year', '19-07-2014', '20-08-2017'),
  100. );
  101. }
  102. public function getDiffInTestData()
  103. {
  104. return array(
  105. array('in 1 second', 'diff.in.second', '24-07-2014 17:28:02', '24-07-2014 17:28:01'),
  106. array('in 5 seconds', 'diff.in.second', '24-07-2014 17:28:06', '24-07-2014 17:28:01'),
  107. array('in 1 minute', 'diff.in.minute', '24-07-2014 17:29:01', '24-07-2014 17:28:01'),
  108. array('in 5 minutes', 'diff.in.minute', '24-07-2014 17:33:03', '24-07-2014 17:28:01'),
  109. array('in 1 hour', 'diff.in.hour', '24-07-2014 18:29:01', '24-07-2014 17:28:01'),
  110. array('in 9 hours', 'diff.in.hour', '25-07-2014 02:33:03', '24-07-2014 17:28:01'),
  111. array('in 1 day', 'diff.in.day', '24-07-2014', '23-07-2014'),
  112. array('in 5 days', 'diff.in.day', '24-07-2014', '19-07-2014'),
  113. array('in 1 month', 'diff.in.month', '24-08-2014', '23-07-2014'),
  114. array('in 6 months', 'diff.in.month', '24-01-2015', '19-07-2014'),
  115. array('in 1 year', 'diff.in.year', '20-08-2015', '19-07-2014'),
  116. array('in 3 years', 'diff.in.year', '20-08-2017', '19-07-2014'),
  117. );
  118. }
  119. }