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