DateTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  11. * @author Robin van der Vleuten <robinvdvleuten@gmail.com>
  12. */
  13. class Twig_Tests_Extension_DateTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var TwigEnvironment
  17. */
  18. private $env;
  19. public function setUp()
  20. {
  21. $this->env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
  22. }
  23. /**
  24. * @dataProvider getDiffTestData()
  25. */
  26. public function testDiffWithStringsFromGivenNow($expected, $translated, $date, $now)
  27. {
  28. $extension = new Twig_Extensions_Extension_Date();
  29. $this->assertEquals($expected, $extension->diff($this->env, $date, $now));
  30. }
  31. public function testDiffWithStringsFromNow()
  32. {
  33. $extension = new Twig_Extensions_Extension_Date();
  34. $this->assertRegExp('/^[0-9]+ (second|minute|hour|day|month|year)s* ago$/', $extension->diff($this->env, '24-07-2014'));
  35. }
  36. /**
  37. * @dataProvider getDiffTestData()
  38. */
  39. public function testDiffWithDateTimeFromGivenNow($expected, $translated, $date, $now)
  40. {
  41. $extension = new Twig_Extensions_Extension_Date();
  42. $this->assertEquals($expected, $extension->diff($this->env, new DateTime($date), new DateTime($now)));
  43. }
  44. public function testDiffWithDateTimeFromNow()
  45. {
  46. $extension = new Twig_Extensions_Extension_Date();
  47. $this->assertRegExp('/^[0-9]+ (second|minute|hour|day|month|year)s* ago$/', $extension->diff($this->env, new DateTime('24-07-2014')));
  48. }
  49. /**
  50. * @dataProvider getDiffTestData()
  51. */
  52. public function testDiffCanReturnTranslatableString($expected, $translated, $date, $now)
  53. {
  54. $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
  55. $translator
  56. ->expects($this->once())
  57. ->method('transChoice')
  58. ->with($translated);
  59. $extension = new Twig_Extensions_Extension_Date($translator);
  60. $extension->diff($this->env, $date, $now);
  61. }
  62. public function getDiffTestData()
  63. {
  64. return array_merge($this->getDiffAgoTestData(), $this->getDiffInTestData());
  65. }
  66. public function getDiffAgoTestData()
  67. {
  68. return array(
  69. array('1 second ago', 'diff.ago.second', '24-07-2014 17:28:01', '24-07-2014 17:28:02'),
  70. array('5 seconds ago', 'diff.ago.second', '24-07-2014 17:28:01', '24-07-2014 17:28:06'),
  71. array('1 minute ago', 'diff.ago.minute', '24-07-2014 17:28:01', '24-07-2014 17:29:01'),
  72. array('5 minutes ago', 'diff.ago.minute', '24-07-2014 17:28:01', '24-07-2014 17:33:03'),
  73. array('1 hour ago', 'diff.ago.hour', '24-07-2014 17:28:01', '24-07-2014 18:29:01'),
  74. array('9 hours ago', 'diff.ago.hour', '24-07-2014 17:28:01', '25-07-2014 02:33:03'),
  75. array('1 day ago', 'diff.ago.day', '23-07-2014', '24-07-2014'),
  76. array('5 days ago', 'diff.ago.day', '19-07-2014', '24-07-2014'),
  77. array('1 month ago', 'diff.ago.month', '23-07-2014', '24-08-2014'),
  78. array('6 months ago', 'diff.ago.month', '19-07-2014', '24-01-2015'),
  79. array('1 year ago', 'diff.ago.year', '19-07-2014', '20-08-2015'),
  80. array('3 years ago', 'diff.ago.year', '19-07-2014', '20-08-2017'),
  81. );
  82. }
  83. public function getDiffInTestData()
  84. {
  85. return array(
  86. array('in 1 second', 'diff.in.second', '24-07-2014 17:28:02', '24-07-2014 17:28:01'),
  87. array('in 5 seconds', 'diff.in.second', '24-07-2014 17:28:06', '24-07-2014 17:28:01'),
  88. array('in 1 minute', 'diff.in.minute', '24-07-2014 17:29:01', '24-07-2014 17:28:01'),
  89. array('in 5 minutes', 'diff.in.minute', '24-07-2014 17:33:03', '24-07-2014 17:28:01'),
  90. array('in 1 hour', 'diff.in.hour', '24-07-2014 18:29:01', '24-07-2014 17:28:01'),
  91. array('in 9 hours', 'diff.in.hour', '25-07-2014 02:33:03', '24-07-2014 17:28:01'),
  92. array('in 1 day', 'diff.in.day', '24-07-2014', '23-07-2014'),
  93. array('in 5 days', 'diff.in.day', '24-07-2014', '19-07-2014'),
  94. array('in 1 month', 'diff.in.month', '24-08-2014', '23-07-2014'),
  95. array('in 6 months', 'diff.in.month', '24-01-2015', '19-07-2014'),
  96. array('in 1 year', 'diff.in.year', '20-08-2015', '19-07-2014'),
  97. array('in 3 years', 'diff.in.year', '20-08-2017', '19-07-2014'),
  98. );
  99. }
  100. }