TimeHelperTest.php 2.2 KB

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