Skip to content

Commit dc9c1db

Browse files
ENGCOM-6767: Unit test for Magento\Reports\Observer\CheckoutCartAddProductObserver #26579
- Merge Pull Request #26579 from karyna-tsymbal-atwix/magento2:unit-test-reports-checkoutCartAddProductObserver - Merged commits: 1. c877563 2. d2bb11c 3. c81e728 4. a183437
2 parents 606c3e8 + a183437 commit dc9c1db

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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\Reports\Test\Unit\Observer;
10+
11+
use Magento\Framework\Event;
12+
use Magento\Framework\Event\Observer;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Quote\Model\Quote\Item as QuoteItem;
15+
use Magento\Reports\Model\Event as ReportsEventModel;
16+
use Magento\Reports\Model\ReportStatus;
17+
use Magento\Reports\Observer\CheckoutCartAddProductObserver;
18+
use Magento\Reports\Observer\EventSaver;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* Unit Test for \Magento\Reports\Observer\CheckoutCartAddProductObserver
24+
*/
25+
class CheckoutCartAddProductObserverTest extends TestCase
26+
{
27+
const STUB_QUOTE_PARENT_ITEM_ID = 1;
28+
const STUB_QUOTE_ITEM_ID = 2;
29+
30+
/**
31+
* @var MockObject|EventSaver
32+
*/
33+
private $eventSaverMock;
34+
35+
/**
36+
* @var MockObject|ReportStatus
37+
*/
38+
private $reportStatusMock;
39+
40+
/**
41+
* @var MockObject|Observer
42+
*/
43+
private $eventObserverMock;
44+
45+
/**
46+
* @var MockObject|Event
47+
*/
48+
private $eventMock;
49+
50+
/**
51+
* @var MockObject|QuoteItem
52+
*/
53+
private $quoteItemMock;
54+
55+
/**
56+
* @var CheckoutCartAddProductObserver
57+
*/
58+
private $observer;
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
protected function setUp()
64+
{
65+
$this->eventSaverMock = $this->createMock(EventSaver::class);
66+
$this->reportStatusMock = $this->createMock(ReportStatus::class);
67+
$this->eventObserverMock = $this->createMock(Observer::class);
68+
$this->quoteItemMock = $this->createMock(QuoteItem::class);
69+
$this->eventMock = $this->getMockBuilder(Event::class)
70+
->disableOriginalConstructor()
71+
->setMethods(['getItem'])
72+
->getMock();
73+
74+
$objectManager = new ObjectManager($this);
75+
$this->observer = $objectManager->getObject(
76+
CheckoutCartAddProductObserver::class,
77+
[
78+
'eventSaver' => $this->eventSaverMock,
79+
'reportStatus' => $this->reportStatusMock
80+
]
81+
);
82+
}
83+
84+
/**
85+
* The case when event has to be successfully saved
86+
*/
87+
public function testExecuteExpectsSaveCalledWhenNewProductAdded()
88+
{
89+
$this->configureMocksWhenReportsEnabled();
90+
$this->quoteItemMock->expects($this->once())
91+
->method('getId')
92+
->willReturn(null);
93+
$this->quoteItemMock->expects($this->once())
94+
->method('getParentItem')
95+
->willReturn(null);
96+
97+
$this->eventSaverMock->expects($this->once())->method('save');
98+
$this->observer->execute($this->eventObserverMock);
99+
}
100+
101+
/**
102+
* Test the method when 'Product Added To Cart' Report is disabled in configuration
103+
*/
104+
public function testExecuteWhenReportsDisabled()
105+
{
106+
$this->reportStatusMock->expects($this->once())
107+
->method('isReportEnabled')
108+
->with(ReportsEventModel::EVENT_PRODUCT_TO_CART)
109+
->willReturn(false);
110+
111+
$this->checkOriginalMethodIsNeverExecuted();
112+
}
113+
114+
/**
115+
* Test when Quote Item has Id
116+
*/
117+
public function testExecuteWithQuoteItemIdSet()
118+
{
119+
$this->configureMocksWhenReportsEnabled();
120+
$this->quoteItemMock->expects($this->any())
121+
->method('getId')
122+
->willReturn(self::STUB_QUOTE_ITEM_ID);
123+
124+
$this->checkOriginalMethodIsNeverExecuted();
125+
}
126+
127+
/**
128+
* Test when Quote Item has Parent Item set
129+
*/
130+
public function testExecuteWithQuoteParentItemIdSet()
131+
{
132+
$this->configureMocksWhenReportsEnabled();
133+
$this->quoteItemMock->expects($this->any())
134+
->method('getParentItem')
135+
->willReturn(self::STUB_QUOTE_PARENT_ITEM_ID);
136+
137+
$this->checkOriginalMethodIsNeverExecuted();
138+
}
139+
140+
/**
141+
* Common mocks assertions when Report is enabled in configuration
142+
*/
143+
private function configureMocksWhenReportsEnabled()
144+
{
145+
$this->reportStatusMock->expects($this->once())
146+
->method('isReportEnabled')
147+
->willReturn(true);
148+
$this->eventObserverMock->expects($this->once())
149+
->method('getEvent')
150+
->willReturn($this->eventMock);
151+
$this->eventMock->expects($this->once())
152+
->method('getItem')
153+
->willReturn($this->quoteItemMock);
154+
}
155+
156+
/**
157+
* Checking that the method will be never executed
158+
*/
159+
private function checkOriginalMethodIsNeverExecuted()
160+
{
161+
$this->eventSaverMock->expects($this->never())->method('save');
162+
$this->observer->execute($this->eventObserverMock);
163+
}
164+
}

0 commit comments

Comments
 (0)