|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Observer\SetSpecialPriceStartDate; |
| 13 | +use Magento\Framework\Event; |
| 14 | +use Magento\Framework\Event\Observer; |
| 15 | +use Magento\Framework\Stdlib\DateTime\Timezone; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit test for \Magento\Catalog\Observer\SetSpecialPriceStartDate |
| 22 | + */ |
| 23 | +class SetSpecialPriceStartDateTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Testable Object |
| 27 | + * |
| 28 | + * @var SetSpecialPriceStartDate |
| 29 | + */ |
| 30 | + private $observer; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ObjectManager |
| 34 | + */ |
| 35 | + private $objectManager; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Observer|MockObject |
| 39 | + */ |
| 40 | + private $observerMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Event|MockObject |
| 44 | + */ |
| 45 | + private $eventMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Product|MockObject |
| 49 | + */ |
| 50 | + private $productMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Timezone|MockObject |
| 54 | + */ |
| 55 | + private $timezone; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var \DateTime|MockObject |
| 59 | + */ |
| 60 | + private $dateObject; |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritdoc |
| 64 | + */ |
| 65 | + protected function setUp(): void |
| 66 | + { |
| 67 | + $this->objectManager = new ObjectManager($this); |
| 68 | + $this->observerMock = $this->createMock(Observer::class); |
| 69 | + $this->timezone = $this->createMock(Timezone::class); |
| 70 | + $this->dateObject = $this->createMock(\DateTime::class); |
| 71 | + |
| 72 | + $this->eventMock = $this->getMockBuilder(Event::class) |
| 73 | + ->disableOriginalConstructor() |
| 74 | + ->setMethods(['getProduct']) |
| 75 | + ->getMock(); |
| 76 | + |
| 77 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 78 | + ->disableOriginalConstructor() |
| 79 | + ->setMethods(['getSpecialPrice', 'getSpecialFromDate', 'setData']) |
| 80 | + ->getMock(); |
| 81 | + |
| 82 | + $this->observer = $this->objectManager->getObject( |
| 83 | + SetSpecialPriceStartDate::class, |
| 84 | + [ |
| 85 | + 'localeDate' => $this->timezone |
| 86 | + ] |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test observer execute method |
| 92 | + */ |
| 93 | + public function testExecuteModifySpecialFromDate(): void |
| 94 | + { |
| 95 | + $specialPrice = 15; |
| 96 | + $specialFromDate = null; |
| 97 | + $localeDateMock = ['special_from_date' => $this->returnValue($this->dateObject)]; |
| 98 | + |
| 99 | + $this->observerMock |
| 100 | + ->expects($this->once()) |
| 101 | + ->method('getEvent') |
| 102 | + ->willReturn($this->eventMock); |
| 103 | + |
| 104 | + $this->eventMock |
| 105 | + ->expects($this->once()) |
| 106 | + ->method('getProduct') |
| 107 | + ->willReturn($this->productMock); |
| 108 | + |
| 109 | + $this->dateObject->expects($this->any()) |
| 110 | + ->method('setTime') |
| 111 | + ->willReturnSelf(); |
| 112 | + |
| 113 | + $this->timezone |
| 114 | + ->expects($this->once()) |
| 115 | + ->method('date') |
| 116 | + ->willReturn($this->dateObject); |
| 117 | + |
| 118 | + $this->productMock |
| 119 | + ->expects($this->once()) |
| 120 | + ->method('getSpecialPrice') |
| 121 | + ->willReturn($specialPrice); |
| 122 | + |
| 123 | + $this->productMock |
| 124 | + ->expects($this->once()) |
| 125 | + ->method('getSpecialFromDate') |
| 126 | + ->willReturn($specialFromDate); |
| 127 | + |
| 128 | + $this->productMock |
| 129 | + ->expects($this->once()) |
| 130 | + ->method('setData') |
| 131 | + ->willReturn($localeDateMock); |
| 132 | + |
| 133 | + $this->observer->execute($this->observerMock); |
| 134 | + } |
| 135 | +} |
0 commit comments