SaveTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Test\Unit\Controller\Adminhtml\Term;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class SaveTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  11. private $request;
  12. /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
  13. private $redirect;
  14. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  15. private $messageManager;
  16. /** @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  17. private $session;
  18. /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  19. private $context;
  20. /** @var \Magento\Search\Model\Query|\PHPUnit_Framework_MockObject_MockObject */
  21. private $query;
  22. /** @var \Magento\Search\Controller\Adminhtml\Term\Save */
  23. private $controller;
  24. protected function setUp()
  25. {
  26. $objectManagerHelper = new ObjectManagerHelper($this);
  27. $this->context = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->redirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  31. ->setMethods(['setPath'])
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $redirectFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
  35. ->setMethods(['create'])
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $redirectFactory->expects($this->any())
  39. ->method('create')
  40. ->will($this->returnValue($this->redirect));
  41. $this->context->expects($this->any())
  42. ->method('getResultRedirectFactory')
  43. ->willReturn($redirectFactory);
  44. $this->context->expects($this->any())
  45. ->method('getResultFactory')
  46. ->willReturn($redirectFactory);
  47. $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  48. ->disableOriginalConstructor()
  49. ->setMethods(['getPostValue', 'isPost', 'getPost'])
  50. ->getMockForAbstractClass();
  51. $this->context->expects($this->atLeastOnce())
  52. ->method('getRequest')
  53. ->willReturn($this->request);
  54. $objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  55. ->disableOriginalConstructor()
  56. ->setMethods(['create'])
  57. ->getMockForAbstractClass();
  58. $this->context->expects($this->any())
  59. ->method('getObjectManager')
  60. ->willReturn($objectManager);
  61. $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods(['addSuccessMessage', 'addErrorMessage', 'addExceptionMessage'])
  64. ->getMockForAbstractClass();
  65. $this->context->expects($this->any())
  66. ->method('getMessageManager')
  67. ->willReturn($this->messageManager);
  68. $this->session = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(['setPageData'])
  71. ->getMock();
  72. $this->context->expects($this->any())
  73. ->method('getSession')
  74. ->willReturn($this->session);
  75. $this->query = $this->getMockBuilder(\Magento\Search\Model\Query::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods(['getId', 'load', 'addData', 'setIsProcessed', 'save', 'loadByQueryText', 'setStoreId'])
  78. ->getMock();
  79. $queryFactory = $this->getMockBuilder(\Magento\Search\Model\QueryFactory::class)
  80. ->setMethods(['create'])
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $queryFactory->expects($this->any())
  84. ->method('create')
  85. ->will($this->returnValue($this->query));
  86. $this->controller = $objectManagerHelper->getObject(
  87. \Magento\Search\Controller\Adminhtml\Term\Save::class,
  88. [
  89. 'context' => $this->context,
  90. 'queryFactory' => $queryFactory,
  91. ]
  92. );
  93. }
  94. /**
  95. * @param bool $isPost
  96. * @param array $data
  97. * @dataProvider executeIsPostDataDataProvider
  98. */
  99. public function testExecuteIsPostData($isPost, $data)
  100. {
  101. $this->request->expects($this->at(0))->method('getPostValue')->willReturn($data);
  102. $this->request->expects($this->at(1))->method('isPost')->willReturn($isPost);
  103. $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
  104. $this->assertSame($this->redirect, $this->controller->execute());
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function executeIsPostDataDataProvider()
  110. {
  111. return [
  112. [false, ['0' => '0']],
  113. [true, []]
  114. ];
  115. }
  116. public function testExecuteLoadQueryQueryId()
  117. {
  118. $queryId = 1;
  119. $queryText = '';
  120. $this->mockGetRequestData($queryText, $queryId);
  121. $this->query->expects($this->once())->method('getId')->willReturn(false);
  122. $this->query->expects($this->once())->method('load')->with($queryId);
  123. $this->messageManager->expects($this->once())->method('addSuccessMessage');
  124. $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
  125. $this->assertSame($this->redirect, $this->controller->execute());
  126. }
  127. public function testExecuteLoadQueryQueryIdQueryText()
  128. {
  129. $queryId = 1;
  130. $queryText = 'search';
  131. $this->mockGetRequestData($queryText, $queryId);
  132. $this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
  133. $this->query->expects($this->once())->method('setStoreId');
  134. $this->query->expects($this->once())->method('loadByQueryText')->with($queryText);
  135. $this->query->expects($this->any())->method('getId')->willReturn($queryId);
  136. $this->messageManager->expects($this->once())->method('addSuccessMessage');
  137. $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
  138. $this->assertSame($this->redirect, $this->controller->execute());
  139. }
  140. public function testExecuteLoadQueryQueryIdQueryText2()
  141. {
  142. $queryId = 1;
  143. $queryText = 'search';
  144. $this->mockGetRequestData($queryText, $queryId);
  145. $this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
  146. $this->query->expects($this->once())->method('setStoreId');
  147. $this->query->expects($this->once())->method('loadByQueryText')->with($queryText);
  148. $this->query->expects($this->any())->method('getId')->willReturn(false);
  149. $this->query->expects($this->once())->method('load')->with($queryId);
  150. $this->messageManager->expects($this->once())->method('addSuccessMessage');
  151. $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
  152. $this->assertSame($this->redirect, $this->controller->execute());
  153. }
  154. public function testExecuteLoadQueryQueryIdQueryTextException()
  155. {
  156. $queryId = 1;
  157. $anotherQueryId = 2;
  158. $queryText = 'search';
  159. $this->mockGetRequestData($queryText, $queryId);
  160. $this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
  161. $this->query->expects($this->once())->method('setStoreId');
  162. $this->query->expects($this->once())->method('loadByQueryText')->with($queryText);
  163. $this->query->expects($this->any())->method('getId')->willReturn($anotherQueryId);
  164. $this->messageManager->expects($this->once())->method('addErrorMessage');
  165. $this->session->expects($this->once())->method('setPageData');
  166. $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
  167. $this->assertSame($this->redirect, $this->controller->execute());
  168. }
  169. public function testExecuteException()
  170. {
  171. $queryId = 1;
  172. $queryText = 'search';
  173. $this->mockGetRequestData($queryText, $queryId);
  174. $this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
  175. $this->query->expects($this->once())->method('setStoreId');
  176. $this->query->expects($this->once())->method('loadByQueryText')->willThrowException(new \Exception());
  177. $this->messageManager->expects($this->once())->method('addExceptionMessage');
  178. $this->session->expects($this->once())->method('setPageData');
  179. $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
  180. $this->assertSame($this->redirect, $this->controller->execute());
  181. }
  182. /**
  183. * @param string $queryText
  184. * @param int $queryId
  185. */
  186. private function mockGetRequestData($queryText, $queryId)
  187. {
  188. $this->request->expects($this->at(0))->method('getPostValue')->willReturn(['0' => '0']);
  189. $this->request->expects($this->at(1))->method('isPost')->willReturn(true);
  190. $this->request->expects($this->at(2))->method('getPost')->with('query_text', false)->willReturn($queryText);
  191. $this->request->expects($this->at(3))->method('getPost')->with('query_id', null)->willReturn($queryId);
  192. }
  193. }