TimeHelperTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }
  47. /**
  48. * Tests that calling a CakeTime method works.
  49. *
  50. * @return void
  51. */
  52. public function testTimeAgoInWords() {
  53. $res = $this->Time->timeAgoInWords(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR));
  54. $this->assertNotEmpty($res);
  55. }
  56. /**
  57. * @return void
  58. */
  59. public function testPublished() {
  60. $result = $this->Time->published((new DateTime(date(FORMAT_DB_DATETIME)))->addSeconds(1));
  61. $expected = 'class="published today';
  62. $this->assertStringContainsString($expected, $result);
  63. $result = $this->Time->published((new Date(date(FORMAT_DB_DATETIME)))->addDays(1));
  64. $expected = 'class="published notyet';
  65. $this->assertStringContainsString($expected, $result);
  66. $result = $this->Time->published((new DateTime(date(FORMAT_DB_DATETIME)))->subDays(2));
  67. $expected = 'class="published already';
  68. $this->assertStringContainsString($expected, $result);
  69. $result = $this->Time->published(new DateTime('2012-02-03 14:12:10'));
  70. $this->assertStringContainsString('03.02.2012', $result);
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testTimezones() {
  76. $result = $this->Time->timezones();
  77. $this->assertTrue(!empty($result));
  78. }
  79. /**
  80. * TearDown method
  81. *
  82. * @return void
  83. */
  84. public function tearDown(): void {
  85. parent::tearDown();
  86. unset($this->Time);
  87. }
  88. }