|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Weee\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\Framework\Event; |
| 11 | +use Magento\Framework\Event\Observer; |
| 12 | +use Magento\Payment\Model\Cart; |
| 13 | +use Magento\Payment\Model\Cart\SalesModel\SalesModelInterface; |
| 14 | +use Magento\Quote\Model\Quote\Item; |
| 15 | +use Magento\Store\Api\Data\StoreInterface; |
| 16 | +use Magento\Store\Model\Store; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use Magento\Weee\Helper\Data; |
| 19 | +use Magento\Weee\Observer\AddPaymentWeeeItem; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class AddPaymentWeeeItemTest |
| 25 | + */ |
| 26 | +class AddPaymentWeeeItemTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Testable object |
| 30 | + * |
| 31 | + * @var AddPaymentWeeeItem |
| 32 | + */ |
| 33 | + private $observer; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Data|MockObject |
| 37 | + */ |
| 38 | + private $weeeHelperMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var StoreManagerInterface|MockObject |
| 42 | + */ |
| 43 | + private $storeManagerMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * Set Up |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->weeeHelperMock = $this->createMock(Data::class); |
| 51 | + $this->storeManagerMock = $this->createMock(StoreManagerInterface::class); |
| 52 | + |
| 53 | + $this->observer = new AddPaymentWeeeItem( |
| 54 | + $this->weeeHelperMock, |
| 55 | + $this->storeManagerMock |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test execute |
| 61 | + * |
| 62 | + * @dataProvider dataProvider |
| 63 | + * @param bool $isEnabled |
| 64 | + * @param bool $includeInSubtotal |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function testExecute(bool $isEnabled, bool $includeInSubtotal) |
| 68 | + { |
| 69 | + /** @var Observer|MockObject $observerMock */ |
| 70 | + $observerMock = $this->createMock(Observer::class); |
| 71 | + $cartModelMock = $this->createMock(Cart::class); |
| 72 | + $salesModelMock = $this->createMock(SalesModelInterface::class); |
| 73 | + $itemMock = $this->createPartialMock(Item::class, ['getOriginalItem']); |
| 74 | + $originalItemMock = $this->createPartialMock(Item::class, ['getParentItem']); |
| 75 | + $parentItemMock = $this->createMock(Item::class); |
| 76 | + $eventMock = $this->getMockBuilder(Event::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->setMethods(['getCart']) |
| 79 | + ->getMock(); |
| 80 | + |
| 81 | + $asCustomItem = $this->prepareShouldBeAddedAsCustomItem($isEnabled, $includeInSubtotal); |
| 82 | + $toBeCalled = 1; |
| 83 | + if (!$asCustomItem) { |
| 84 | + $toBeCalled = 0; |
| 85 | + } |
| 86 | + |
| 87 | + $eventMock->expects($this->atLeast($toBeCalled)) |
| 88 | + ->method('getCart') |
| 89 | + ->willReturn($cartModelMock); |
| 90 | + $observerMock->expects($this->atLeast($toBeCalled)) |
| 91 | + ->method('getEvent') |
| 92 | + ->willReturn($eventMock); |
| 93 | + $itemMock->expects($this->atLeast($toBeCalled)) |
| 94 | + ->method('getOriginalItem') |
| 95 | + ->willReturn($originalItemMock); |
| 96 | + $originalItemMock->expects($this->atLeast($toBeCalled)) |
| 97 | + ->method('getParentItem') |
| 98 | + ->willReturn($parentItemMock); |
| 99 | + $salesModelMock->expects($this->atLeast($toBeCalled)) |
| 100 | + ->method('getAllItems') |
| 101 | + ->willReturn([$itemMock]); |
| 102 | + $cartModelMock->expects($this->atLeast($toBeCalled)) |
| 103 | + ->method('getSalesModel') |
| 104 | + ->willReturn($salesModelMock); |
| 105 | + |
| 106 | + $this->observer->execute($observerMock); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + public function dataProvider(): array |
| 113 | + { |
| 114 | + return [ |
| 115 | + [true, false], |
| 116 | + [true, true], |
| 117 | + [false, true], |
| 118 | + [false, false], |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Prepare if FPT should be added to payment cart as custom item or not. |
| 124 | + * |
| 125 | + * @param bool $isEnabled |
| 126 | + * @param bool $includeInSubtotal |
| 127 | + * @return bool |
| 128 | + */ |
| 129 | + private function prepareShouldBeAddedAsCustomItem(bool $isEnabled, bool $includeInSubtotal): bool |
| 130 | + { |
| 131 | + $storeMock = $this->getMockBuilder(StoreInterface::class) |
| 132 | + ->setMethods(['getId']) |
| 133 | + ->getMockForAbstractClass(); |
| 134 | + $storeMock->method('getId')->willReturn(Store::DEFAULT_STORE_ID); |
| 135 | + $this->storeManagerMock->method('getStore')->willReturn($storeMock); |
| 136 | + $this->weeeHelperMock->method('isEnabled')->with(Store::DEFAULT_STORE_ID) |
| 137 | + ->willReturn($isEnabled); |
| 138 | + |
| 139 | + if ($isEnabled) { |
| 140 | + $this->weeeHelperMock->method('includeInSubtotal')->with(Store::DEFAULT_STORE_ID) |
| 141 | + ->willReturn($includeInSubtotal); |
| 142 | + } |
| 143 | + |
| 144 | + return $isEnabled && !$includeInSubtotal; |
| 145 | + } |
| 146 | +} |
0 commit comments