TimeHelperTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use Cake\I18n\DateTime;
  18. use Cake\I18n\I18n;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\View\Helper\TimeHelper;
  21. use Cake\View\View;
  22. use DateTime as NativeDateTime;
  23. use DateTimeZone;
  24. use IntlDateFormatter;
  25. /**
  26. * TimeHelperTest class
  27. */
  28. class TimeHelperTest extends TestCase
  29. {
  30. /**
  31. * @var \Cake\View\Helper\TimeHelper
  32. */
  33. protected $Time;
  34. /**
  35. * @var \Cake\View\View
  36. */
  37. protected $View;
  38. /**
  39. * setUp method
  40. */
  41. public function setUp(): void
  42. {
  43. parent::setUp();
  44. $this->View = new View();
  45. $this->Time = new TimeHelper($this->View);
  46. }
  47. /**
  48. * tearDown method
  49. */
  50. public function tearDown(): void
  51. {
  52. parent::tearDown();
  53. DateTime::setDefaultLocale(null);
  54. I18n::setLocale(I18n::getDefaultLocale());
  55. }
  56. /**
  57. * Test element wrapping in timeAgoInWords
  58. */
  59. public function testTimeAgoInWords(): void
  60. {
  61. $Time = new TimeHelper($this->View);
  62. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  63. $result = $Time->timeAgoInWords($timestamp, [
  64. 'end' => '1 years',
  65. 'element' => 'span',
  66. ]);
  67. $expected = [
  68. 'span' => [
  69. 'title' => $timestamp,
  70. 'class' => 'time-ago-in-words',
  71. ],
  72. 'on ' . date('n/j/y', $timestamp),
  73. '/span',
  74. ];
  75. $this->assertHtml($expected, $result);
  76. $result = $Time->timeAgoInWords($timestamp, [
  77. 'end' => '1 years',
  78. 'element' => [
  79. 'title' => 'testing',
  80. 'rel' => 'test',
  81. ],
  82. ]);
  83. $expected = [
  84. 'span' => [
  85. 'title' => 'testing',
  86. 'class' => 'time-ago-in-words',
  87. 'rel' => 'test',
  88. ],
  89. 'on ' . date('n/j/y', $timestamp),
  90. '/span',
  91. ];
  92. $this->assertHtml($expected, $result);
  93. $timestamp = strtotime('+2 weeks');
  94. $result = $Time->timeAgoInWords(
  95. $timestamp,
  96. ['end' => '1 years', 'element' => 'div']
  97. );
  98. $expected = [
  99. 'div' => [
  100. 'title' => $timestamp,
  101. 'class' => 'time-ago-in-words',
  102. ],
  103. '2 weeks',
  104. '/div',
  105. ];
  106. $this->assertHtml($expected, $result);
  107. }
  108. /**
  109. * Test output timezone with timeAgoInWords
  110. */
  111. public function testTimeAgoInWordsOutputTimezone(): void
  112. {
  113. $Time = new TimeHelper($this->View, ['outputTimezone' => 'America/Vancouver']);
  114. $timestamp = new DateTime('+8 years, +4 months +2 weeks +3 days');
  115. $result = $Time->timeAgoInWords($timestamp, [
  116. 'end' => '1 years',
  117. 'element' => 'span',
  118. ]);
  119. $vancouver = clone $timestamp;
  120. $vancouver = $vancouver->setTimezone('America/Vancouver');
  121. $expected = [
  122. 'span' => [
  123. 'title' => $vancouver->__toString(),
  124. 'class' => 'time-ago-in-words',
  125. ],
  126. 'on ' . $vancouver->format('n/j/y'),
  127. '/span',
  128. ];
  129. $this->assertHtml($expected, $result);
  130. }
  131. /**
  132. * testToQuarter method
  133. */
  134. public function testToQuarter(): void
  135. {
  136. $this->assertSame(4, $this->Time->toQuarter('2007-12-25'));
  137. $this->assertEquals(['2007-10-01', '2007-12-31'], $this->Time->toQuarter('2007-12-25', true));
  138. }
  139. /**
  140. * testNice method
  141. */
  142. public function testNice(): void
  143. {
  144. $time = '2014-04-20 20:00';
  145. $result = $this->Time->nice($time);
  146. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  147. $this->assertTimeFormat('Apr 20, 2014, 8:00 PM', $result);
  148. $result = $this->Time->nice($time, 'America/New_York');
  149. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  150. $this->assertTimeFormat('Apr 20, 2014, 4:00 PM', $result);
  151. }
  152. /**
  153. * test nice with outputTimezone
  154. */
  155. public function testNiceOutputTimezone(): void
  156. {
  157. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  158. $result = $this->Time->nice('2014-04-20 20:00');
  159. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  160. $this->assertTimeFormat('Apr 20, 2014, 1:00 PM', $result);
  161. }
  162. /**
  163. * testToUnix method
  164. */
  165. public function testToUnix(): void
  166. {
  167. $this->assertSame('1397980800', $this->Time->toUnix('2014-04-20 08:00:00'));
  168. }
  169. /**
  170. * testToAtom method
  171. */
  172. public function testToAtom(): void
  173. {
  174. $dateTime = new NativeDateTime();
  175. $this->assertSame($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
  176. }
  177. /**
  178. * testToAtom method
  179. */
  180. public function testToAtomOutputTimezone(): void
  181. {
  182. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  183. $dateTime = new DateTime();
  184. $vancouver = clone $dateTime;
  185. $vancouver = $vancouver->setTimezone('America/Vancouver');
  186. $this->assertSame($vancouver->format(DATE_ATOM), $this->Time->toAtom($vancouver));
  187. }
  188. /**
  189. * testToRss method
  190. */
  191. public function testToRss(): void
  192. {
  193. $date = '2012-08-12 12:12:45';
  194. $time = strtotime($date);
  195. $this->assertSame(date('r', $time), $this->Time->toRss($time));
  196. $timezones = ['Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu'];
  197. foreach ($timezones as $timezone) {
  198. $yourTimezone = new DateTimeZone($timezone);
  199. $yourTime = new NativeDateTime($date, $yourTimezone);
  200. $time = $yourTime->format('U');
  201. $this->assertSame($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
  202. }
  203. }
  204. /**
  205. * test toRss with outputTimezone
  206. */
  207. public function testToRssOutputTimezone(): void
  208. {
  209. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  210. $dateTime = new DateTime();
  211. $vancouver = clone $dateTime;
  212. $vancouver = $vancouver->setTimezone('America/Vancouver');
  213. $this->assertSame($vancouver->format('r'), $this->Time->toRss($vancouver));
  214. }
  215. /**
  216. * testOfGmt method
  217. */
  218. public function testGmt(): void
  219. {
  220. $this->assertSame('1397980800', $this->Time->gmt('2014-04-20 08:00:00'));
  221. }
  222. /**
  223. * testIsToday method
  224. */
  225. public function testIsToday(): void
  226. {
  227. $result = $this->Time->isToday('+1 day');
  228. $this->assertFalse($result);
  229. $result = $this->Time->isToday('+1 days');
  230. $this->assertFalse($result);
  231. $result = $this->Time->isToday('+0 day');
  232. $this->assertTrue($result);
  233. $result = $this->Time->isToday('-1 day');
  234. $this->assertFalse($result);
  235. }
  236. /**
  237. * testIsFuture method
  238. */
  239. public function testIsFuture(): void
  240. {
  241. $this->assertTrue($this->Time->isFuture('+1 month'));
  242. $this->assertTrue($this->Time->isFuture('+1 days'));
  243. $this->assertTrue($this->Time->isFuture('+1 minute'));
  244. $this->assertTrue($this->Time->isFuture('+1 second'));
  245. $this->assertFalse($this->Time->isFuture('-1 second'));
  246. $this->assertFalse($this->Time->isFuture('-1 day'));
  247. $this->assertFalse($this->Time->isFuture('-1 week'));
  248. $this->assertFalse($this->Time->isFuture('-1 month'));
  249. }
  250. /**
  251. * testIsPast method
  252. */
  253. public function testIsPast(): void
  254. {
  255. $this->assertFalse($this->Time->isPast('+1 month'));
  256. $this->assertFalse($this->Time->isPast('+1 days'));
  257. $this->assertFalse($this->Time->isPast('+1 minute'));
  258. $this->assertFalse($this->Time->isPast('+1 second'));
  259. $this->assertTrue($this->Time->isPast('-1 second'));
  260. $this->assertTrue($this->Time->isPast('-1 day'));
  261. $this->assertTrue($this->Time->isPast('-1 week'));
  262. $this->assertTrue($this->Time->isPast('-1 month'));
  263. }
  264. /**
  265. * testIsThisWeek method
  266. */
  267. public function testIsThisWeek(): void
  268. {
  269. // A map of days which goes from -1 day of week to +1 day of week
  270. $map = [
  271. 'Mon' => [-1, 7], 'Tue' => [-2, 6], 'Wed' => [-3, 5],
  272. 'Thu' => [-4, 4], 'Fri' => [-5, 3], 'Sat' => [-6, 2],
  273. 'Sun' => [-7, 1],
  274. ];
  275. $days = $map[date('D')];
  276. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  277. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  278. }
  279. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  280. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  281. }
  282. /**
  283. * testIsThisMonth method
  284. */
  285. public function testIsThisMonth(): void
  286. {
  287. $result = $this->Time->isThisMonth('+0 day');
  288. $this->assertTrue($result);
  289. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, (int)date('m'), mt_rand(1, 28), (int)date('Y')));
  290. $this->assertTrue($result);
  291. $result = $this->Time->isThisMonth(mktime(0, 0, 0, (int)date('m'), mt_rand(1, 28), (int)date('Y') - mt_rand(1, 12)));
  292. $this->assertFalse($result);
  293. $result = $this->Time->isThisMonth(mktime(0, 0, 0, (int)date('m'), mt_rand(1, 28), (int)date('Y') + mt_rand(1, 12)));
  294. $this->assertFalse($result);
  295. }
  296. /**
  297. * testIsThisYear method
  298. */
  299. public function testIsThisYear(): void
  300. {
  301. $result = $this->Time->isThisYear('+0 day');
  302. $this->assertTrue($result);
  303. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), (int)date('Y')));
  304. $this->assertTrue($result);
  305. }
  306. /**
  307. * testWasYesterday method
  308. */
  309. public function testWasYesterday(): void
  310. {
  311. $result = $this->Time->wasYesterday('+1 day');
  312. $this->assertFalse($result);
  313. $result = $this->Time->wasYesterday('+1 days');
  314. $this->assertFalse($result);
  315. $result = $this->Time->wasYesterday('+0 day');
  316. $this->assertFalse($result);
  317. $result = $this->Time->wasYesterday('-1 day');
  318. $this->assertTrue($result);
  319. $result = $this->Time->wasYesterday('-1 days');
  320. $this->assertTrue($result);
  321. $result = $this->Time->wasYesterday('-2 days');
  322. $this->assertFalse($result);
  323. }
  324. /**
  325. * testIsTomorrow method
  326. */
  327. public function testIsTomorrow(): void
  328. {
  329. $result = $this->Time->isTomorrow('+1 day');
  330. $this->assertTrue($result);
  331. $result = $this->Time->isTomorrow('+1 days');
  332. $this->assertTrue($result);
  333. $result = $this->Time->isTomorrow('+0 day');
  334. $this->assertFalse($result);
  335. $result = $this->Time->isTomorrow('-1 day');
  336. $this->assertFalse($result);
  337. }
  338. /**
  339. * testWasWithinLast method
  340. */
  341. public function testWasWithinLast(): void
  342. {
  343. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  344. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  345. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  346. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  347. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  348. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-1 hour'));
  349. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  350. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  351. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  352. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  353. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  354. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  355. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  356. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  357. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  358. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  359. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  360. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  361. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  362. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  363. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  364. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  365. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  366. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  367. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  368. $this->assertTrue($this->Time->wasWithinLast('1 day', '-23 hours -59 minutes -59 seconds'));
  369. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  370. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  371. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  372. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  373. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  374. }
  375. /**
  376. * testisWithinNext method
  377. */
  378. public function testIsWithinNext(): void
  379. {
  380. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  381. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
  382. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  383. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
  384. $this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
  385. $this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
  386. $this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
  387. $this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
  388. $this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
  389. $this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
  390. $this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
  391. $this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
  392. $this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
  393. $this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
  394. $this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
  395. $this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
  396. $this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
  397. $this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
  398. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  399. $this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
  400. $this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
  401. $this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
  402. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
  403. $this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
  404. $this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
  405. $this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
  406. $this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
  407. $this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
  408. $this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
  409. $this->assertTrue($this->Time->isWithinNext('1 day', '+1 day'));
  410. $this->assertTrue($this->Time->isWithinNext('7 day', '+1 week'));
  411. $this->assertTrue($this->Time->isWithinNext('1 minute', '+1 second'));
  412. $this->assertTrue($this->Time->isWithinNext('1 month', '+1 month'));
  413. }
  414. /**
  415. * test formatting dates taking in account preferred i18n locale file
  416. */
  417. public function testFormat(): void
  418. {
  419. $time = strtotime('Thu Jan 14 13:59:28 2010');
  420. $result = $this->Time->format($time);
  421. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  422. $this->assertTimeFormat('1/14/10, 1:59 PM', $result);
  423. $result = $this->Time->format($time, IntlDateFormatter::FULL);
  424. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  425. $this->assertStringStartsWith('Thursday, January 14, 2010 at 1:59:28 PM', $result);
  426. $result = $this->Time->format($time, [IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM]);
  427. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  428. $this->assertSame('1/14/10, 1:59:28 PM', $result);
  429. $result = $this->Time->format('invalid date', null, 'Date invalid');
  430. $expected = 'Date invalid';
  431. $this->assertSame($expected, $result);
  432. I18n::setLocale('fr_FR');
  433. DateTime::setDefaultLocale('fr_FR');
  434. $time = new DateTime('Thu Jan 14 13:59:28 2010');
  435. $result = $this->Time->format($time, IntlDateFormatter::FULL);
  436. $this->assertStringContainsString('jeudi 14 janvier 2010', $result);
  437. $this->assertStringContainsString('13:59:28', $result);
  438. }
  439. /**
  440. * test format with outputTimezone
  441. */
  442. public function testFormatOutputTimezone(): void
  443. {
  444. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  445. $time = strtotime('Thu Jan 14 8:59:28 2010 UTC');
  446. $result = $this->Time->format($time);
  447. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  448. $this->assertTimeFormat('1/14/10, 12:59 AM', $result);
  449. $time = new DateTime('Thu Jan 14 8:59:28 2010', 'UTC');
  450. $result = $this->Time->format($time);
  451. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  452. $this->assertTimeFormat('1/14/10, 12:59 AM', $result);
  453. }
  454. /**
  455. * test i18nFormat with outputTimezone
  456. */
  457. public function testI18nFormatOutputTimezone(): void
  458. {
  459. $this->Time->setConfig('outputTimezone', 'America/Vancouver');
  460. $time = strtotime('Thu Jan 14 8:59:28 2010 UTC');
  461. $result = $this->Time->i18nFormat($time, [IntlDateFormatter::SHORT, IntlDateFormatter::FULL]);
  462. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  463. $this->assertStringStartsWith('1/14/10, 12:59:28 AM', $result);
  464. }
  465. /**
  466. * Test format() with a string.
  467. */
  468. public function testFormatString(): void
  469. {
  470. $time = '2010-01-14 13:59:28';
  471. $result = $this->Time->format($time);
  472. $result = preg_replace('/[\pZ\pC]/u', ' ', $result);
  473. $this->assertTimeFormat('1/14/10 1:59 PM', $result);
  474. $result = $this->Time->format($time, 'HH:mm', false, 'America/New_York');
  475. $this->assertTimeFormat('08:59', $result);
  476. }
  477. /**
  478. * Test format() with a Time instance.
  479. */
  480. public function testFormatTimeInstance(): void
  481. {
  482. $time = new DateTime('2010-01-14 13:59:28', 'America/New_York');
  483. $result = $this->Time->format($time, 'HH:mm', false, 'America/New_York');
  484. $this->assertTimeFormat('13:59', $result);
  485. $time = new DateTime('2010-01-14 13:59:28', 'UTC');
  486. $result = $this->Time->format($time, 'HH:mm', false, 'America/New_York');
  487. $this->assertTimeFormat('08:59', $result);
  488. }
  489. /**
  490. * Custom assert to allow for variation in the version of the intl library, where
  491. * some translations contain a few extra commas.
  492. *
  493. * @param string $expected
  494. * @param string $result
  495. */
  496. public function assertTimeFormat($expected, $result): void
  497. {
  498. $this->assertSame(
  499. str_replace([',', '(', ')', ' at', ' à'], '', $expected),
  500. str_replace([',', '(', ')', ' at', ' à'], '', $result)
  501. );
  502. }
  503. /**
  504. * Test formatting in case the $time parameter is not set
  505. */
  506. public function testNullDateFormat(): void
  507. {
  508. $result = $this->Time->format(null);
  509. $this->assertFalse($result);
  510. $fallback = 'Date invalid or not set';
  511. $result = $this->Time->format(null, IntlDateFormatter::FULL, $fallback);
  512. $this->assertSame($fallback, $result);
  513. }
  514. }