PostTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Contact\Test\Unit\Controller\Index;
  8. use Magento\Contact\Model\ConfigInterface;
  9. use Magento\Contact\Model\MailInterface;
  10. use Magento\Framework\Controller\Result\Redirect;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class PostTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Contact\Controller\Index\Index
  18. */
  19. private $controller;
  20. /**
  21. * @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $configMock;
  24. /**
  25. * @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $redirectResultFactoryMock;
  28. /**
  29. * @var Redirect|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $redirectResultMock;
  32. /**
  33. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $urlMock;
  36. /**
  37. * @var \Magento\Framework\App\Request\HttpRequest|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $requestStub;
  40. /**
  41. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $messageManagerMock;
  44. /**
  45. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $storeManagerMock;
  48. /**
  49. * @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $dataPersistorMock;
  52. /**
  53. * @var MailInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $mailMock;
  56. /**
  57. * test setup
  58. */
  59. protected function setUp()
  60. {
  61. $this->mailMock = $this->getMockBuilder(MailInterface::class)->getMockForAbstractClass();
  62. $this->configMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();
  63. $context = $this->createPartialMock(
  64. \Magento\Framework\App\Action\Context::class,
  65. ['getRequest', 'getResponse', 'getResultRedirectFactory', 'getUrl', 'getRedirect', 'getMessageManager']
  66. );
  67. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  68. $this->messageManagerMock =
  69. $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  70. $this->requestStub = $this->createPartialMock(
  71. \Magento\Framework\App\Request\Http::class,
  72. ['getPostValue', 'getParams', 'getParam', 'isPost']
  73. );
  74. $this->redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
  75. $this->redirectResultMock->method('setPath')->willReturnSelf();
  76. $this->redirectResultFactoryMock = $this->createPartialMock(
  77. \Magento\Framework\Controller\Result\RedirectFactory::class,
  78. ['create']
  79. );
  80. $this->redirectResultFactoryMock
  81. ->method('create')
  82. ->willReturn($this->redirectResultMock);
  83. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  84. $this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
  85. ->getMockForAbstractClass();
  86. $context->expects($this->any())
  87. ->method('getRequest')
  88. ->willReturn($this->requestStub);
  89. $context->expects($this->any())
  90. ->method('getResponse')
  91. ->willReturn($this->createMock(\Magento\Framework\App\ResponseInterface::class));
  92. $context->expects($this->any())
  93. ->method('getMessageManager')
  94. ->willReturn($this->messageManagerMock);
  95. $context->expects($this->any())
  96. ->method('getUrl')
  97. ->willReturn($this->urlMock);
  98. $context->expects($this->once())
  99. ->method('getResultRedirectFactory')
  100. ->willReturn($this->redirectResultFactoryMock);
  101. $this->controller = new \Magento\Contact\Controller\Index\Post(
  102. $context,
  103. $this->configMock,
  104. $this->mailMock,
  105. $this->dataPersistorMock
  106. );
  107. }
  108. /**
  109. * testExecuteEmptyPost
  110. */
  111. public function testExecuteEmptyPost()
  112. {
  113. $this->stubRequestPostData([]);
  114. $this->assertSame($this->redirectResultMock, $this->controller->execute());
  115. }
  116. /**
  117. * @param array $postData
  118. * @param bool $exceptionExpected
  119. * @dataProvider postDataProvider
  120. */
  121. public function testExecutePostValidation($postData, $exceptionExpected)
  122. {
  123. $this->stubRequestPostData($postData);
  124. if ($exceptionExpected) {
  125. $this->messageManagerMock->expects($this->once())
  126. ->method('addErrorMessage');
  127. $this->dataPersistorMock->expects($this->once())
  128. ->method('set')
  129. ->with('contact_us', $postData);
  130. }
  131. $this->controller->execute();
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function postDataProvider()
  137. {
  138. return [
  139. [['name' => null, 'comment' => null, 'email' => '', 'hideit' => 'no'], true],
  140. [['name' => 'test', 'comment' => '', 'email' => '', 'hideit' => 'no'], true],
  141. [['name' => '', 'comment' => 'test', 'email' => '', 'hideit' => 'no'], true],
  142. [['name' => '', 'comment' => '', 'email' => 'test', 'hideit' => 'no'], true],
  143. [['name' => '', 'comment' => '', 'email' => '', 'hideit' => 'no'], true],
  144. [['name' => 'Name', 'comment' => 'Name', 'email' => 'invalidmail', 'hideit' => 'no'], true],
  145. ];
  146. }
  147. /**
  148. * testExecuteValidPost
  149. */
  150. public function testExecuteValidPost()
  151. {
  152. $post = ['name' => 'Name', 'comment' => 'Comment', 'email' => 'valid@mail.com', 'hideit' => null];
  153. $this->dataPersistorMock->expects($this->once())
  154. ->method('clear')
  155. ->with('contact_us');
  156. $this->stubRequestPostData($post);
  157. $this->controller->execute();
  158. }
  159. /**
  160. * @param array $post
  161. */
  162. private function stubRequestPostData($post)
  163. {
  164. $this->requestStub
  165. ->expects($this->once())
  166. ->method('isPost')
  167. ->willReturn(!empty($post));
  168. $this->requestStub->method('getPostValue')->willReturn($post);
  169. $this->requestStub->method('getParams')->willReturn($post);
  170. $this->requestStub->method('getParam')->willReturnCallback(
  171. function ($key) use ($post) {
  172. return $post[$key];
  173. }
  174. );
  175. }
  176. }