public function testLocale() { $localeDetector = $this->getMock('Sonata\\IntlBundle\\Locale\\LocaleDetectorInterface'); $localeDetector->expects($this->any())->method('getLocale')->will($this->returnValue('fr')); $timezoneDetector = $this->getMock('Sonata\\IntlBundle\\Timezone\\TimezoneDetectorInterface'); $timezoneDetector->expects($this->any())->method('getTimezone')->will($this->returnValue('Europe/Paris')); $helper = new DateTimeHelper($timezoneDetector, 'UTF-8', $localeDetector); $datetimeLondon = new \DateTime(); $datetimeLondon->setDate(1981, 11, 30); $datetimeLondon->setTime(2, 0, 0); $datetimeLondon->setTimezone(new \DateTimeZone('Europe/London')); $datetimeParis = $helper->getDatetime('1981-11-30 02:00'); $this->assertEquals('Mon, 30 Nov 1981 01:00:00 +0000', $datetimeLondon->format('r')); $this->assertEquals('Mon, 30 Nov 1981 02:00:00 +0100', $datetimeParis->format('r')); // check convertor $this->assertEquals(375930000, $helper->getDatetime($datetimeParis)->format('U')); $this->assertEquals(375930000, $helper->getDatetime($datetimeLondon)->format('U')); // warning .. this use value php.ini's timezone configuration $this->assertEquals(1293708203, $helper->getDatetime('2010-12-30 12:23:23')->format('U')); $this->assertEquals(1293663600, $helper->getDatetime('2010-12-30')->format('U')); $this->assertEquals(1293708180, $helper->getDatetime('2010-12-30 12:23')->format('U')); // check default method $this->assertEquals('30 nov. 1981', $helper->formatDate($datetimeParis)); $this->assertEquals('30 déc. 2010', $helper->formatDate('2010-12-30 12:23:23')); $this->assertEquals('02:00:00', $helper->formatTime($datetimeParis)); $this->assertEquals('30 nov. 1981 02:00:00', $helper->formatDateTime($datetimeParis)); // custom format $this->assertEquals('30 novembre 1981', $helper->formatDate($datetimeParis, null, null, \IntlDateFormatter::LONG)); if (version_compare(DateTimeHelper::getICUDataVersion(), '52', '>=')) { $this->assertEquals('02:00:00 UTC+1', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG), 'ICU Version: ' . DateTimeHelper::getICUDataVersion()); } elseif (version_compare(DateTimeHelper::getICUDataVersion(), '4.8.0', '>=')) { $this->assertEquals('02:00:00 UTC+01:00', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG), 'ICU Version: ' . DateTimeHelper::getICUDataVersion()); } else { $this->assertEquals('02:00:00 HNEC', $helper->formatTime($datetimeParis, null, null, \IntlDateFormatter::LONG), 'ICU Version: ' . DateTimeHelper::getICUDataVersion()); } $this->assertEquals('30 novembre 1981 02:00', $helper->formatDateTime($datetimeParis, null, null, \IntlDateFormatter::LONG, \IntlDateFormatter::SHORT)); $this->assertEquals('30 nov. 1981 ap. J.-C.', $helper->format($datetimeParis, 'dd MMM Y G')); }
<?php if (file_exists($file = __DIR__ . '/autoload.php')) { require_once $file; } elseif (file_exists($file = __DIR__ . '/autoload.php.dist')) { require_once $file; } use Sonata\IntlBundle\Templating\Helper\DateTimeHelper; echo sprintf("ICU Version : %s\n", DateTimeHelper::getICUDataVersion()); // try to get Symfony's PHPunit Bridge $files = array_filter(array(__DIR__ . '/../../vendor/symfony/symfony/src/Symfony/Bridge/PhpUnit/bootstrap.php', __DIR__ . '/../../vendor/symfony/phpunit-bridge/bootstrap.php', __DIR__ . '/../../../../../vendor/symfony/symfony/src/Symfony/Bridge/PhpUnit/bootstrap.php', __DIR__ . '/../../../../../vendor/symfony/phpunit-bridge/bootstrap.php'), 'file_exists'); if ($files) { require_once current($files); }