]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/TemporalTest.php
spelling: cached
[friendica.git] / tests / src / Util / TemporalTest.php
index a2f76114d3472643be7035d083bad159881d009b..10e3646ae472a45251168dc633246806eca0f557 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -22,6 +22,8 @@
 namespace Friendica\Test\src\Util;
 
 use Friendica\DI;
+use Friendica\Util\Clock\FrozenClock;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Temporal;
 use PHPUnit\Framework\TestCase;
 
@@ -35,25 +37,46 @@ class TemporalTest extends TestCase
         */
        public function testGetRelativeDate()
        {
-               // "never" would should be returned
+               $clock = new FrozenClock();
+
+               // "never" should be returned
                self::assertEquals(
-                       Temporal::getRelativeDate(''),
+                       Temporal::getRelativeDate('', true, $clock),
                        DI::l10n()->t('never')
                );
 
                // Format current date/time into "MySQL" format
-               $now = date('Y-m-d H:i:s');
                self::assertEquals(
-                       Temporal::getRelativeDate($now),
+                       Temporal::getRelativeDate($clock->now()->format(DateTimeFormat::MYSQL), true, $clock),
                        DI::l10n()->t('less than a second ago')
                );
 
                // Format current date/time - 1 minute into "MySQL" format
-               $minuteAgo = date('Y-m-d H:i:s', time() - 60);
-               $format = DI::l10n()->t('%1$d %2$s ago');
+               $minuteAgo = date('Y-m-d H:i:s', $clock->now()->getTimestamp() - 60);
+               $format    = DI::l10n()->t('%1$d %2$s ago');
+
+               // Should be both equal
                self::assertEquals(
-                       Temporal::getRelativeDate($minuteAgo),
+                       Temporal::getRelativeDate($minuteAgo, true, $clock),
                        sprintf($format, 1, DI::l10n()->t('minute'))
                );
+
+               $almostAnHourAgoInterval = new \DateInterval('PT59M59S');
+               $almostAnHourAgoInterval->invert = 1;
+               $almostAnHourAgo = (clone $clock->now())->add($almostAnHourAgoInterval);
+
+               self::assertEquals(
+                       Temporal::getRelativeDate($almostAnHourAgo->format(DateTimeFormat::MYSQL), true, $clock),
+                       sprintf($format, 59, DI::l10n()->t('minutes'))
+               );
+
+               $anHourAgoInterval = new \DateInterval('PT1H');
+               $anHourAgoInterval->invert = 1;
+               $anHourAgo = (clone $clock->now())->add($anHourAgoInterval);
+
+               self::assertEquals(
+                       Temporal::getRelativeDate($anHourAgo->format(DateTimeFormat::MYSQL), true, $clock),
+                       sprintf($format, 1, DI::l10n()->t('hour'))
+               );
        }
 }