Skip to content

Commit 804e2f8

Browse files
committed
[Downloadable] Cover the Observer SetHasDownloadableProductsObserver by Unit Test
1 parent e8c2526 commit 804e2f8

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
63+
$this->setHasDownloadableProductsObserver = $this->objectManager->getObject(
64+
SetHasDownloadableProductsObserver::class,
65+
[
66+
'checkoutSession' => $this->checkoutSessionMock
67+
]
68+
);
69+
}
70+
71+
/**
72+
* Test execute with session has downloadable products
73+
*/
74+
public function testExecuteWithSessionHasDownloadableProducts()
75+
{
76+
$event = new DataObject(['item' => $this->orderMock]);
77+
$observer = new Observer(['event' => $event]);
78+
79+
$this->checkoutSessionMock->method('getHasDownloadableProducts')->willReturn(true);
80+
$this->orderMock->method('getAllItems')->willReturn([]);
81+
82+
$this->checkoutSessionMock->expects($this->never())
83+
->method('setHasDownloadableProducts')->with(true);
84+
85+
$this->setHasDownloadableProductsObserver->execute($observer);
86+
}
87+
88+
/**
89+
* Test execute with session has no downloadable products with the data provider
90+
*
91+
* @dataProvider executeWithSessionNoDownloadableProductsDataProvider
92+
*/
93+
public function testExecuteWithSessionNoDownloadableProducts($allItems, $expectedCall)
94+
{
95+
$event = new DataObject(['order' => $this->orderMock]);
96+
$observer = new Observer(['event' => $event]);
97+
98+
$allOrderItemsMock = [];
99+
foreach ($allItems as $item) {
100+
$allOrderItemsMock[] = $this->createOrderItem(...$item);
101+
}
102+
103+
$this->checkoutSessionMock->method('getHasDownloadableProducts')->willReturn(false);
104+
105+
$this->orderMock->method('getAllItems')->willReturn($allOrderItemsMock);
106+
107+
$this->checkoutSessionMock->expects($expectedCall)
108+
->method('setHasDownloadableProducts')->with(true);
109+
110+
$this->setHasDownloadableProductsObserver->execute($observer);
111+
}
112+
113+
/**
114+
* Create Order Item Mock
115+
*
116+
* @param string $productType
117+
* @param string $realProductType
118+
* @param string $isDownloadable
119+
* @return Item|MockObject
120+
*/
121+
private function createOrderItem(
122+
$productType = DownloadableProductType::TYPE_DOWNLOADABLE,
123+
$realProductType = DownloadableProductType::TYPE_DOWNLOADABLE,
124+
$isDownloadable = '1'
125+
) {
126+
$item = $this->createPartialMock(
127+
Item::class,
128+
['getProductType', 'getRealProductType', 'getProductOptionByCode']
129+
);
130+
131+
$item->expects($this->any())
132+
->method('getProductType')
133+
->willReturn($productType);
134+
$item->expects($this->any())
135+
->method('getRealProductType')
136+
->willReturn($realProductType);
137+
$item->expects($this->any())
138+
->method('getProductOptionByCode')
139+
->with('is_downloadable')
140+
->willReturn($isDownloadable);
141+
142+
return $item;
143+
}
144+
145+
/**
146+
* Data Provider for test execute with session has no downloadable product
147+
*
148+
* @return array
149+
*/
150+
public function executeWithSessionNoDownloadableProductsDataProvider()
151+
{
152+
return [
153+
'Order has once item is downloadable product' => [
154+
[
155+
[
156+
DownloadableProductType::TYPE_DOWNLOADABLE,
157+
DownloadableProductType::TYPE_DOWNLOADABLE,
158+
'1'
159+
],
160+
[
161+
ProductType::TYPE_SIMPLE,
162+
ProductType::TYPE_SIMPLE,
163+
'1'
164+
]
165+
],
166+
$this->once()
167+
],
168+
'Order has all items are simple product' => [
169+
[
170+
[
171+
ProductType::TYPE_SIMPLE,
172+
ProductType::TYPE_SIMPLE,
173+
'0'
174+
],
175+
[
176+
ProductType::TYPE_SIMPLE,
177+
ProductType::TYPE_SIMPLE,
178+
'0'
179+
]
180+
],
181+
$this->never()
182+
],
183+
];
184+
}
185+
}

0 commit comments

Comments
 (0)