Skip to content

Commit 8ff9925

Browse files
ENGCOM-6709: Unit test for Magento\Reports\Observer\EventSaver #26525
- Merge Pull Request #26525 from karyna-tsymbal-atwix/magento2:unit-test-reports-observer-eventSaver - Merged commits: 1. 52a66ac 2. e543d0a 3. 5342a25
2 parents 06a0dd0 + 5342a25 commit 8ff9925

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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\Customer\Model\Session;
12+
use Magento\Customer\Model\Visitor;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Reports\Model\Event;
15+
use Magento\Reports\Model\EventFactory;
16+
use Magento\Reports\Observer\EventSaver;
17+
use Magento\Store\Api\Data\StoreInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* Unit Test for @see EventSaver
24+
*/
25+
class EventSaverTest extends TestCase
26+
{
27+
const STUB_CUSTOMER_ID = 1;
28+
const STUB_VISITOR_ID = 2;
29+
const STUB_EVENT_TYPE_ID = 1;
30+
const STUB_OBJECT_ID = 1;
31+
const STUB_STORE_ID = 1;
32+
33+
/**
34+
* @var Session|MockObject
35+
*/
36+
private $customerSessionMock;
37+
38+
/**
39+
* @var Visitor|MockObject
40+
*/
41+
private $customerVisitorMock;
42+
43+
/**
44+
* @var StoreInterface|MockObject
45+
*/
46+
private $storeMock;
47+
48+
/**
49+
* @var StoreManagerInterface|MockObject
50+
*/
51+
private $storeManagerMock;
52+
53+
/**
54+
* @var EventFactory|MockObject
55+
*/
56+
private $eventFactoryMock;
57+
58+
/**
59+
* @var Event|MockObject
60+
*/
61+
private $eventMock;
62+
63+
/**
64+
* @var EventSaver
65+
*/
66+
private $eventSaver;
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function setUp()
72+
{
73+
$this->customerSessionMock = $this->createMock(Session::class);
74+
$this->customerVisitorMock = $this->createMock(Visitor::class);
75+
76+
$this->storeMock = $this->createMock(StoreInterface::class);
77+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
78+
$this->storeManagerMock->expects($this->once())
79+
->method('getStore')
80+
->willReturn($this->storeMock);
81+
$this->storeMock->expects($this->once())
82+
->method('getId')
83+
->willReturn(self::STUB_STORE_ID);
84+
85+
$this->eventMock = $this->createMock(Event::class);
86+
$this->eventFactoryMock = $this->createMock(EventFactory::class);
87+
$this->eventFactoryMock->expects($this->once())
88+
->method('create')
89+
->willReturn($this->eventMock);
90+
$this->eventMock->expects($this->once())
91+
->method('setData')
92+
->willReturnSelf();
93+
$this->eventMock->expects($this->once())
94+
->method('save')
95+
->willReturnSelf();
96+
97+
$objectManagerHelper = new ObjectManager($this);
98+
$this->eventSaver = $objectManagerHelper->getObject(
99+
EventSaver::class,
100+
[
101+
'_storeManager' => $this->storeManagerMock,
102+
'_eventFactory' => $this->eventFactoryMock,
103+
'_customerSession' => $this->customerSessionMock,
104+
'_customerVisitor' => $this->customerVisitorMock
105+
]
106+
);
107+
}
108+
109+
/**
110+
* Test save with subject ID provided
111+
*/
112+
public function testSaveWithSubject()
113+
{
114+
$subjectId = 5;
115+
$this->customerSessionMock->expects($this->never())
116+
->method('isLoggedIn');
117+
$this->customerSessionMock->expects($this->never())
118+
->method('getCustomerId');
119+
$this->customerVisitorMock->expects($this->never())
120+
->method('getId');
121+
$this->eventSaver->save(self::STUB_EVENT_TYPE_ID, self::STUB_OBJECT_ID, $subjectId);
122+
}
123+
124+
/**
125+
* Test save with no subject ID provided and customer is logged in
126+
*/
127+
public function testSaveWithoutSubjectWhenLoggedIn()
128+
{
129+
$this->customerSessionMock->expects($this->once())
130+
->method('isLoggedIn')
131+
->willReturn(true);
132+
$this->customerSessionMock->expects($this->once())
133+
->method('getCustomerId')
134+
->willReturn(self::STUB_CUSTOMER_ID);
135+
$this->eventSaver->save(self::STUB_EVENT_TYPE_ID, self::STUB_OBJECT_ID, null);
136+
}
137+
138+
/**
139+
* Test save with no subject ID provided and customer is not logged in
140+
*/
141+
public function testSaveWithoutSubjectForGuest()
142+
{
143+
$this->customerSessionMock->expects($this->once())
144+
->method('isLoggedIn')
145+
->willReturn(false);
146+
$this->customerVisitorMock->expects($this->once())
147+
->method('getId')
148+
->willReturn(self::STUB_VISITOR_ID);
149+
$this->eventSaver->save(self::STUB_EVENT_TYPE_ID, self::STUB_OBJECT_ID, null);
150+
}
151+
}

0 commit comments

Comments
 (0)