Skip to content

Commit b20f9eb

Browse files
andrewbessAndrii Beziazychnyi
authored andcommitted
Fixed tests for Magento\Framework\Stdlib\DateTime\DateTime
1 parent 176ba2b commit b20f9eb

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

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

Lines changed: 41 additions & 10 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,28 +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();
5362
$timezone->method('getConfigTimezone')->willReturn('Europe/Amsterdam');
5463

55-
/** @var DateTime|\PHPUnit_Framework_MockObject_MockObject $dateTime */
64+
/** @var DateTime|MockObject $dateTime */
5665
$dateTime = $this->getMockBuilder(DateTime::class)
5766
->setConstructorArgs([$timezone])
5867
->setMethods(null)
5968
->getMock();
6069

61-
$this->assertEquals(3600, $dateTime->getGmtOffset());
70+
$this->assertEquals(
71+
$this->getExpectedGtmOffset($timezone->getConfigTimezone()),
72+
$dateTime->getGmtOffset()
73+
);
6274
}
6375

6476
/**
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+
*
6595
* @return array
96+
* @throws Exception
6697
*/
6798
public function dateTimeInputDataProvider()
6899
{
69100
return [
70101
'string' => [$this->testDate],
71102
'int' => [strtotime($this->testDate)],
72-
\DateTimeInterface::class => [new \DateTimeImmutable($this->testDate)],
103+
DateTimeInterface::class => [new DateTimeImmutable($this->testDate)],
73104
];
74105
}
75106
}

0 commit comments

Comments
 (0)