|
| 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 | + /** |
| 28 | + * @var Session|MockObject |
| 29 | + */ |
| 30 | + private $customerSessionMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Visitor|MockObject |
| 34 | + */ |
| 35 | + private $customerVisitorMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var StoreInterface|MockObject |
| 39 | + */ |
| 40 | + private $storeMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var StoreManagerInterface|MockObject |
| 44 | + */ |
| 45 | + private $storeManagerMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var EventFactory|MockObject |
| 49 | + */ |
| 50 | + private $eventFactoryMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Event|MockObject |
| 54 | + */ |
| 55 | + private $eventMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var EventSaver |
| 59 | + */ |
| 60 | + private $eventSaver; |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritDoc |
| 64 | + */ |
| 65 | + public function setUp() |
| 66 | + { |
| 67 | + $this->customerSessionMock = $this->createMock(Session::class); |
| 68 | + $this->customerVisitorMock = $this->createMock(Visitor::class); |
| 69 | + |
| 70 | + $this->storeMock = $this->createMock(StoreInterface::class); |
| 71 | + $this->storeManagerMock = $this->createMock(StoreManagerInterface::class); |
| 72 | + $this->storeManagerMock->expects($this->once()) |
| 73 | + ->method('getStore') |
| 74 | + ->willReturn($this->storeMock); |
| 75 | + $this->storeMock->expects($this->once()) |
| 76 | + ->method('getId') |
| 77 | + ->willReturn(1); |
| 78 | + |
| 79 | + $this->eventMock = $this->createMock(Event::class); |
| 80 | + $this->eventFactoryMock = $this->createMock(EventFactory::class); |
| 81 | + $this->eventFactoryMock->expects($this->once()) |
| 82 | + ->method('create') |
| 83 | + ->willReturn($this->eventMock); |
| 84 | + $this->eventMock->expects($this->once()) |
| 85 | + ->method('setData') |
| 86 | + ->willReturnSelf(); |
| 87 | + $this->eventMock->expects($this->once()) |
| 88 | + ->method('save') |
| 89 | + ->willReturnSelf(); |
| 90 | + |
| 91 | + $objectManagerHelper = new ObjectManager($this); |
| 92 | + $this->eventSaver = $objectManagerHelper->getObject( |
| 93 | + EventSaver::class, |
| 94 | + [ |
| 95 | + '_storeManager' => $this->storeManagerMock, |
| 96 | + '_eventFactory' => $this->eventFactoryMock, |
| 97 | + '_customerSession' => $this->customerSessionMock, |
| 98 | + '_customerVisitor' => $this->customerVisitorMock |
| 99 | + ] |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Test save with subject ID provided |
| 105 | + */ |
| 106 | + public function testSaveWithSubject() |
| 107 | + { |
| 108 | + $subjectId = 5; |
| 109 | + $this->customerSessionMock->expects($this->never()) |
| 110 | + ->method('isLoggedIn'); |
| 111 | + $this->customerSessionMock->expects($this->never()) |
| 112 | + ->method('getCustomerId'); |
| 113 | + $this->customerVisitorMock->expects($this->never()) |
| 114 | + ->method('getId'); |
| 115 | + $this->eventSaver->save(1, 1, $subjectId); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Test save with no subject ID provided |
| 120 | + * |
| 121 | + * @param bool $isLoggedIn |
| 122 | + * @dataProvider dataProviderTestSaveWithoutSubject |
| 123 | + */ |
| 124 | + public function testSaveWithoutSubject(bool $isLoggedIn) |
| 125 | + { |
| 126 | + if ($isLoggedIn) { |
| 127 | + $this->customerSessionMock->expects($this->once()) |
| 128 | + ->method('isLoggedIn') |
| 129 | + ->willReturn(true); |
| 130 | + $this->customerSessionMock->expects($this->once()) |
| 131 | + ->method('getCustomerId') |
| 132 | + ->willReturn(1); |
| 133 | + } else { |
| 134 | + $this->customerSessionMock->expects($this->once()) |
| 135 | + ->method('isLoggedIn') |
| 136 | + ->willReturn(false); |
| 137 | + $this->customerVisitorMock->expects($this->once()) |
| 138 | + ->method('getId') |
| 139 | + ->willReturn(2); |
| 140 | + } |
| 141 | + $this->eventSaver->save(1, 1, null); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Data provider for a case without subjectId provided |
| 146 | + * |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + public function dataProviderTestSaveWithoutSubject() |
| 150 | + { |
| 151 | + return [ |
| 152 | + [true], |
| 153 | + [false] |
| 154 | + ]; |
| 155 | + } |
| 156 | +} |
0 commit comments