|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +/** |
| 4 | + * Copyright 2015 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Magento\Framework\Stdlib\Test\Unit\DateTime; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\App\ScopeInterface; |
| 12 | +use Magento\Framework\App\ScopeResolverInterface; |
| 13 | +use Magento\Framework\Locale\ResolverInterface; |
| 14 | +use Magento\Framework\Stdlib\DateTime; |
| 15 | +use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory; |
| 16 | +use Magento\Framework\Stdlib\DateTime\Timezone; |
| 17 | +use PHPUnit\Framework\MockObject\Exception; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test for scope date interval functionality @see Timezone |
| 23 | + */ |
| 24 | +class TimezoneScopeTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var string|null |
| 28 | + */ |
| 29 | + private ?string $defaultTimeZone; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + private string $scopeType = 'store'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + private string $defaultTimezonePath = 'default/timezone/path'; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ScopeResolverInterface|MockObject |
| 43 | + */ |
| 44 | + private ScopeResolverInterface|MockObject $scopeResolver; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var ScopeConfigInterface|MockObject |
| 48 | + */ |
| 49 | + private MockObject|ScopeConfigInterface $scopeConfig; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var ResolverInterface|MockObject |
| 53 | + */ |
| 54 | + private ResolverInterface|MockObject $localeResolver; |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritdoc |
| 58 | + */ |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + $this->defaultTimeZone = date_default_timezone_get(); |
| 62 | + date_default_timezone_set('UTC'); |
| 63 | + $this->scopeType = 'store'; |
| 64 | + $this->defaultTimezonePath = 'default/timezone/path'; |
| 65 | + |
| 66 | + $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class) |
| 67 | + ->getMock(); |
| 68 | + $this->localeResolver = $this->getMockBuilder(ResolverInterface::class) |
| 69 | + ->getMock(); |
| 70 | + $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class) |
| 71 | + ->getMock(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @inheritdoc |
| 76 | + */ |
| 77 | + protected function tearDown(): void |
| 78 | + { |
| 79 | + date_default_timezone_set($this->defaultTimeZone); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return Timezone |
| 84 | + * @throws Exception |
| 85 | + */ |
| 86 | + private function getTimezone(): Timezone |
| 87 | + { |
| 88 | + return new Timezone( |
| 89 | + $this->scopeResolver, |
| 90 | + $this->localeResolver, |
| 91 | + $this->createMock(DateTime::class), |
| 92 | + $this->scopeConfig, |
| 93 | + $this->scopeType, |
| 94 | + $this->defaultTimezonePath, |
| 95 | + new DateFormatterFactory() |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return void |
| 101 | + * @throws Exception |
| 102 | + */ |
| 103 | + public function testIsScopeDateInInterval() |
| 104 | + { |
| 105 | + $scopeMock = $this->createMock(ScopeInterface::class); |
| 106 | + $this->scopeResolver->method('getScope')->willReturn($scopeMock); |
| 107 | + |
| 108 | + $result = $this->getTimezone()->isScopeDateInInterval( |
| 109 | + null, |
| 110 | + '2025-04-01 00:00:00', |
| 111 | + '2999-05-01 00:00:00', |
| 112 | + ); |
| 113 | + |
| 114 | + $this->assertTrue($result); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return void |
| 119 | + * @throws Exception |
| 120 | + */ |
| 121 | + public function testIsScopeDateInIntervalFalse() |
| 122 | + { |
| 123 | + $scopeMock = $this->createMock(ScopeInterface::class); |
| 124 | + $this->scopeResolver->method('getScope')->willReturn($scopeMock); |
| 125 | + |
| 126 | + $result = $this->getTimezone()->isScopeDateInInterval( |
| 127 | + null, |
| 128 | + '2025-03-01 00:00:00', |
| 129 | + '2025-04-01 00:00:00', |
| 130 | + ); |
| 131 | + |
| 132 | + $this->assertFalse($result); |
| 133 | + } |
| 134 | +} |
0 commit comments