TimeHelperTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\I18n\Date;
  4. use Cake\View\View;
  5. use Shim\TestSuite\TestCase;
  6. use Tools\I18n\DateTime;
  7. use Tools\View\Helper\TimeHelper;
  8. /**
  9. * Datetime Test Case
  10. */
  11. class TimeHelperTest extends TestCase {
  12. /**
  13. * @var \Tools\View\Helper\TimeHelper|\Tools\Utility\Time
  14. */
  15. protected $Time;
  16. /**
  17. * @return void
  18. */
  19. public function setUp(): void {
  20. parent::setUp();
  21. $this->Time = new TimeHelper(new View(null));
  22. }
  23. /**
  24. * Test calling Utility.Number class
  25. *
  26. * @return void
  27. */
  28. public function testParentCall() {
  29. $result = $this->Time->age((date('Y') - 15) . '-01-01');
  30. $this->assertSame(15, $result);
  31. }
  32. /**
  33. * Test user age
  34. *
  35. * @return void
  36. */
  37. public function testUserAge() {
  38. $res = $this->Time->userAge((date('Y') - 4) . '-01-01');
  39. $this->assertTrue($res >= 3 && $res <= 5);
  40. $res = $this->Time->userAge(date('Y') . '-01-01');
  41. $this->assertSame('', $res);
  42. $res = $this->Time->userAge('1903-01-01');
  43. $this->assertSame('', $res);
  44. $res = $this->Time->userAge('1901-01-01');
  45. $this->assertSame('', $res);
  46. $res = $this->Time->userAge(new Date('1981-02-03'));
  47. $this->assertTrue($res >= 42 && $res <= 44);
  48. }
  49. /**
  50. * Tests that calling a CakeTime method works.
  51. *
  52. * @return void
  53. */
  54. public function testTimeAgoInWords() {
  55. $res = $this->Time->timeAgoInWords(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR));
  56. $this->assertNotEmpty($res);
  57. }
  58. /**
  59. * @return void
  60. */
  61. public function testPublished() {
  62. $result = $this->Time->published((new DateTime(date(FORMAT_DB_DATETIME)))->addSeconds(1));
  63. $expected = 'class="published today';
  64. $this->assertStringContainsString($expected, $result);
  65. $result = $this->Time->published((new Date(date(FORMAT_DB_DATETIME)))->addDays(1));
  66. $expected = 'class="published notyet';
  67. $this->assertStringContainsString($expected, $result);
  68. $result = $this->Time->published((new DateTime(date(FORMAT_DB_DATETIME)))->subDays(2));
  69. $expected = 'class="published already';
  70. $this->assertStringContainsString($expected, $result);
  71. $result = $this->Time->published(new DateTime('2012-02-03 14:12:10'));
  72. $this->assertStringContainsString('03.02.2012', $result);
  73. }
  74. /**
  75. * @return void
  76. */
  77. public function testTimezones() {
  78. $result = $this->Time->timezones();
  79. $this->assertTrue(!empty($result));
  80. }
  81. /**
  82. * TearDown method
  83. *
  84. * @return void
  85. */
  86. public function tearDown(): void {
  87. parent::tearDown();
  88. unset($this->Time);
  89. }
  90. }