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-contact/Test/Unit/Controller/Index/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

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

namespace Magento\Contact\Test\Unit\Controller\Index;

use Magento\Contact\Controller\Index\Post;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\UrlInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
 * @covers \Magento\Contact\Controller\Index\Post
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class PostTest extends TestCase
{
    /**
     * @var Post
     */
    private $controller;

    /**
     * @var RedirectFactory|MockObject
     */
    private $redirectResultFactoryMock;

    /**
     * @var Redirect|MockObject
     */
    private $redirectResultMock;

    /**
     * @var UrlInterface|MockObject
     */
    private $urlMock;

    /**
     * @var Http|MockObject
     */
    private $requestStub;

    /**
     * @var ManagerInterface|MockObject
     */
    private $messageManagerMock;

    /**
     * @var DataPersistorInterface|MockObject
     */
    private $dataPersistorMock;

    /**
     * @var MailInterface|MockObject
     */
    private $mailMock;

    /**
     * test setup
     */
    protected function setUp(): void
    {
        $this->mailMock = $this->getMockBuilder(MailInterface::class)
            ->getMockForAbstractClass();
        $contextMock = $this->createPartialMock(
            Context::class,
            ['getRequest', 'getResponse', 'getResultRedirectFactory', 'getUrl', 'getRedirect', 'getMessageManager']
        );
        $this->urlMock = $this->getMockForAbstractClass(UrlInterface::class);
        $this->messageManagerMock = $this->getMockForAbstractClass(ManagerInterface::class);
        $this->requestStub = $this->createPartialMock(
            Http::class,
            ['getPostValue', 'getParams', 'getParam', 'isPost']
        );

        $this->redirectResultMock = $this->createMock(Redirect::class);
        $this->redirectResultMock->method('setPath')->willReturnSelf();

        $this->redirectResultFactoryMock = $this->createPartialMock(
            RedirectFactory::class,
            ['create']
        );
        $this->redirectResultFactoryMock
            ->method('create')
            ->willReturn($this->redirectResultMock);

        $this->dataPersistorMock = $this->getMockBuilder(DataPersistorInterface::class)
            ->getMockForAbstractClass();

        $contextMock->expects($this->any())
            ->method('getRequest')
            ->willReturn($this->requestStub);
        $contextMock->expects($this->any())
            ->method('getResponse')
            ->willReturn($this->getMockForAbstractClass(ResponseInterface::class));
        $contextMock->expects($this->any())
            ->method('getMessageManager')
            ->willReturn($this->messageManagerMock);
        $contextMock->expects($this->any())
            ->method('getUrl')
            ->willReturn($this->urlMock);
        $contextMock->expects($this->once())
            ->method('getResultRedirectFactory')
            ->willReturn($this->redirectResultFactoryMock);

        $this->controller = (new ObjectManagerHelper($this))->getObject(
            Post::class,
            [
                'context' => $contextMock,
                'mail' => $this->mailMock,
                'dataPersistor' => $this->dataPersistorMock
            ]
        );
    }

    /**
     * Test ExecuteEmptyPost
     */
    public function testExecuteEmptyPost(): void
    {
        $this->stubRequestPostData([]);
        $this->assertSame($this->redirectResultMock, $this->controller->execute());
    }

    /**
     * Test exceute post validation
     *
     * @param array $postData
     * @dataProvider postDataProvider
     */
    public function testExecutePostValidation($postData): void
    {
        $this->stubRequestPostData($postData);
        $this->messageManagerMock->expects($this->once())
            ->method('addErrorMessage');
        $this->dataPersistorMock->expects($this->once())
            ->method('set')
            ->with('contact_us', $postData);

        $this->controller->execute();
    }

    /**
     * Data provider for test exceute post validation
     */
    public function postDataProvider(): array
    {
        return [
            [['name' => null, 'comment' => null, 'email' => '', 'hideit' => 'no']],
            [['name' => 'test', 'comment' => '', 'email' => '', 'hideit' => 'no']],
            [['name' => '', 'comment' => 'test', 'email' => '', 'hideit' => 'no']],
            [['name' => '', 'comment' => '', 'email' => 'test', 'hideit' => 'no']],
            [['name' => '', 'comment' => '', 'email' => '', 'hideit' => 'no']],
            [['name' => 'Name', 'comment' => 'Name', 'email' => 'invalidmail', 'hideit' => 'no']],
        ];
    }

    /**
     * Test ExecuteValidPost
     */
    public function testExecuteValidPost(): void
    {
        $postStub = [
            'name' => 'Name',
            'comment' => 'Comment',
            'email' => '[email protected]',
            'hideit' => ''
        ];

        $this->dataPersistorMock->expects($this->once())
            ->method('clear')
            ->with('contact_us');

        $this->stubRequestPostData($postStub);

        $this->controller->execute();
    }

    /**
     * Stub request for post data
     *
     * @param array $post
     */
    private function stubRequestPostData($post): void
    {
        $this->requestStub
            ->expects($this->once())
            ->method('isPost')
            ->willReturn(!empty($post));
        $this->requestStub->method('getPostValue')->willReturn($post);
        $this->requestStub->method('getParams')->willReturn($post);
        $this->requestStub->method('getParam')->willReturnCallback(
            function ($key, $defaultValue) use ($post) {
                return $post[$key] ?? $defaultValue;
            }
        );
    }
}

Spamworldpro Mini