DateTimeTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\Test\Unit\DateTime;
  7. use Magento\Framework\Stdlib\DateTime\DateTime;
  8. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  9. /**
  10. * Magento\Framework\Stdlib\DateTimeTest test case
  11. */
  12. class DateTimeTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var string
  16. */
  17. private $testDate = '2015-04-02 21:03:00';
  18. /**
  19. * @param int|string|\DateTimeInterface $input
  20. * @dataProvider dateTimeInputDataProvider
  21. */
  22. public function testGmtTimestamp($input)
  23. {
  24. /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
  25. $timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock();
  26. $timezone->method('date')->willReturn(new \DateTime($this->testDate));
  27. $expected = gmdate('U', strtotime($this->testDate));
  28. $this->assertEquals($expected, (new DateTime($timezone))->gmtTimestamp($input));
  29. }
  30. /**
  31. * @param int|string|\DateTimeInterface $input
  32. * @dataProvider dateTimeInputDataProvider
  33. */
  34. public function testTimestamp($input)
  35. {
  36. /** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
  37. $timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock();
  38. $timezone->method('date')->willReturn(new \DateTime($this->testDate));
  39. $expected = gmdate('U', strtotime($this->testDate));
  40. $this->assertEquals($expected, (new DateTime($timezone))->timestamp($input));
  41. }
  42. /**
  43. * @return array
  44. */
  45. public function dateTimeInputDataProvider()
  46. {
  47. return [
  48. 'string' => [$this->testDate],
  49. 'int' => [strtotime($this->testDate)],
  50. \DateTimeInterface::class => [new \DateTimeImmutable($this->testDate)],
  51. ];
  52. }
  53. }