Skip to content

Commit c877563

Browse files
committed
Unit test for Magento\Reports\Observer\CheckoutCartAddProductObserver
1 parent 4e6f6ae commit c877563

File tree

1 file changed

+161
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)