/** * {@inheritDoc} */ public function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->productIndexFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\ViewedFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->productIndexMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\Viewed')->disableOriginalConstructor()->getMock(); $this->productIndexFactoryMock->expects($this->any())->method('create')->willReturn($this->productIndexMock); $this->productCompModelMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\Compared')->disableOriginalConstructor()->getMock(); $this->productCompFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\ComparedFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->productCompFactoryMock->expects($this->any())->method('create')->willReturn($this->productCompModelMock); $this->observer = $objectManager->getObject('Magento\\Reports\\Observer\\CustomerLogoutObserver', ['productIndxFactory' => $this->productIndexFactoryMock, 'productCompFactory' => $this->productCompFactoryMock]); }
/** * Add Product to Compare Products List action * * Reset count of compared products cache * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { $productId = $observer->getEvent()->getProduct()->getId(); $viewData = ['product_id' => $productId]; if ($this->_customerSession->isLoggedIn()) { $viewData['customer_id'] = $this->_customerSession->getCustomerId(); } else { $viewData['visitor_id'] = $this->_customerVisitor->getId(); } $this->_productCompFactory->create()->setData($viewData)->save()->calculate(); $this->eventSaver->save(\Magento\Reports\Model\Event::EVENT_PRODUCT_COMPARE, $productId); }
/** * Customer login action * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->_customerSession->isLoggedIn()) { return $this; } $visitorId = $this->_customerVisitor->getId(); $customerId = $this->_customerSession->getCustomerId(); $eventModel = $this->_eventFactory->create(); $eventModel->updateCustomerType($visitorId, $customerId); $this->_productCompFactory->create()->updateCustomerFromVisitor()->calculate(); $this->_productIndexFactory->create()->updateCustomerFromVisitor()->calculate(); return $this; }
/** * {@inheritDoc} */ protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->customerSessionMock = $this->getMockBuilder('Magento\\Customer\\Model\\Session')->disableOriginalConstructor()->getMock(); $this->customerVisitorMock = $this->getMockBuilder('Magento\\Customer\\Model\\Visitor')->disableOriginalConstructor()->getMock(); $this->productIndexFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\ViewedFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->productIndexMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\Viewed')->disableOriginalConstructor()->getMock(); $this->productIndexFactoryMock->expects($this->any())->method('create')->willReturn($this->productIndexMock); $reportEventFactory = $this->getMockBuilder('Magento\\Reports\\Model\\EventFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->reportEventMock = $this->getMockBuilder('Magento\\Reports\\Model\\Event')->disableOriginalConstructor()->getMock(); $reportEventFactory->expects($this->any())->method('create')->willReturn($this->reportEventMock); $this->productCompModelMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\Compared')->disableOriginalConstructor()->getMock(); $this->productCompFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\ComparedFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->productCompFactoryMock->expects($this->any())->method('create')->willReturn($this->productCompModelMock); $this->observer = $objectManager->getObject('Magento\\Reports\\Observer\\CustomerLoginObserver', ['event' => $reportEventFactory, 'productCompFactory' => $this->productCompFactoryMock, 'productIndexFactory' => $this->productIndexFactoryMock, 'customerSession' => $this->customerSessionMock, 'customerVisitor' => $this->customerVisitorMock]); }
/** * {@inheritDoc} */ public function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->customerSessionMock = $this->getMockBuilder('Magento\\Customer\\Model\\Session')->disableOriginalConstructor()->getMock(); $this->customerVisitorMock = $this->getMockBuilder('Magento\\Customer\\Model\\Visitor')->disableOriginalConstructor()->getMock(); $reportEventFactory = $this->getMockBuilder('Magento\\Reports\\Model\\EventFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->reportEventMock = $this->getMockBuilder('Magento\\Reports\\Model\\Event')->disableOriginalConstructor()->getMock(); $reportEventFactory->expects($this->any())->method('create')->willReturn($this->reportEventMock); /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject $storeManager */ $storeManager = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface'); $this->storeMock = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $storeManager->expects($this->any())->method('getStore')->willReturn($this->storeMock); $this->productCompModelMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\Compared')->disableOriginalConstructor()->getMock(); $this->productCompFactoryMock = $this->getMockBuilder('Magento\\Reports\\Model\\Product\\Index\\ComparedFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->productCompFactoryMock->expects($this->any())->method('create')->willReturn($this->productCompModelMock); $this->eventSaverMock = $this->getMockBuilder('\\Magento\\Reports\\Observer\\EventSaver')->disableOriginalConstructor()->setMethods(['save'])->getMock(); $this->observer = $objectManager->getObject('Magento\\Reports\\Observer\\CatalogProductCompareAddProductObserver', ['productCompFactory' => $this->productCompFactoryMock, 'customerSession' => $this->customerSessionMock, 'customerVisitor' => $this->customerVisitorMock, 'eventSaver' => $this->eventSaverMock]); }
/** * @param bool $isLoggedIn * @param string $userKey * @param int $userId * @dataProvider catalogProductCompareAddProductDataProvider */ public function testCatalogProductCompareAddProduct($isLoggedIn, $userKey, $userId) { $productId = 111; $customerId = 222; $visitorId = 333; $viewData = ['product_id' => $productId, $userKey => $userId]; $observerMock = $this->getObserverMock($productId); $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn($isLoggedIn); $this->customerSessionMock->expects($this->any())->method('getCustomerId')->willReturn($customerId); $this->customerVisitorMock->expects($this->any())->method('getId')->willReturn($visitorId); $this->productCompFactoryMock->expects($this->any())->method('create')->willReturn($this->productCompModelMock); $this->productCompModelMock->expects($this->any())->method('setData')->with($viewData)->willReturnSelf(); $this->productCompModelMock->expects($this->any())->method('save')->willReturnSelf(); $this->productCompModelMock->expects($this->any())->method('calculate')->willReturnSelf(); $this->assertEquals($this->observer, $this->observer->catalogProductCompareAddProduct($observerMock)); }
/** * Customer logout processing * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { $this->_productCompFactory->create()->purgeVisitorByCustomer()->calculate(); $this->_productIndxFactory->create()->purgeVisitorByCustomer()->calculate(); }
/** * Remove All Products from Compare Products * * Reset count of compared products cache * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { $this->_productCompFactory->create()->calculate(); return $this; }
/** * Add Product to Compare Products List action * * Reset count of compared products cache * * @param \Magento\Framework\Event\Observer $observer * @return Observer */ public function catalogProductCompareAddProduct(\Magento\Framework\Event\Observer $observer) { $productId = $observer->getEvent()->getProduct()->getId(); $this->_productCompFactory->create()->setProductId($productId)->save()->calculate(); return $this->_event(\Magento\Reports\Model\Event::EVENT_PRODUCT_COMPARE, $productId); }