Skip to content

Commit 62120b2

Browse files
ENGCOM-7224: Fixed tests for Magento\Framework\Stdlib\DateTime\DateTime #27492
- Merge Pull Request #27492 from andrewbess/magento2:bugfix/fix-test-for-datetime - Merged commits: 1. b20f9eb 2. 7ee81db
2 parents 1116437 + 7ee81db commit 62120b2

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeTest.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@
55
*/
66
namespace Magento\Framework\Stdlib\Test\Unit\DateTime;
77

8+
use DateTimeImmutable;
9+
use DateTimeInterface;
10+
use Exception;
811
use Magento\Framework\Stdlib\DateTime\DateTime;
912
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
1015

1116
/**
12-
* Magento\Framework\Stdlib\DateTimeTest test case
17+
* Tests for @see DateTime
1318
*/
14-
class DateTimeTest extends \PHPUnit\Framework\TestCase
19+
class DateTimeTest extends TestCase
1520
{
1621
/**
1722
* @var string
1823
*/
1924
private $testDate = '2015-04-02 21:03:00';
2025

2126
/**
22-
* @param int|string|\DateTimeInterface $input
27+
* @param int|string|DateTimeInterface $input
28+
* @throws Exception
29+
*
2330
* @dataProvider dateTimeInputDataProvider
2431
*/
2532
public function testGmtTimestamp($input)
2633
{
27-
/** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
34+
/** @var TimezoneInterface|MockObject $timezone */
2835
$timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock();
2936
$timezone->method('date')->willReturn(new \DateTime($this->testDate));
3037

@@ -33,12 +40,14 @@ public function testGmtTimestamp($input)
3340
}
3441

3542
/**
36-
* @param int|string|\DateTimeInterface $input
43+
* @param int|string|DateTimeInterface $input
44+
* @throws Exception
45+
*
3746
* @dataProvider dateTimeInputDataProvider
3847
*/
3948
public function testTimestamp($input)
4049
{
41-
/** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
50+
/** @var TimezoneInterface|MockObject $timezone */
4251
$timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock();
4352
$timezone->method('date')->willReturn(new \DateTime($this->testDate));
4453

@@ -48,29 +57,50 @@ public function testTimestamp($input)
4857

4958
public function testGtmOffset()
5059
{
51-
/** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
60+
/** @var TimezoneInterface|MockObject $timezone */
5261
$timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock();
53-
// Asia/Tbilisi timezone have no DST
54-
$timezone->method('getConfigTimezone')->willReturn('Asia/Tbilisi');
62+
$timezone->method('getConfigTimezone')->willReturn('Europe/Amsterdam');
5563

56-
/** @var DateTime|\PHPUnit_Framework_MockObject_MockObject $dateTime */
64+
/** @var DateTime|MockObject $dateTime */
5765
$dateTime = $this->getMockBuilder(DateTime::class)
5866
->setConstructorArgs([$timezone])
5967
->setMethods(null)
6068
->getMock();
6169

62-
$this->assertEquals(14400, $dateTime->getGmtOffset());
70+
$this->assertEquals(
71+
$this->getExpectedGtmOffset($timezone->getConfigTimezone()),
72+
$dateTime->getGmtOffset()
73+
);
6374
}
6475

6576
/**
77+
* Returns expected offset according to Daylight Saving Time in timezone
78+
*
79+
* @param string $timezoneIdentifier
80+
* @return int
81+
*/
82+
private function getExpectedGtmOffset(string $timezoneIdentifier): int
83+
{
84+
$timeZoneToReturn = date_default_timezone_get();
85+
date_default_timezone_set($timezoneIdentifier);
86+
$expectedOffset = (date('I', time()) + 1) * 3600;
87+
date_default_timezone_set($timeZoneToReturn);
88+
89+
return (int) $expectedOffset;
90+
}
91+
92+
/**
93+
* Data provider
94+
*
6695
* @return array
96+
* @throws Exception
6797
*/
6898
public function dateTimeInputDataProvider()
6999
{
70100
return [
71101
'string' => [$this->testDate],
72102
'int' => [strtotime($this->testDate)],
73-
\DateTimeInterface::class => [new \DateTimeImmutable($this->testDate)],
103+
DateTimeInterface::class => [new DateTimeImmutable($this->testDate)],
74104
];
75105
}
76106
}

0 commit comments

Comments
 (0)