123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Contact\Test\Unit\Controller\Index;
- use Magento\Contact\Model\ConfigInterface;
- use Magento\Contact\Model\MailInterface;
- use Magento\Framework\Controller\Result\Redirect;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class PostTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Contact\Controller\Index\Index
- */
- private $controller;
- /**
- * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $configMock;
- /**
- * @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $redirectResultFactoryMock;
- /**
- * @var Redirect|\PHPUnit_Framework_MockObject_MockObject
- */
- private $redirectResultMock;
- /**
- * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $urlMock;
- /**
- * @var \Magento\Framework\App\Request\HttpRequest|\PHPUnit_Framework_MockObject_MockObject
- */
- private $requestStub;
- /**
- * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $messageManagerMock;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeManagerMock;
- /**
- * @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $dataPersistorMock;
- /**
- * @var MailInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $mailMock;
- /**
- * test setup
- */
- protected function setUp()
- {
- $this->mailMock = $this->getMockBuilder(MailInterface::class)->getMockForAbstractClass();
- $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();
- $context = $this->createPartialMock(
- \Magento\Framework\App\Action\Context::class,
- ['getRequest', 'getResponse', 'getResultRedirectFactory', 'getUrl', 'getRedirect', 'getMessageManager']
- );
- $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
- $this->messageManagerMock =
- $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
- $this->requestStub = $this->createPartialMock(
- \Magento\Framework\App\Request\Http::class,
- ['getPostValue', 'getParams', 'getParam', 'isPost']
- );
- $this->redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
- $this->redirectResultMock->method('setPath')->willReturnSelf();
- $this->redirectResultFactoryMock = $this->createPartialMock(
- \Magento\Framework\Controller\Result\RedirectFactory::class,
- ['create']
- );
- $this->redirectResultFactoryMock
- ->method('create')
- ->willReturn($this->redirectResultMock);
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
- $this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
- ->getMockForAbstractClass();
- $context->expects($this->any())
- ->method('getRequest')
- ->willReturn($this->requestStub);
- $context->expects($this->any())
- ->method('getResponse')
- ->willReturn($this->createMock(\Magento\Framework\App\ResponseInterface::class));
- $context->expects($this->any())
- ->method('getMessageManager')
- ->willReturn($this->messageManagerMock);
- $context->expects($this->any())
- ->method('getUrl')
- ->willReturn($this->urlMock);
- $context->expects($this->once())
- ->method('getResultRedirectFactory')
- ->willReturn($this->redirectResultFactoryMock);
- $this->controller = new \Magento\Contact\Controller\Index\Post(
- $context,
- $this->configMock,
- $this->mailMock,
- $this->dataPersistorMock
- );
- }
- /**
- * testExecuteEmptyPost
- */
- public function testExecuteEmptyPost()
- {
- $this->stubRequestPostData([]);
- $this->assertSame($this->redirectResultMock, $this->controller->execute());
- }
- /**
- * @param array $postData
- * @param bool $exceptionExpected
- * @dataProvider postDataProvider
- */
- public function testExecutePostValidation($postData, $exceptionExpected)
- {
- $this->stubRequestPostData($postData);
- if ($exceptionExpected) {
- $this->messageManagerMock->expects($this->once())
- ->method('addErrorMessage');
- $this->dataPersistorMock->expects($this->once())
- ->method('set')
- ->with('contact_us', $postData);
- }
- $this->controller->execute();
- }
- /**
- * @return array
- */
- public function postDataProvider()
- {
- return [
- [['name' => null, 'comment' => null, 'email' => '', 'hideit' => 'no'], true],
- [['name' => 'test', 'comment' => '', 'email' => '', 'hideit' => 'no'], true],
- [['name' => '', 'comment' => 'test', 'email' => '', 'hideit' => 'no'], true],
- [['name' => '', 'comment' => '', 'email' => 'test', 'hideit' => 'no'], true],
- [['name' => '', 'comment' => '', 'email' => '', 'hideit' => 'no'], true],
- [['name' => 'Name', 'comment' => 'Name', 'email' => 'invalidmail', 'hideit' => 'no'], true],
- ];
- }
- /**
- * testExecuteValidPost
- */
- public function testExecuteValidPost()
- {
- $post = ['name' => 'Name', 'comment' => 'Comment', 'email' => 'valid@mail.com', 'hideit' => null];
- $this->dataPersistorMock->expects($this->once())
- ->method('clear')
- ->with('contact_us');
- $this->stubRequestPostData($post);
- $this->controller->execute();
- }
- /**
- * @param array $post
- */
- private function stubRequestPostData($post)
- {
- $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) use ($post) {
- return $post[$key];
- }
- );
- }
- }
|