|
| 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\Downloadable\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Product\Type as ProductType; |
| 12 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 13 | +use Magento\Downloadable\Model\Product\Type as DownloadableProductType; |
| 14 | +use Magento\Downloadable\Observer\SetHasDownloadableProductsObserver; |
| 15 | +use Magento\Framework\DataObject; |
| 16 | +use Magento\Framework\Event\Observer; |
| 17 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 18 | +use Magento\Sales\Model\Order; |
| 19 | +use Magento\Sales\Model\Order\Item; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class SetHasDownloadableProductsObserverTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ObjectManager |
| 27 | + */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var CheckoutSession|MockObject |
| 32 | + */ |
| 33 | + private $checkoutSessionMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var SetHasDownloadableProductsObserver |
| 37 | + */ |
| 38 | + private $setHasDownloadableProductsObserver; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Order|MockObject |
| 42 | + */ |
| 43 | + private $orderMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * Setup environment for test |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->objectManager = new ObjectManager($this); |
| 51 | + |
| 52 | + $this->orderMock = $this->createPartialMock(Order::class, ['getAllItems']); |
| 53 | + |
| 54 | + $this->checkoutSessionMock = $this->createPartialMock( |
| 55 | + CheckoutSession::class, |
| 56 | + [ |
| 57 | + 'getHasDownloadableProducts', |
| 58 | + 'setHasDownloadableProducts' |
| 59 | + ] |
| 60 | + ); |
| 61 | + |
| 62 | + $this->setHasDownloadableProductsObserver = $this->objectManager->getObject( |
| 63 | + SetHasDownloadableProductsObserver::class, |
| 64 | + [ |
| 65 | + 'checkoutSession' => $this->checkoutSessionMock |
| 66 | + ] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test execute with session has downloadable products |
| 72 | + */ |
| 73 | + public function testExecuteWithSessionHasDownloadableProducts() |
| 74 | + { |
| 75 | + $event = new DataObject(['item' => $this->orderMock]); |
| 76 | + $observer = new Observer(['event' => $event]); |
| 77 | + |
| 78 | + $this->checkoutSessionMock->method('getHasDownloadableProducts')->willReturn(true); |
| 79 | + $this->orderMock->method('getAllItems')->willReturn([]); |
| 80 | + |
| 81 | + $this->checkoutSessionMock->expects($this->never()) |
| 82 | + ->method('setHasDownloadableProducts')->with(true); |
| 83 | + |
| 84 | + $this->setHasDownloadableProductsObserver->execute($observer); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test execute with session has no downloadable products with the data provider |
| 89 | + * |
| 90 | + * @dataProvider executeWithSessionNoDownloadableProductsDataProvider |
| 91 | + */ |
| 92 | + public function testExecuteWithSessionNoDownloadableProducts($allItems, $expectedCall) |
| 93 | + { |
| 94 | + $event = new DataObject(['order' => $this->orderMock]); |
| 95 | + $observer = new Observer(['event' => $event]); |
| 96 | + |
| 97 | + $allOrderItemsMock = []; |
| 98 | + foreach ($allItems as $item) { |
| 99 | + $allOrderItemsMock[] = $this->createOrderItem(...$item); |
| 100 | + } |
| 101 | + |
| 102 | + $this->checkoutSessionMock->method('getHasDownloadableProducts')->willReturn(false); |
| 103 | + |
| 104 | + $this->orderMock->method('getAllItems')->willReturn($allOrderItemsMock); |
| 105 | + |
| 106 | + $this->checkoutSessionMock->expects($expectedCall) |
| 107 | + ->method('setHasDownloadableProducts')->with(true); |
| 108 | + |
| 109 | + $this->setHasDownloadableProductsObserver->execute($observer); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Create Order Item Mock |
| 114 | + * |
| 115 | + * @param string $productType |
| 116 | + * @param string $realProductType |
| 117 | + * @param string $isDownloadable |
| 118 | + * @return Item|MockObject |
| 119 | + */ |
| 120 | + private function createOrderItem( |
| 121 | + $productType = DownloadableProductType::TYPE_DOWNLOADABLE, |
| 122 | + $realProductType = DownloadableProductType::TYPE_DOWNLOADABLE, |
| 123 | + $isDownloadable = '1' |
| 124 | + ) { |
| 125 | + $item = $this->createPartialMock( |
| 126 | + Item::class, |
| 127 | + ['getProductType', 'getRealProductType', 'getProductOptionByCode'] |
| 128 | + ); |
| 129 | + |
| 130 | + $item->expects($this->any()) |
| 131 | + ->method('getProductType') |
| 132 | + ->willReturn($productType); |
| 133 | + $item->expects($this->any()) |
| 134 | + ->method('getRealProductType') |
| 135 | + ->willReturn($realProductType); |
| 136 | + $item->expects($this->any()) |
| 137 | + ->method('getProductOptionByCode') |
| 138 | + ->with('is_downloadable') |
| 139 | + ->willReturn($isDownloadable); |
| 140 | + |
| 141 | + return $item; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Data Provider for test execute with session has no downloadable product |
| 146 | + * |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + public function executeWithSessionNoDownloadableProductsDataProvider() |
| 150 | + { |
| 151 | + return [ |
| 152 | + 'Order has one item is downloadable product' => [ |
| 153 | + [ |
| 154 | + [ |
| 155 | + DownloadableProductType::TYPE_DOWNLOADABLE, |
| 156 | + DownloadableProductType::TYPE_DOWNLOADABLE, |
| 157 | + '1' |
| 158 | + ], |
| 159 | + [ |
| 160 | + ProductType::TYPE_SIMPLE, |
| 161 | + ProductType::TYPE_SIMPLE, |
| 162 | + '1' |
| 163 | + ] |
| 164 | + ], |
| 165 | + $this->once() |
| 166 | + ], |
| 167 | + 'Order has all items are simple product' => [ |
| 168 | + [ |
| 169 | + [ |
| 170 | + ProductType::TYPE_SIMPLE, |
| 171 | + ProductType::TYPE_SIMPLE, |
| 172 | + '0' |
| 173 | + ], |
| 174 | + [ |
| 175 | + ProductType::TYPE_SIMPLE, |
| 176 | + ProductType::TYPE_SIMPLE, |
| 177 | + '0' |
| 178 | + ] |
| 179 | + ], |
| 180 | + $this->never() |
| 181 | + ], |
| 182 | + ]; |
| 183 | + } |
| 184 | +} |
0 commit comments