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-elasticsearch/Test/Unit/Setup/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

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

namespace Magento\Elasticsearch\Test\Unit\Setup;

use Magento\AdvancedSearch\Model\Client\ClientResolver;
use Magento\Elasticsearch\Setup\Validator;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ValidatorTest extends TestCase
{
    /**
     * @var Validator
     */
    private $validator;

    /**
     * @var ClientResolver|MockObject
     */
    private $clientResolverMock;

    /**
     * @var Elasticsearch|MockObject
     */
    private $elasticsearchClientMock;

    protected function setUp(): void
    {
        $this->clientResolverMock = $this->getMockBuilder(ClientResolver::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->elasticsearchClientMock = $this->getMockBuilder(Elasticsearch::class)
            ->disableOriginalConstructor()
            ->getMock();

        $objectManager = new ObjectManager($this);
        $this->validator = $objectManager->getObject(
            Validator::class,
            [
                'clientResolver' => $this->clientResolverMock
            ]
        );
    }

    public function testValidate()
    {
        $this->clientResolverMock
            ->expects($this->once())
            ->method('create')
            ->willReturn($this->elasticsearchClientMock);
        $this->elasticsearchClientMock->expects($this->once())->method('testConnection')->willReturn(true);

        $this->assertEquals([], $this->validator->validate());
    }

    public function testValidateFail()
    {
        $this->clientResolverMock
            ->expects($this->once())
            ->method('create')
            ->willReturn($this->elasticsearchClientMock);
        $this->elasticsearchClientMock->expects($this->once())->method('testConnection')->willReturn(false);

        $expected = [
            'Could not validate a connection to Elasticsearch.'
            . ' Verify that the Elasticsearch host and port are configured correctly.'
        ];
        $this->assertEquals($expected, $this->validator->validate());
    }

    public function testValidateFailException()
    {
        $this->clientResolverMock
            ->expects($this->once())
            ->method('create')
            ->willReturn($this->elasticsearchClientMock);
        $exceptionMessage = 'Could not ping host.';
        $this->elasticsearchClientMock
            ->expects($this->once())
            ->method('testConnection')
            ->willThrowException(new \Exception($exceptionMessage));

        $expected = ['Could not validate a connection to Elasticsearch. ' . $exceptionMessage];
        $this->assertEquals($expected, $this->validator->validate());
    }
}

Spamworldpro Mini