1

I am trying to write integration Test for my custom module that have very basic functionality to block the frontend page.

1 . created one controller to submit the password and on that basis we return the user on home page to allow access the sites and if failed on password then it keeps on the same password page

controller code like below

<?php
declare(strict_types=1);
namespace CustomWeb\FrontendStop\Controller\Index;

use CustomWeb\FrontendStop\Helper\Config;
use CustomWeb\FrontendStop\Service\FrontendStopCookie;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Message\ManagerInterface;

class Submit implements HttpPostActionInterface
{
   public function __construct(
       protected Config $helper,
       protected FrontendStopCookie $service,
       protected ResultFactory $resultFactory,
       protected RequestInterface $request,
       protected ManagerInterface $messageManager
   ) {
   }

   public function execute():ResultInterface
   {
       $password = $this->request->getParam('password');
       $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

       if (empty($password)) {
           $this->messageManager->addErrorMessage('Please provide a valid password.');
           $resultRedirect->setUrl('/frontendblock/index/index');
           return $resultRedirect;
       }

       if (!$this->helper->isValidPassword($password)) {
           $this->messageManager->addErrorMessage('Please provide a valid password.');
           $resultRedirect->setUrl('/frontendblock/index/index');
           return $resultRedirect;
       }
       $this->service->setIsAuthenticated();
       $this->messageManager->addSuccessMessage('Successfully Authenticated!');
       $resultRedirect->setUrl('/home');

       return $resultRedirect;
   }
}

I would like to write the Integration Test for this class, for any help much appreciated.

1 Answer 1

1

I havent tested it but it should look something like this:

<?php
declare(strict_types=1);

namespace CustomWeb\FrontendStop\Test\Controller\Index;

use CustomWeb\FrontendStop\Controller\Index\Submit;
use CustomWeb\FrontendStop\Helper\Config;
use CustomWeb\FrontendStop\Service\FrontendStopCookie;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class SubmitTest extends TestCase
{
    /**
     * @var Submit
     */
    private $submit;

    /**
     * @var Config|MockObject
     */
    private $configMock;

    /**
     * @var FrontendStopCookie|MockObject
     */
    private $cookieMock;

    /**
     * @var RequestInterface|MockObject
     */
    private $requestMock;

    /**
     * @var ResultFactory|MockObject
     */
    private $resultFactoryMock;

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

    /**
     * @var ResultInterface|MockObject
     */
    private $resultRedirectMock;

    protected function setUp(): void
    {
        $objectManager = new ObjectManager($this);

        $this->configMock = $this->createMock(Config::class);
        $this->cookieMock = $this->createMock(FrontendStopCookie::class);
        $this->requestMock = $this->createMock(RequestInterface::class);
        $this->resultFactoryMock = $this->createMock(ResultFactory::class);
        $this->messageManagerMock = $this->createMock(ManagerInterface::class);
        $this->resultRedirectMock = $this->createMock(ResultInterface::class);

        $this->submit = $objectManager->getObject(
            Submit::class,
            [
                'helper' => $this->configMock,
                'service' => $this->cookieMock,
                'resultFactory' => $this->resultFactoryMock,
                'request' => $this->requestMock,
                'messageManager' => $this->messageManagerMock,
            ]
        );
    }

    /**
     * @dataProvider executeDataProvider
     */
    public function testExecute($password, $isValid, $redirectUrl, $messageType, $message): void
    {
        $this->requestMock->method('getParam')
            ->willReturn($password);

        $this->resultFactoryMock->method('create')
            ->willReturn($this->resultRedirectMock);

        $this->configMock->method('isValidPassword')
            ->willReturn($isValid);

        $this->messageManagerMock->expects($this->once())
            ->method($messageType)
            ->with($message);

        $this->resultRedirectMock->expects($this->once())
            ->method('setUrl')
            ->with($redirectUrl);

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

    public function executeDataProvider(): array
    {
        return [
            'empty password' => [
                'password' => '',
                'isValid' => false,
                'redirectUrl' => '/frontendblock/index/index',
                'messageType' => 'addErrorMessage',
                'message' => 'Please provide a valid password.'
            ],
            'invalid password' => [
                'password' => 'invalid',
                'isValid' => false,
                'redirectUrl' => '/frontendblock/index/index',
                'messageType' => 'addErrorMessage',
                'message' => 'Please provide a valid password.'
            ],
            'valid password' => [
                'password' => 'validPassword',
                'isValid' => true,
                'redirectUrl' => '/home',
                'messageType' => 'addSuccessMessage',
                'message' => 'Successfully Authenticated!'
            ],
        ];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.