Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/vendor/magento/module-reports/Test/Unit/Observer/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/magento/module-reports/Test/Unit/Observer/EventSaverTest.php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

namespace Magento\Reports\Test\Unit\Observer;

use Magento\Customer\Model\Session;
use Magento\Customer\Model\Visitor;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Reports\Model\Event;
use Magento\Reports\Model\EventFactory;
use Magento\Reports\Observer\EventSaver;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
 * Unit Test for @see EventSaver
 */
class EventSaverTest extends TestCase
{
    const STUB_CUSTOMER_ID = 1;
    const STUB_VISITOR_ID = 2;
    const STUB_EVENT_TYPE_ID = 1;
    const STUB_OBJECT_ID = 1;
    const STUB_STORE_ID = 1;

    /**
     * @var Session|MockObject
     */
    private $customerSessionMock;

    /**
     * @var Visitor|MockObject
     */
    private $customerVisitorMock;

    /**
     * @var StoreInterface|MockObject
     */
    private $storeMock;

    /**
     * @var StoreManagerInterface|MockObject
     */
    private $storeManagerMock;

    /**
     * @var EventFactory|MockObject
     */
    private $eventFactoryMock;

    /**
     * @var Event|MockObject
     */
    private $eventMock;

    /**
     * @var EventSaver
     */
    private $eventSaver;

    /**
     * @inheritDoc
     */
    protected function setUp(): void
    {
        $this->customerSessionMock = $this->createMock(Session::class);
        $this->customerVisitorMock = $this->createMock(Visitor::class);

        $this->storeMock = $this->getMockForAbstractClass(StoreInterface::class);
        $this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
        $this->storeManagerMock->expects($this->once())
            ->method('getStore')
            ->willReturn($this->storeMock);
        $this->storeMock->expects($this->once())
            ->method('getId')
            ->willReturn(self::STUB_STORE_ID);

        $this->eventMock = $this->createMock(Event::class);
        $this->eventFactoryMock = $this->createMock(EventFactory::class);
        $this->eventFactoryMock->expects($this->once())
            ->method('create')
            ->willReturn($this->eventMock);
        $this->eventMock->expects($this->once())
            ->method('setData')
            ->willReturnSelf();
        $this->eventMock->expects($this->once())
            ->method('save')
            ->willReturnSelf();

        $objectManagerHelper = new ObjectManager($this);
        $this->eventSaver = $objectManagerHelper->getObject(
            EventSaver::class,
            [
                '_storeManager' => $this->storeManagerMock,
                '_eventFactory' => $this->eventFactoryMock,
                '_customerSession' => $this->customerSessionMock,
                '_customerVisitor' => $this->customerVisitorMock
            ]
        );
    }

    /**
     * Test save with subject ID provided
     */
    public function testSaveWithSubject()
    {
        $subjectId = 5;
        $this->customerSessionMock->expects($this->never())
            ->method('isLoggedIn');
        $this->customerSessionMock->expects($this->never())
            ->method('getCustomerId');
        $this->customerVisitorMock->expects($this->never())
            ->method('getId');
        $this->eventSaver->save(self::STUB_EVENT_TYPE_ID, self::STUB_OBJECT_ID, $subjectId);
    }

    /**
     * Test save with no subject ID provided and customer is logged in
     */
    public function testSaveWithoutSubjectWhenLoggedIn()
    {
        $this->customerSessionMock->expects($this->once())
            ->method('isLoggedIn')
            ->willReturn(true);
        $this->customerSessionMock->expects($this->once())
            ->method('getCustomerId')
            ->willReturn(self::STUB_CUSTOMER_ID);
        $this->eventSaver->save(self::STUB_EVENT_TYPE_ID, self::STUB_OBJECT_ID, null);
    }

    /**
     * Test save with no subject ID provided and customer is not logged in
     */
    public function testSaveWithoutSubjectForGuest()
    {
        $this->customerSessionMock->expects($this->once())
            ->method('isLoggedIn')
            ->willReturn(false);
        $this->customerVisitorMock->expects($this->once())
            ->method('getId')
            ->willReturn(self::STUB_VISITOR_ID);
        $this->eventSaver->save(self::STUB_EVENT_TYPE_ID, self::STUB_OBJECT_ID, null);
    }
}

Spamworldpro Mini