From 907b1c90126714d75004c20829fe1bcbe2c5e986 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Tue, 25 Oct 2016 11:38:22 +0100 Subject: [PATCH 01/11] Type hint for \DateTimeInterface instead of \DateTime This change allows userland code to pass instances of \DateTimeImmutable as well as \DateTime by using \DateTimeInterface in type hints instead of the more strict \DateTime. --- .../Block/Widget/Grid/Column/Filter/Date.php | 2 +- .../Widget/Grid/Column/Filter/Datetime.php | 2 +- .../CatalogRule/Model/ResourceModel/Rule.php | 6 +++--- .../Entity/Attribute/Backend/Datetime.php | 2 +- .../Indexer/Model/Mview/View/State.php | 2 +- .../ResourceModel/Report/AbstractReport.php | 8 ++++---- .../Model/ResourceModel/Report/Collection.php | 20 +++++++++---------- .../ResourceModel/Report/CollectionTest.php | 8 ++++---- .../Model/ResourceModel/AbstractResource.php | 2 +- .../Adminhtml/Order/View/Tab/History.php | 2 +- .../SalesRule/Model/Coupon/Massgenerator.php | 2 +- .../Model/ResourceModel/Design/Collection.php | 2 +- .../Framework/DB/Adapter/AdapterInterface.php | 2 +- .../Framework/DB/Adapter/Pdo/Mysql.php | 6 +++--- .../Framework/Mview/View/StateInterface.php | 2 +- .../Magento/Framework/Stdlib/DateTime.php | 2 +- .../Stdlib/DateTime/DateTimeFormatter.php | 2 +- .../DateTime/DateTimeFormatterInterface.php | 2 +- .../Framework/Stdlib/DateTime/Timezone.php | 8 ++++---- .../Unit/DateTime/DateTimeFormatterTest.php | 2 +- .../Framework/View/Element/AbstractBlock.php | 2 +- 21 files changed, 43 insertions(+), 43 deletions(-) diff --git a/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Date.php b/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Date.php index 39bce723de37c..8f0b9b993554b 100644 --- a/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Date.php +++ b/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Date.php @@ -131,7 +131,7 @@ public function getHtml() public function getEscapedValue($index = null) { $value = $this->getValue($index); - if ($value instanceof \DateTime) { + if ($value instanceof \DateTimeInterface) { return $this->dateTimeFormatter->formatObject( $value, $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT) diff --git a/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Datetime.php b/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Datetime.php index fc68884351e3b..96b3471db845e 100644 --- a/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Datetime.php +++ b/app/code/Magento/Backend/Block/Widget/Grid/Column/Filter/Datetime.php @@ -147,7 +147,7 @@ public function getEscapedValue($index = null) { if ($this->getColumn()->getFilterTime()) { $value = $this->getValue($index); - if ($value instanceof \DateTime) { + if ($value instanceof \DateTimeInterface) { return $this->_localeDate->formatDateTime($value); } return $value; diff --git a/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php b/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php index 1f4c45c6754ff..4d37625533f3c 100644 --- a/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php +++ b/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php @@ -153,7 +153,7 @@ protected function _afterDelete(\Magento\Framework\Model\AbstractModel $rule) * Get catalog rules product price for specific date, website and * customer group * - * @param \DateTime $date + * @param \DateTimeInterface $date * @param int $wId * @param int $gId * @param int $pId @@ -173,13 +173,13 @@ public function getRulePrice($date, $wId, $gId, $pId) * Retrieve product prices by catalog rule for specific date, website and customer group * Collect data with product Id => price pairs * - * @param \DateTime $date + * @param \DateTimeInterface $date * @param int $websiteId * @param int $customerGroupId * @param array $productIds * @return array */ - public function getRulePrices(\DateTime $date, $websiteId, $customerGroupId, $productIds) + public function getRulePrices(\DateTimeInterface $date, $websiteId, $customerGroupId, $productIds) { $connection = $this->getConnection(); $select = $connection->select() diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php b/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php index 7b5730824bf33..dada35da063c9 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php @@ -76,7 +76,7 @@ public function formatDate($date) // unix timestamp given - simply instantiate date object if (is_scalar($date) && preg_match('/^[0-9]+$/', $date)) { $date = (new \DateTime())->setTimestamp($date); - } elseif (!($date instanceof \DateTime)) { + } elseif (!($date instanceof \DateTimeInterface)) { // normalized format expecting Y-m-d[ H:i:s] - time is optional $date = new \DateTime($date); } diff --git a/app/code/Magento/Indexer/Model/Mview/View/State.php b/app/code/Magento/Indexer/Model/Mview/View/State.php index 3a15aef3e2650..fe1a3a1e8766d 100644 --- a/app/code/Magento/Indexer/Model/Mview/View/State.php +++ b/app/code/Magento/Indexer/Model/Mview/View/State.php @@ -140,7 +140,7 @@ public function getUpdated() /** * Set state updated time * - * @param string|int|\DateTime $updated + * @param string|int|\DateTimeInterface $updated * @return $this */ public function setUpdated($updated) diff --git a/app/code/Magento/Reports/Model/ResourceModel/Report/AbstractReport.php b/app/code/Magento/Reports/Model/ResourceModel/Report/AbstractReport.php index 496655b8df60a..09df97a3c2457 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Report/AbstractReport.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Report/AbstractReport.php @@ -188,8 +188,8 @@ protected function _clearTableByDateRange( * @param string $table * @param string $column * @param string $whereColumn - * @param null|string|\DateTime $from - * @param null|string|\DateTime $to + * @param null|string|\DateTimeInterface $from + * @param null|string|\DateTimeInterface $to * @param [][] $additionalWhere * @param string $alias * @return \Magento\Framework\DB\Select @@ -440,12 +440,12 @@ protected function _getTZOffsetTransitions($timezone, $from = null, $to = null) $tzTransitions = []; try { if (!empty($from)) { - $from = $from instanceof \DateTime + $from = $from instanceof \DateTimeInterface ? $from->getTimestamp() : (new \DateTime($from))->getTimestamp(); } - $to = $to instanceof \DateTime + $to = $to instanceof \DateTimeInterface ? $to : new \DateTime($to); $nextPeriod = $this->getConnection()->formatDate( diff --git a/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php index 1b7318548ad7e..58ae28ad1f5ea 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php @@ -114,8 +114,8 @@ public function setPeriod($period) * Set interval * @codeCoverageIgnore * - * @param \DateTime $fromDate - * @param \DateTime $toDate + * @param \DateTimeInterface $fromDate + * @param \DateTimeInterface $toDate * @return $this */ public function setInterval($fromDate, $toDate) @@ -168,10 +168,10 @@ protected function _getIntervals() /** * Get interval for a day * - * @param \DateTime $dateStart + * @param \DateTimeInterface $dateStart * @return array */ - protected function _getDayInterval(\DateTime $dateStart) + protected function _getDayInterval(\DateTimeInterface $dateStart) { $interval = [ 'period' => $this->_localeDate->formatDateTime( @@ -188,12 +188,12 @@ protected function _getDayInterval(\DateTime $dateStart) /** * Get interval for a month * - * @param \DateTime $dateStart - * @param \DateTime $dateEnd + * @param \DateTimeInterface $dateStart + * @param \DateTimeInterface $dateEnd * @param bool $firstInterval * @return array */ - protected function _getMonthInterval(\DateTime $dateStart, \DateTime $dateEnd, $firstInterval) + protected function _getMonthInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, $firstInterval) { $interval = []; $interval['period'] = $dateStart->format('m/Y'); @@ -231,12 +231,12 @@ protected function _getMonthInterval(\DateTime $dateStart, \DateTime $dateEnd, $ /** * Get Interval for a year * - * @param \DateTime $dateStart - * @param \DateTime $dateEnd + * @param \DateTimeInterface $dateStart + * @param \DateTimeInterface $dateEnd * @param bool $firstInterval * @return array */ - protected function _getYearInterval(\DateTime $dateStart, \DateTime $dateEnd, $firstInterval) + protected function _getYearInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, $firstInterval) { $interval = []; $interval['period'] = $dateStart->format('Y'); diff --git a/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Report/CollectionTest.php b/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Report/CollectionTest.php index 86c8f3317f2c9..9d7db61725a9c 100644 --- a/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Report/CollectionTest.php +++ b/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Report/CollectionTest.php @@ -79,8 +79,8 @@ public function testGetStoreIds() /** * @param string $period - * @param \DateTime $fromDate - * @param \DateTime $toDate + * @param \DateTimeInterface $fromDate + * @param \DateTimeInterface $toDate * @param int $size * @dataProvider intervalsDataProvider * @return void @@ -105,8 +105,8 @@ public function testGetPageSize() /** * @param string $period - * @param \DateTime $fromDate - * @param \DateTime $toDate + * @param \DateTimeInterface $fromDate + * @param \DateTimeInterface $toDate * @param int $size * @dataProvider intervalsDataProvider * @return void diff --git a/app/code/Magento/Rule/Model/ResourceModel/AbstractResource.php b/app/code/Magento/Rule/Model/ResourceModel/AbstractResource.php index d5953f6613f8b..38ed5147ab2a8 100644 --- a/app/code/Magento/Rule/Model/ResourceModel/AbstractResource.php +++ b/app/code/Magento/Rule/Model/ResourceModel/AbstractResource.php @@ -57,7 +57,7 @@ public function _beforeSave(\Magento\Framework\Model\AbstractModel $object) private function resolveDate(\Magento\Framework\Model\AbstractModel $object, $dateIdentifier) { $date = $object->getData($dateIdentifier); - if ($date instanceof \DateTime) { + if ($date instanceof \DateTimeInterface) { $object->setData($dateIdentifier, $date->format('Y-m-d H:i:s')); } elseif (!is_string($date) || empty($date)) { $object->setData($dateIdentifier, null); diff --git a/app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php b/app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php index 48d6e127472aa..e7dfd6c6e4816 100644 --- a/app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php +++ b/app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php @@ -207,7 +207,7 @@ public function getItemComment(array $item) * * @param string $label * @param bool $notified - * @param \DateTime $created + * @param \DateTimeInterface $created * @param string $comment * @return array */ diff --git a/app/code/Magento/SalesRule/Model/Coupon/Massgenerator.php b/app/code/Magento/SalesRule/Model/Coupon/Massgenerator.php index 1a22756314d1d..a03840e7bb9be 100644 --- a/app/code/Magento/SalesRule/Model/Coupon/Massgenerator.php +++ b/app/code/Magento/SalesRule/Model/Coupon/Massgenerator.php @@ -171,7 +171,7 @@ public function generatePool() } while ($this->getResource()->exists($code)); $expirationDate = $this->getToDate(); - if ($expirationDate instanceof \DateTime) { + if ($expirationDate instanceof \DateTimeInterface) { $expirationDate = $expirationDate->format('Y-m-d H:i:s'); } diff --git a/app/code/Magento/Theme/Model/ResourceModel/Design/Collection.php b/app/code/Magento/Theme/Model/ResourceModel/Design/Collection.php index b328339af4dea..5861d5a1b339d 100644 --- a/app/code/Magento/Theme/Model/ResourceModel/Design/Collection.php +++ b/app/code/Magento/Theme/Model/ResourceModel/Design/Collection.php @@ -60,7 +60,7 @@ public function joinStore() /** * Add date filter to collection * - * @param null|int|string|\DateTime $date + * @param null|int|string|\DateTimeInterface $date * @return $this */ public function addDateFilter($date = null) diff --git a/lib/internal/Magento/Framework/DB/Adapter/AdapterInterface.php b/lib/internal/Magento/Framework/DB/Adapter/AdapterInterface.php index cb9dac42e63d4..8887004fd4cbe 100644 --- a/lib/internal/Magento/Framework/DB/Adapter/AdapterInterface.php +++ b/lib/internal/Magento/Framework/DB/Adapter/AdapterInterface.php @@ -667,7 +667,7 @@ public function quoteTableAs($ident, $alias = null, $auto = false); /** * Format Date to internal database date format * - * @param int|string|\DateTime $date + * @param int|string|\DateTimeInterface $date * @param boolean $includeTime * @return \Zend_Db_Expr */ diff --git a/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php b/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php index a05207ab5f5b2..4ab7fb49c2309 100644 --- a/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php +++ b/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php @@ -332,7 +332,7 @@ public function getTransactionLevel() /** * Convert date to DB format * - * @param int|string|\DateTime $date + * @param int|string|\DateTimeInterface $date * @return \Zend_Db_Expr */ public function convertDate($date) @@ -343,7 +343,7 @@ public function convertDate($date) /** * Convert date and time to DB format * - * @param int|string|\DateTime $datetime + * @param int|string|\DateTimeInterface $datetime * @return \Zend_Db_Expr */ public function convertDateTime($datetime) @@ -2746,7 +2746,7 @@ public function addForeignKey( /** * Format Date to internal database date format * - * @param int|string|\DateTime $date + * @param int|string|\DateTimeInterface $date * @param bool $includeTime * @return \Zend_Db_Expr */ diff --git a/lib/internal/Magento/Framework/Mview/View/StateInterface.php b/lib/internal/Magento/Framework/Mview/View/StateInterface.php index 7c3d1a6637a93..2d1f2cc98b893 100644 --- a/lib/internal/Magento/Framework/Mview/View/StateInterface.php +++ b/lib/internal/Magento/Framework/Mview/View/StateInterface.php @@ -113,7 +113,7 @@ public function getUpdated(); /** * Set state updated time * - * @param string|int|\DateTime $updated + * @param string|int|\DateTimeInterface $updated * @return \Magento\Framework\Mview\View\StateInterface */ public function setUpdated($updated); diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime.php b/lib/internal/Magento/Framework/Stdlib/DateTime.php index 36fdbd9419ff4..c13f875b3c415 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime.php @@ -46,7 +46,7 @@ class DateTime */ public function formatDate($date, $includeTime = true) { - if ($date instanceof \DateTime) { + if ($date instanceof \DateTimeInterface) { $format = $includeTime ? self::DATETIME_PHP_FORMAT : self::DATE_PHP_FORMAT; return $date->format($format); } elseif (empty($date)) { diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatter.php b/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatter.php index 61eeac4522fe0..19a960568d9fc 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatter.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatter.php @@ -64,7 +64,7 @@ public function formatObject($object, $format = null, $locale = null) /** * Implements what IntlDateFormatter::formatObject() is in PHP 5.5+ * - * @param \IntlCalendar|\DateTime $object + * @param \IntlCalendar|\DateTimeInterface $object * @param string|int|array|null $format * @param string|null $locale * @return string diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatterInterface.php b/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatterInterface.php index 4b48c99c73d40..a2d18215385fc 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatterInterface.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatterInterface.php @@ -13,7 +13,7 @@ interface DateTimeFormatterInterface /** * Returns a translated and localized date string * - * @param \IntlCalendar|\DateTime $object + * @param \IntlCalendar|\DateTimeInterface $object * @param string|int|array|null $format * @param string|null $locale * @return string diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php b/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php index c680a24b0ee08..5a705fedc1717 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php @@ -162,7 +162,7 @@ public function date($date = null, $locale = null, $useTimezone = true, $include if (empty($date)) { return new \DateTime('now', new \DateTimeZone($timezone)); - } elseif ($date instanceof \DateTime) { + } elseif ($date instanceof \DateTimeInterface) { return $date->setTimezone(new \DateTimeZone($timezone)); } elseif (!is_numeric($date)) { $timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE; @@ -197,7 +197,7 @@ public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $s { $formatTime = $showTime ? $format : \IntlDateFormatter::NONE; - if (!($date instanceof \DateTime)) { + if (!($date instanceof \DateTimeInterface)) { $date = new \DateTime($date); } @@ -260,7 +260,7 @@ public function formatDateTime( $timezone = null, $pattern = null ) { - if (!($date instanceof \DateTime)) { + if (!($date instanceof \DateTimeInterface)) { $date = new \DateTime($date); } @@ -296,7 +296,7 @@ public function formatDateTime( */ public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s') { - if (!($date instanceof \DateTime)) { + if (!($date instanceof \DateTimeInterface)) { $date = new \DateTime($date, new \DateTimeZone($this->getConfigTimezone())); } else { if ($date->getTimezone()->getName() !== $this->getConfigTimezone()) { diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeFormatterTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeFormatterTest.php index 27b0b87106bcb..9413cdf5875c0 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeFormatterTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeFormatterTest.php @@ -34,7 +34,7 @@ protected function setUp() } /** - * @param \IntlCalendar|\DateTime $object + * @param \IntlCalendar|\DateTimeInterface $object * @param string|int|array|null $format * @param string|null $locale * @param boolean $useIntlFormatObject diff --git a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php index f17b0e68835ae..4a987adc38b28 100644 --- a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php +++ b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php @@ -801,7 +801,7 @@ protected function _getNotFoundUrl($route = '', $params = ['_direct' => 'core/in /** * Retrieve formatting date * - * @param null|string|\DateTime $date + * @param null|string|\DateTimeInterface $date * @param int $format * @param bool $showTime * @param null|string $timezone From 52222949eae3db5ed08c9915d63edb59316c9ac0 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 13:11:33 +0100 Subject: [PATCH 02/11] Add unit tests for Timezone in stdlib, fix edge cases --- .../Framework/Stdlib/DateTime/Timezone.php | 39 +++--- .../Test/Unit/DateTime/TimezoneTest.php | 124 +++++++++++++++--- 2 files changed, 131 insertions(+), 32 deletions(-) diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php b/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php index 5a705fedc1717..388c13e4dcfb6 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php @@ -160,20 +160,25 @@ public function date($date = null, $locale = null, $useTimezone = true, $include ? $this->getConfigTimezone() : date_default_timezone_get(); - if (empty($date)) { - return new \DateTime('now', new \DateTimeZone($timezone)); - } elseif ($date instanceof \DateTimeInterface) { - return $date->setTimezone(new \DateTimeZone($timezone)); - } elseif (!is_numeric($date)) { - $timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE; - $formatter = new \IntlDateFormatter( - $locale, - \IntlDateFormatter::SHORT, - $timeType, - new \DateTimeZone($timezone) - ); - $date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp(); + switch (true) { + case (empty($date)): + return new \DateTime('now', new \DateTimeZone($timezone)); + case ($date instanceof \DateTime): + return $date->setTimezone(new \DateTimeZone($timezone)); + case ($date instanceof \DateTimeImmutable): + return new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone()); + case (!is_numeric($date)): + $timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE; + $formatter = new \IntlDateFormatter( + $locale, + \IntlDateFormatter::SHORT, + $timeType, + new \DateTimeZone($timezone) + ); + $date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp(); + break; } + return (new \DateTime(null, new \DateTimeZone($timezone)))->setTimestamp($date); } @@ -296,8 +301,12 @@ public function formatDateTime( */ public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s') { - if (!($date instanceof \DateTimeInterface)) { - $date = new \DateTime($date, new \DateTimeZone($this->getConfigTimezone())); + if (!($date instanceof \DateTime)) { + if ($date instanceof \DateTimeImmutable) { + $date = new \DateTime($date->format('Y-m-d H:i:s'), new \DateTimeZone($this->getConfigTimezone())); + } else { + $date = new \DateTime($date, new \DateTimeZone($this->getConfigTimezone())); + } } else { if ($date->getTimezone()->getName() !== $this->getConfigTimezone()) { throw new LocalizedException( diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php index 77c8c5607d258..5dd62132320fb 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php @@ -5,46 +5,53 @@ */ namespace Magento\Framework\Stdlib\Test\Unit\DateTime; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\App\ScopeResolverInterface; +use Magento\Framework\Locale\ResolverInterface; +use Magento\Framework\Stdlib\DateTime; use Magento\Framework\Stdlib\DateTime\Timezone; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -/** - * Test for @see Timezone - */ class TimezoneTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + * @var ObjectManager */ private $objectManager; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $scopeResolver; + + /** + * @var ResolverInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $scopeConfigMock; + private $localeResolver; + + /** + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $scopeConfig; protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class) - ->disableOriginalConstructor() - ->getMock(); - parent::setUp(); + $this->objectManager = new ObjectManager($this); + $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)->getMock(); + $this->localeResolver = $this->getMockBuilder(ResolverInterface::class)->getMock(); + $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->getMock(); } /** * Test date parsing with different includeTime options * - * @param string $date - * @param string $locale - * @param bool $includeTime - * @param int $expectedTimestamp * @dataProvider dateIncludeTimeDataProvider */ public function testDateIncludeTime($date, $locale, $includeTime, $expectedTimestamp) { - $this->scopeConfigMock->method('getValue')->willReturn('America/Chicago'); + $this->scopeConfig->method('getValue')->willReturn('America/Chicago'); /** @var Timezone $timezone */ - $timezone = $this->objectManager->getObject(Timezone::class, ['scopeConfig' => $this->scopeConfigMock]); + $timezone = $this->objectManager->getObject(Timezone::class, ['scopeConfig' => $this->scopeConfig]); /** @var \DateTime $dateTime */ $dateTime = $timezone->date($date, $locale, true, $includeTime); @@ -68,4 +75,87 @@ public function dateIncludeTimeDataProvider() ], ]; } + + /** + * @dataProvider getConvertConfigTimeToUtcFixtures + */ + public function testConvertConfigTimeToUtc($date, $configuredTimezone, $expectedResult) + { + $this->scopeConfigWillReturnConfiguredTimezone($configuredTimezone); + + $this->assertEquals($expectedResult, $this->getTimezone()->convertConfigTimeToUtc($date)); + } + + public function getConvertConfigTimeToUtcFixtures() + { + return [ + 'string' => [ + '2016-10-10 10:00:00', + 'UTC', + '2016-10-10 10:00:00' + ], + 'datetime' => [ + new \DateTime('2016-10-10 10:00:00'), + 'UTC', + '2016-10-10 10:00:00' + ], + 'datetimeimmutable' => [ + new \DateTimeImmutable('2016-10-10 10:00:00'), + 'UTC', + '2016-10-10 10:00:00' + ] + ]; + } + + /** + * @dataProvider getDateFixtures + */ + public function testDate($expectedResult, $timezone = 'UTC', $date = null, $locale = null) + { + $this->localeResolver + ->method('getLocale') + ->willReturn('en_GB') + ; + + $this->scopeConfigWillReturnConfiguredTimezone($timezone); + + $this->assertEquals( + $expectedResult, + $this->getTimezone()->date($date, $locale, true), + '', + 1 + ); + } + + public function getDateFixtures() + { + return [ + 'now_datetime_utc' => [new \DateTime('now', new \DateTimeZone('UTC')), 'UTC'], + 'fixed_datetime_utc' => [new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')), 'UTC', new \DateTime('2017-01-01 10:00:00')], + 'now_datetime_vancouver' => [new \DateTime('now', new \DateTimeZone('America/Vancouver')), 'America/Vancouver'], + 'now_datetimeimmutable_utc' => [new \DateTimeImmutable('now', new \DateTimeZone('UTC')), 'UTC'], + 'fixed_datetimeimmutable_utc' => [new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')), 'UTC', new \DateTimeImmutable('2017-01-01 10:00:00')], + 'now_datetimeimmutable_vancouver' => [new \DateTimeImmutable('now', new \DateTimeZone('America/Vancouver')), 'America/Vancouver'], + ]; + } + + private function getTimezone() + { + return new \Magento\Framework\Stdlib\DateTime\Timezone( + $this->scopeResolver, + $this->localeResolver, + $this->getMockBuilder(DateTime::class)->getMock(), + $this->scopeConfig, + '', + '' + ); + } + + private function scopeConfigWillReturnConfiguredTimezone($configuredTimezone) + { + $this->scopeConfig + ->method('getValue') + ->with('', '', null) + ->willReturn($configuredTimezone); + } } From 044038acc53852782b7452f129b0ca77daec4f9d Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 13:13:15 +0100 Subject: [PATCH 03/11] Add di.xml preference for DateTimeInterface --- app/etc/di.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/etc/di.xml b/app/etc/di.xml index f372bd69b9f03..0ba8d4fc8d40a 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -6,6 +6,7 @@ */ --> + From f01ea4c68329fe474716d0965bb95f9085b427bd Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 13:21:17 +0100 Subject: [PATCH 04/11] Update DateTime methods in stdlib to support DateTimeInterface --- .../Magento/Framework/Stdlib/DateTime.php | 2 +- .../Framework/Stdlib/DateTime/DateTime.php | 28 +++++++---- .../Test/Unit/DateTime/DateTimeTest.php | 46 +++++++++++++------ 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime.php b/lib/internal/Magento/Framework/Stdlib/DateTime.php index c13f875b3c415..7f25620fdc736 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime.php @@ -39,7 +39,7 @@ class DateTime /** * Format date to internal format * - * @param string|\DateTime|bool|null $date + * @param string|\DateTimeInterface|bool|null $date * @param boolean $includeTime * @return string|null * @api diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/DateTime.php b/lib/internal/Magento/Framework/Stdlib/DateTime/DateTime.php index 3234eb0ec83fc..7ac93c7b94305 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/DateTime.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/DateTime.php @@ -97,7 +97,7 @@ public function date($format = null, $input = null) /** * Forms GMT timestamp * - * @param int|string $input date in current timezone + * @param int|string|\DateTimeInterface $input date in current timezone * @return int */ public function gmtTimestamp($input = null) @@ -106,6 +106,8 @@ public function gmtTimestamp($input = null) return (int)gmdate('U'); } elseif (is_numeric($input)) { $result = $input; + } elseif ($input instanceof \DateTimeInterface) { + $result = $input->getTimestamp(); } else { $result = strtotime($input); } @@ -128,17 +130,23 @@ public function gmtTimestamp($input = null) */ public function timestamp($input = null) { - if ($input === null) { - $result = $this->gmtTimestamp(); - } elseif (is_numeric($input)) { - $result = $input; - } else { - $result = strtotime($input); + switch (true) { + case ($input === null): + $result = $this->gmtTimestamp(); + break; + case (is_numeric($input)): + $result = $input; + break; + case ($input instanceof \DateTimeInterface): + $result = $input->getTimestamp(); + break; + default: + $result = strtotime($input); } + $date = $this->_localeDate->date($result); - $timestamp = $date->getTimestamp(); - unset($date); - return $timestamp; + + return $date->getTimestamp(); } /** diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php index 47879d4505b5a..cdb19d6514804 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php @@ -1,32 +1,52 @@ getMock(TimezoneInterface::class); + $timezone->method('date')->willReturn(new \DateTime($this->testDate)); + + $expected = gmdate('U', strtotime($this->testDate)); + $this->assertEquals($expected, (new DateTime($timezone))->gmtTimestamp($input)); + } + /** - * @test + * @dataProvider dateTimeInputDataProvider */ - public function testGmtTimestamp() + public function testTimestamp($input) { - $timezone = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)->getMock(); - $timezone->expects($this->any()) - ->method('date') - ->willReturn(new \DateTime('2015-04-02 21:03:00')); - /** @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone */ + /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */ + $timezone = $this->getMock(TimezoneInterface::class); + $timezone->method('date')->willReturn(new \DateTime($this->testDate)); - $dateTime = new DateTime($timezone); - $this->assertEquals( - gmdate('U', strtotime('2015-04-02 21:03:00')), - $dateTime->gmtTimestamp('2015-04-02 21:03:00') - ); + $expected = gmdate('U', strtotime($this->testDate)); + $this->assertEquals($expected, (new DateTime($timezone))->timestamp($input)); + } + + public function dateTimeInputDataProvider() + { + return [ + 'string' => [$this->testDate], + 'int' => [strtotime($this->testDate)], + '\\DateTimeInterface' => [new \DateTimeImmutable($this->testDate)], + ]; } } From 4f645624a077479fd8b69c014490dfd5dfafaf47 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 13:32:05 +0100 Subject: [PATCH 05/11] Fix some incorrect typehints in report collection --- .../Model/ResourceModel/Report/Collection.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php index 58ae28ad1f5ea..e0c22003c8763 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php @@ -19,14 +19,14 @@ class Collection extends \Magento\Framework\Data\Collection /** * From value * - * @var string + * @var \DateTime */ protected $_from; /** * To value * - * @var string + * @var \DateTime */ protected $_to; @@ -118,10 +118,10 @@ public function setPeriod($period) * @param \DateTimeInterface $toDate * @return $this */ - public function setInterval($fromDate, $toDate) + public function setInterval(\DateTimeInterface $fromDate, \DateTimeInterface $toDate) { - $this->_from = $fromDate; - $this->_to = $toDate; + $this->_from = new \DateTime($fromDate->format('Y-m-d'), $fromDate->getTimezone()); + $this->_to = new \DateTime($toDate->format('Y-m-d'), $toDate->getTimezone()); return $this; } @@ -168,10 +168,10 @@ protected function _getIntervals() /** * Get interval for a day * - * @param \DateTimeInterface $dateStart + * @param \DateTime $dateStart * @return array */ - protected function _getDayInterval(\DateTimeInterface $dateStart) + protected function _getDayInterval(\DateTime $dateStart) { $interval = [ 'period' => $this->_localeDate->formatDateTime( @@ -188,12 +188,12 @@ protected function _getDayInterval(\DateTimeInterface $dateStart) /** * Get interval for a month * - * @param \DateTimeInterface $dateStart - * @param \DateTimeInterface $dateEnd + * @param \DateTime $dateStart + * @param \DateTime $dateEnd * @param bool $firstInterval * @return array */ - protected function _getMonthInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, $firstInterval) + protected function _getMonthInterval(\DateTime $dateStart, \DateTime $dateEnd, $firstInterval) { $interval = []; $interval['period'] = $dateStart->format('m/Y'); @@ -231,12 +231,12 @@ protected function _getMonthInterval(\DateTimeInterface $dateStart, \DateTimeInt /** * Get Interval for a year * - * @param \DateTimeInterface $dateStart - * @param \DateTimeInterface $dateEnd + * @param \DateTime $dateStart + * @param \DateTime $dateEnd * @param bool $firstInterval * @return array */ - protected function _getYearInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, $firstInterval) + protected function _getYearInterval(\DateTime $dateStart, \DateTime $dateEnd, $firstInterval) { $interval = []; $interval['period'] = $dateStart->format('Y'); From 5b54fdf7d3b186bc9c56bbdac40af0674e331074 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 14:09:45 +0100 Subject: [PATCH 06/11] Fix broken typehint and test --- lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php | 2 +- .../Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php b/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php index 388c13e4dcfb6..ba9c8c8c098fe 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php @@ -301,7 +301,7 @@ public function formatDateTime( */ public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s') { - if (!($date instanceof \DateTime)) { + if (!($date instanceof \DateTimeInterface)) { if ($date instanceof \DateTimeImmutable) { $date = new \DateTime($date->format('Y-m-d H:i:s'), new \DateTimeZone($this->getConfigTimezone())); } else { diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php index 5dd62132320fb..1d3e9363e90ba 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php @@ -95,12 +95,12 @@ public function getConvertConfigTimeToUtcFixtures() '2016-10-10 10:00:00' ], 'datetime' => [ - new \DateTime('2016-10-10 10:00:00'), + new \DateTime('2016-10-10 10:00:00', new \DateTimeZone('UTC')), 'UTC', '2016-10-10 10:00:00' ], 'datetimeimmutable' => [ - new \DateTimeImmutable('2016-10-10 10:00:00'), + new \DateTimeImmutable('2016-10-10 10:00:00', new \DateTimeZone('UTC')), 'UTC', '2016-10-10 10:00:00' ] From 20381ea0e0a486e2ec8ed5af89567a8b2df35820 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 16:10:42 +0100 Subject: [PATCH 07/11] Ensure Timezone set correctly in Timezone test --- .../Stdlib/Test/Unit/DateTime/TimezoneTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php index 1d3e9363e90ba..89e75fb032b45 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php @@ -14,6 +14,11 @@ class TimezoneTest extends \PHPUnit_Framework_TestCase { + /** + * @var string|null + */ + private static $defaultTimeZone; + /** * @var ObjectManager */ @@ -42,6 +47,11 @@ protected function setUp() $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->getMock(); } + public static function tearDownAfterClass() + { + date_default_timezone_set(static::$defaultTimeZone); + } + /** * Test date parsing with different includeTime options * @@ -129,6 +139,10 @@ public function testDate($expectedResult, $timezone = 'UTC', $date = null, $loca public function getDateFixtures() { + static::$defaultTimeZone = date_default_timezone_get(); + + date_default_timezone_set('UTC'); + return [ 'now_datetime_utc' => [new \DateTime('now', new \DateTimeZone('UTC')), 'UTC'], 'fixed_datetime_utc' => [new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')), 'UTC', new \DateTime('2017-01-01 10:00:00')], From 48d5b12ba0d639e264c8307852670b1805bbb944 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 16:26:38 +0100 Subject: [PATCH 08/11] Test tidy up --- .../Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php index 89e75fb032b45..cd82ff17a3bc8 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php @@ -120,7 +120,7 @@ public function getConvertConfigTimeToUtcFixtures() /** * @dataProvider getDateFixtures */ - public function testDate($expectedResult, $timezone = 'UTC', $date = null, $locale = null) + public function testDate($expectedResult, $timezone = 'UTC', $date = null) { $this->localeResolver ->method('getLocale') @@ -131,7 +131,7 @@ public function testDate($expectedResult, $timezone = 'UTC', $date = null, $loca $this->assertEquals( $expectedResult, - $this->getTimezone()->date($date, $locale, true), + $this->getTimezone()->date($date, null, true), '', 1 ); @@ -155,7 +155,7 @@ public function getDateFixtures() private function getTimezone() { - return new \Magento\Framework\Stdlib\DateTime\Timezone( + return new Timezone( $this->scopeResolver, $this->localeResolver, $this->getMockBuilder(DateTime::class)->getMock(), From cb12acebcb3844dd5f85cc372ba9c025665f1686 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 26 May 2017 16:29:43 +0100 Subject: [PATCH 09/11] Make expected result a callable in Timezone dataprovider --- .../Test/Unit/DateTime/TimezoneTest.php | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php index cd82ff17a3bc8..648fd0fb16a91 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php @@ -120,7 +120,7 @@ public function getConvertConfigTimeToUtcFixtures() /** * @dataProvider getDateFixtures */ - public function testDate($expectedResult, $timezone = 'UTC', $date = null) + public function testDate(callable $expectedResult, $timezone = 'UTC', $date = null) { $this->localeResolver ->method('getLocale') @@ -130,7 +130,7 @@ public function testDate($expectedResult, $timezone = 'UTC', $date = null) $this->scopeConfigWillReturnConfiguredTimezone($timezone); $this->assertEquals( - $expectedResult, + $expectedResult(), $this->getTimezone()->date($date, null, true), '', 1 @@ -144,12 +144,44 @@ public function getDateFixtures() date_default_timezone_set('UTC'); return [ - 'now_datetime_utc' => [new \DateTime('now', new \DateTimeZone('UTC')), 'UTC'], - 'fixed_datetime_utc' => [new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')), 'UTC', new \DateTime('2017-01-01 10:00:00')], - 'now_datetime_vancouver' => [new \DateTime('now', new \DateTimeZone('America/Vancouver')), 'America/Vancouver'], - 'now_datetimeimmutable_utc' => [new \DateTimeImmutable('now', new \DateTimeZone('UTC')), 'UTC'], - 'fixed_datetimeimmutable_utc' => [new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')), 'UTC', new \DateTimeImmutable('2017-01-01 10:00:00')], - 'now_datetimeimmutable_vancouver' => [new \DateTimeImmutable('now', new \DateTimeZone('America/Vancouver')), 'America/Vancouver'], + 'now_datetime_utc' => [ + function () { + return new \DateTime('now', new \DateTimeZone('UTC')); + }, + 'UTC' + ], + 'fixed_datetime_utc' => [ + function () { + return new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')); + }, + 'UTC', + new \DateTime('2017-01-01 10:00:00') + ], + 'now_datetime_vancouver' => [ + function () { + return new \DateTime('now', new \DateTimeZone('America/Vancouver')); + }, + 'America/Vancouver' + ], + 'now_datetimeimmutable_utc' => [ + function () { + return new \DateTimeImmutable('now', new \DateTimeZone('UTC')); + }, + 'UTC' + ], + 'fixed_datetimeimmutable_utc' => [ + function () { + return new \DateTime('2017-01-01 10:00:00', new \DateTimeZone('UTC')); + }, + 'UTC', + new \DateTimeImmutable('2017-01-01 10:00:00') + ], + 'now_datetimeimmutable_vancouver' => [ + function () { + return new \DateTimeImmutable('now', new \DateTimeZone('America/Vancouver')); + }, + 'America/Vancouver' + ], ]; } From b3ef8fe729c815beed90c00c30a278c214678ec9 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 30 Jun 2017 16:29:59 +0300 Subject: [PATCH 10/11] magento/magento2#7174: Type hint for \DateTimeInterface instead of \DateTime - Updated unit test which changed default timezone and affected all test suite - Updated copyright to the up-to-date version --- .../Test/Unit/DateTime/DateTimeTest.php | 16 +++- .../Test/Unit/DateTime/TimezoneTest.php | 96 +++++++++++++------ 2 files changed, 77 insertions(+), 35 deletions(-) diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php index cdb19d6514804..a6aae3684b3c8 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\Stdlib\Test\Unit\DateTime; -use \Magento\Framework\Stdlib\DateTime\DateTime; +use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; /** @@ -13,15 +13,19 @@ */ class DateTimeTest extends \PHPUnit_Framework_TestCase { + /** + * @var string + */ private $testDate = '2015-04-02 21:03:00'; /** + * @param int|string|\DateTimeInterface $input * @dataProvider dateTimeInputDataProvider */ public function testGmtTimestamp($input) { /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */ - $timezone = $this->getMock(TimezoneInterface::class); + $timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock(); $timezone->method('date')->willReturn(new \DateTime($this->testDate)); $expected = gmdate('U', strtotime($this->testDate)); @@ -29,24 +33,28 @@ public function testGmtTimestamp($input) } /** + * @param int|string|\DateTimeInterface $input * @dataProvider dateTimeInputDataProvider */ public function testTimestamp($input) { /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */ - $timezone = $this->getMock(TimezoneInterface::class); + $timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock(); $timezone->method('date')->willReturn(new \DateTime($this->testDate)); $expected = gmdate('U', strtotime($this->testDate)); $this->assertEquals($expected, (new DateTime($timezone))->timestamp($input)); } + /** + * @return array + */ public function dateTimeInputDataProvider() { return [ 'string' => [$this->testDate], 'int' => [strtotime($this->testDate)], - '\\DateTimeInterface' => [new \DateTimeImmutable($this->testDate)], + \DateTimeInterface::class => [new \DateTimeImmutable($this->testDate)], ]; } } diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php index 648fd0fb16a91..2d732a9674dc8 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php @@ -17,7 +17,7 @@ class TimezoneTest extends \PHPUnit_Framework_TestCase /** * @var string|null */ - private static $defaultTimeZone; + private $defaultTimeZone; /** * @var ObjectManager @@ -39,22 +39,35 @@ class TimezoneTest extends \PHPUnit_Framework_TestCase */ private $scopeConfig; + /** + * @inheritdoc + */ protected function setUp() { + $this->defaultTimeZone = date_default_timezone_get(); + date_default_timezone_set('UTC'); + $this->objectManager = new ObjectManager($this); $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)->getMock(); $this->localeResolver = $this->getMockBuilder(ResolverInterface::class)->getMock(); $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->getMock(); } - public static function tearDownAfterClass() + /** + * @inheritdoc + */ + protected function tearDown() { - date_default_timezone_set(static::$defaultTimeZone); + date_default_timezone_set($this->defaultTimeZone); } /** * Test date parsing with different includeTime options * + * @param string $date + * @param string $locale + * @param bool $includeTime + * @param int $expectedTimestamp * @dataProvider dateIncludeTimeDataProvider */ public function testDateIncludeTime($date, $locale, $includeTime, $expectedTimestamp) @@ -68,6 +81,10 @@ public function testDateIncludeTime($date, $locale, $includeTime, $expectedTimes $this->assertEquals($expectedTimestamp, $dateTime->getTimestamp()); } + /** + * DataProvider for testDateIncludeTime + * @return array + */ public function dateIncludeTimeDataProvider() { return [ @@ -87,6 +104,9 @@ public function dateIncludeTimeDataProvider() } /** + * @param string $date + * @param string $configuredTimezone + * @param string $expectedResult * @dataProvider getConvertConfigTimeToUtcFixtures */ public function testConvertConfigTimeToUtc($date, $configuredTimezone, $expectedResult) @@ -96,6 +116,10 @@ public function testConvertConfigTimeToUtc($date, $configuredTimezone, $expected $this->assertEquals($expectedResult, $this->getTimezone()->convertConfigTimeToUtc($date)); } + /** + * Data provider for testConvertConfigTimeToUtc + * @return array + */ public function getConvertConfigTimeToUtcFixtures() { return [ @@ -118,37 +142,41 @@ public function getConvertConfigTimeToUtcFixtures() } /** - * @dataProvider getDateFixtures + * Test configuration of the different timezones. */ - public function testDate(callable $expectedResult, $timezone = 'UTC', $date = null) + public function testDate() { - $this->localeResolver - ->method('getLocale') - ->willReturn('en_GB') - ; - - $this->scopeConfigWillReturnConfiguredTimezone($timezone); - - $this->assertEquals( - $expectedResult(), - $this->getTimezone()->date($date, null, true), - '', - 1 - ); + $dateFixtures = $this->getDateFixtures(); + foreach ($dateFixtures as $dateFixture) { + $expectedResult = $dateFixture[0]; + $timezone = $dateFixture[1]; + $date = $dateFixture[2]; + + $this->localeResolver->method('getLocale')->willReturn('en_GB'); + $this->scopeConfigWillReturnConfiguredTimezone($timezone); + + $this->assertEquals( + $expectedResult(), + $this->getTimezone()->date($date, null, true), + '', + 1 + ); + } } - public function getDateFixtures() + /** + * DataProvider for testDate + * @return array + */ + private function getDateFixtures() { - static::$defaultTimeZone = date_default_timezone_get(); - - date_default_timezone_set('UTC'); - return [ 'now_datetime_utc' => [ function () { return new \DateTime('now', new \DateTimeZone('UTC')); }, - 'UTC' + 'UTC', + null ], 'fixed_datetime_utc' => [ function () { @@ -161,13 +189,15 @@ function () { function () { return new \DateTime('now', new \DateTimeZone('America/Vancouver')); }, - 'America/Vancouver' + 'America/Vancouver', + null ], 'now_datetimeimmutable_utc' => [ function () { return new \DateTimeImmutable('now', new \DateTimeZone('UTC')); }, - 'UTC' + 'UTC', + null ], 'fixed_datetimeimmutable_utc' => [ function () { @@ -180,11 +210,15 @@ function () { function () { return new \DateTimeImmutable('now', new \DateTimeZone('America/Vancouver')); }, - 'America/Vancouver' + 'America/Vancouver', + null ], ]; } + /** + * @return Timezone + */ private function getTimezone() { return new Timezone( @@ -197,11 +231,11 @@ private function getTimezone() ); } + /** + * @param string $configuredTimezone + */ private function scopeConfigWillReturnConfiguredTimezone($configuredTimezone) { - $this->scopeConfig - ->method('getValue') - ->with('', '', null) - ->willReturn($configuredTimezone); + $this->scopeConfig->method('getValue')->with('', '', null)->willReturn($configuredTimezone); } } From 9df60566fca2c45843559c305f7168b81e03327b Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 30 Jun 2017 17:39:30 +0300 Subject: [PATCH 11/11] magento/magento2#7174: Type hint for \DateTimeInterface instead of \DateTime - Updated copyright to the up-to-date version --- .../Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php index a6aae3684b3c8..34934c785f569 100644 --- a/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php +++ b/lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php @@ -1,6 +1,6 @@