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-search/Test/Unit/Block/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/magento/module-search/Test/Unit/Block/TermsTest.php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Search\Test\Unit\Block;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Url;
use Magento\Framework\UrlFactory;
use Magento\Framework\View\Element\Template\Context;
use Magento\Search\Block\Term;
use Magento\Search\Model\Query;
use Magento\Search\Model\ResourceModel\Query\Collection;
use Magento\Search\Model\ResourceModel\Query\CollectionFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
 * Tests for Terms block
 */
class TermsTest extends TestCase
{
    /**
     * @var Context|MockObject
     */
    private $contextMock;

    /**
     * @var CollectionFactory|MockObject
     */
    private $collectionFactoryMock;

    /**
     * @var UrlFactory|MockObject
     */
    private $urlFactoryMock;

    /**
     * @var Term
     */
    private $termsModel;

    /**
     * @var StoreManager
     */
    private $storeManagerMock;

    /**
     * @inheritdoc
     */
    protected function setUp(): void
    {
        $objectManager = new ObjectManager($this);

        $this->contextMock = $this->createMock(Context::class);
        $this->collectionFactoryMock = $this->createMock(CollectionFactory::class);
        $this->urlFactoryMock = $this->createMock(UrlFactory::class);
        $this->storeManagerMock = $this->createMock(StoreManager::class);

        $this->contextMock->expects($this->once())
            ->method('getStoreManager')
            ->willReturn($this->storeManagerMock);
        $this->termsModel = $objectManager->getObject(
            Term::class,
            [
                'context' => $this->contextMock,
                '_queryCollectionFactory' => $this->collectionFactoryMock,
                '_urlFactory' => $this->urlFactoryMock
            ]
        );
    }

    /**
     * Verify terms
     *
     * @dataProvider termKeysProvider
     * @param string $termKey
     * @param bool $popularity
     */
    public function testGetTerms(string $termKey, bool $popularity): void
    {
        $terms = $this->createMock(Collection::class);
        $dataObjectMock = $this->getMockBuilder(Query::class)
            ->disableOriginalConstructor()
            ->setMethods(['getPopularity', 'getQueryText'])
            ->getMock();
        $storeMock = $this->createMock(Store::class);

        $this->storeManagerMock->expects($this->once())
            ->method('getStore')
            ->willReturn($storeMock);
        $storeMock->expects($this->once())
            ->method('getId')
            ->willReturn(1);

        $this->collectionFactoryMock->expects($this->once())
            ->method('create')
            ->willReturn($terms);
        $terms->expects($this->once())
            ->method('setPopularQueryFilter')
            ->willReturnSelf();
        $terms->expects($this->once())
            ->method('setPageSize')
            ->willReturnSelf();
        $terms->expects($this->once())
            ->method('load')
            ->willReturnSelf();
        $terms->expects($this->once())
            ->method('getItems')
            ->willReturn([$dataObjectMock]);
        $dataObjectMock->expects($this->exactly(!$popularity ? 3 : 4))
            ->method('getPopularity')
            ->willReturn($popularity);
        $dataObjectMock->expects($this->exactly(!$popularity ? 0 : 2))
            ->method('getQueryText')
            ->willReturn($termKey);

        $this->assertEquals(!$popularity ? [] : [$termKey => $dataObjectMock], $this->termsModel->getTerms());
    }

    /**
     * Verify get search Url
     *
     * @return void
     */
    public function testGetSearchResult(): void
    {
        $urlMock = $this->getMockBuilder(Url::class)
            ->disableOriginalConstructor()
            ->setMethods(['setQueryParam', 'getUrl'])
            ->getMock();

        $dataObjectMock = $this->getMockBuilder(Query::class)
            ->disableOriginalConstructor()
            ->setMethods(['getPopularity', 'getQueryText'])
            ->getMock();
        $this->urlFactoryMock->expects($this->once())
            ->method('create')
            ->willReturn($urlMock);
        $dataObjectMock->expects($this->once())
            ->method('getQueryText')
            ->willReturn('url');
        $urlMock->expects($this->once())->method('setQueryParam');
        $urlMock->expects($this->once())
            ->method('getUrl')
            ->with('catalogsearch/result')
            ->willReturn('url');

        $this->assertEquals('url', $this->termsModel->getSearchUrl($dataObjectMock));
    }

    /**
     * Terms data key provider
     *
     * @return array
     */
    public function termKeysProvider(): array
    {
        return [
            [
                'search',
                true
            ],
            [
                '',
                false
            ]
        ];
    }
}

Spamworldpro Mini