QueryFactoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Test\Unit\Model;
  7. use Magento\Search\Helper\Data;
  8. use Magento\Framework\App\Helper\Context;
  9. use Magento\Framework\App\RequestInterface;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  12. use Magento\Search\Model\QueryFactory;
  13. use Magento\Framework\Stdlib\StringUtils;
  14. use Magento\Search\Model\Query;
  15. /**
  16. * Class QueryFactoryTest tests Magento\Search\Model\QueryFactory
  17. *
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class QueryFactoryTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var QueryFactory
  24. */
  25. private $model;
  26. /**
  27. * @var Data|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $queryHelper;
  30. /**
  31. * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $request;
  34. /**
  35. * @var StringUtils|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $string;
  38. /**
  39. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $objectManager;
  42. /**
  43. * @var Query|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $query;
  46. /**
  47. * SetUp method
  48. */
  49. protected function setUp()
  50. {
  51. $this->queryHelper = $this->getMockBuilder(Data::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->request = $this->getMockBuilder(RequestInterface::class)
  55. ->disableOriginalConstructor()
  56. ->getMockForAbstractClass();
  57. $this->string = $this->getMockBuilder(StringUtils::class)
  58. ->setMethods(['substr', 'strlen', 'cleanString'])
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->query = $this->getMockBuilder(Query::class)
  62. ->setMethods(['setIsQueryTextExceeded', 'setIsQueryTextShort', 'loadByQueryText', 'getId', 'setQueryText'])
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
  66. ->disableOriginalConstructor()
  67. ->getMockForAbstractClass();
  68. /** @var Context|\PHPUnit_Framework_MockObject_MockObject $context */
  69. $context = $this->getMockBuilder(Context::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $context->expects($this->any())
  73. ->method('getRequest')
  74. ->willReturn($this->request);
  75. $this->model = (new ObjectManager($this))->getObject(
  76. QueryFactory::class,
  77. [
  78. 'queryHelper' => $this->queryHelper,
  79. 'context' => $context,
  80. 'string' => $this->string,
  81. 'objectManager' => $this->objectManager
  82. ]
  83. );
  84. }
  85. /**
  86. * Test for create method
  87. */
  88. public function testCreate()
  89. {
  90. $data = [1, 2, 3];
  91. $this->objectManager->expects($this->once())
  92. ->method('create')
  93. ->withConsecutive([Query::class, $data])
  94. ->willReturn($this->query);
  95. $result = $this->model->create($data);
  96. $this->assertSame($this->query, $result);
  97. }
  98. /**
  99. * Test for get new query method
  100. */
  101. public function testGetNewQuery()
  102. {
  103. $queryId = 123;
  104. $maxQueryLength = 100;
  105. $minQueryLength = 3;
  106. $rawQueryText = ' Simple product ';
  107. $cleanedRawText = 'Simple product';
  108. $isQueryTextExceeded = false;
  109. $isQueryTextShort = false;
  110. $this->mockSetQueryTextNeverExecute($cleanedRawText);
  111. $this->mockString($cleanedRawText);
  112. $this->mockQueryLengths($maxQueryLength, $minQueryLength);
  113. $this->mockGetRawQueryText($rawQueryText);
  114. $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
  115. $this->mockCreateQuery();
  116. $result = $this->model->get();
  117. $this->assertSame($this->query, $result);
  118. }
  119. /**
  120. * Test for get query twice method
  121. */
  122. public function testGetQueryTwice()
  123. {
  124. $queryId = 123;
  125. $maxQueryLength = 100;
  126. $minQueryLength = 3;
  127. $rawQueryText = ' Simple product ';
  128. $cleanedRawText = 'Simple product';
  129. $isQueryTextExceeded = false;
  130. $isQueryTextShort = false;
  131. $this->mockSetQueryTextNeverExecute($cleanedRawText);
  132. $this->mockString($cleanedRawText);
  133. $this->mockQueryLengths($maxQueryLength, $minQueryLength);
  134. $this->mockGetRawQueryText($rawQueryText);
  135. $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
  136. $this->mockCreateQuery();
  137. $result = $this->model->get();
  138. $this->assertSame($this->query, $result, 'After first execution queries are not same');
  139. $result = $this->model->get();
  140. $this->assertSame($this->query, $result, 'After second execution queries are not same');
  141. }
  142. /**
  143. * Test for get query is too long method
  144. */
  145. public function testGetTooLongQuery()
  146. {
  147. $queryId = 123;
  148. $maxQueryLength = 8;
  149. $minQueryLength = 3;
  150. $rawQueryText = ' Simple product ';
  151. $cleanedRawText = 'Simple product';
  152. $subRawText = 'Simple p';
  153. $isQueryTextExceeded = true;
  154. $isQueryTextShort = false;
  155. $this->string->expects($this->any())
  156. ->method('substr')
  157. ->withConsecutive([$cleanedRawText, 0, $maxQueryLength])
  158. ->willReturn($subRawText);
  159. $this->mockSetQueryTextNeverExecute($cleanedRawText);
  160. $this->mockString($cleanedRawText);
  161. $this->mockQueryLengths($maxQueryLength, $minQueryLength);
  162. $this->mockGetRawQueryText($rawQueryText);
  163. $this->mockSimpleQuery($subRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
  164. $this->mockCreateQuery();
  165. $result = $this->model->get();
  166. $this->assertSame($this->query, $result);
  167. }
  168. /**
  169. * Test for get query is Short long method
  170. */
  171. public function testGetTooShortQuery()
  172. {
  173. $queryId = 123;
  174. $maxQueryLength = 800;
  175. $minQueryLength = 500;
  176. $rawQueryText = ' Simple product ';
  177. $cleanedRawText = 'Simple product';
  178. $isQueryTextExceeded = false;
  179. $isQueryTextShort = true;
  180. $this->mockSetQueryTextNeverExecute($cleanedRawText);
  181. $this->mockString($cleanedRawText);
  182. $this->mockQueryLengths($maxQueryLength, $minQueryLength);
  183. $this->mockGetRawQueryText($rawQueryText);
  184. $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
  185. $this->mockCreateQuery();
  186. $result = $this->model->get();
  187. $this->assertSame($this->query, $result);
  188. }
  189. /**
  190. * Test for get query is Short long method
  191. */
  192. public function testGetQueryWithoutId()
  193. {
  194. $queryId = 0;
  195. $maxQueryLength = 100;
  196. $minQueryLength = 3;
  197. $rawQueryText = ' Simple product ';
  198. $cleanedRawText = 'Simple product';
  199. $isQueryTextExceeded = false;
  200. $isQueryTextShort = false;
  201. $this->mockSetQueryTextOnceExecute($cleanedRawText);
  202. $this->mockString($cleanedRawText);
  203. $this->mockQueryLengths($maxQueryLength, $minQueryLength);
  204. $this->mockGetRawQueryText($rawQueryText);
  205. $this->mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort);
  206. $this->mockCreateQuery();
  207. $result = $this->model->get();
  208. $this->assertSame($this->query, $result);
  209. }
  210. /**
  211. * @param int $maxQueryLength
  212. * @param int $minQueryLength
  213. * @return void
  214. */
  215. private function mockQueryLengths($maxQueryLength, $minQueryLength)
  216. {
  217. $this->queryHelper->expects($this->once())
  218. ->method('getMaxQueryLength')
  219. ->willReturn($maxQueryLength);
  220. $this->queryHelper->expects($this->once())
  221. ->method('getMinQueryLength')
  222. ->willReturn($minQueryLength);
  223. }
  224. /**
  225. * @param string $rawQueryText
  226. * @return void
  227. */
  228. private function mockGetRawQueryText($rawQueryText)
  229. {
  230. $this->request->expects($this->any())
  231. ->method('getParam')
  232. ->withConsecutive([QueryFactory::QUERY_VAR_NAME])
  233. ->willReturn($rawQueryText);
  234. }
  235. /**
  236. * @param string $cleanedRawText
  237. * @return void
  238. */
  239. private function mockString($cleanedRawText)
  240. {
  241. $this->string->expects($this->any())
  242. ->method('cleanString')
  243. ->withConsecutive([$cleanedRawText])
  244. ->willReturnArgument(0);
  245. $this->string->expects($this->any())
  246. ->method('strlen')
  247. ->withConsecutive([$cleanedRawText])
  248. ->willReturn(strlen($cleanedRawText));
  249. }
  250. /**
  251. * @return void
  252. */
  253. private function mockCreateQuery()
  254. {
  255. $this->objectManager->expects($this->once())
  256. ->method('create')
  257. ->withConsecutive([Query::class, []])
  258. ->willReturn($this->query);
  259. }
  260. /**
  261. * @param string $cleanedRawText
  262. * @param int $queryId
  263. * @param bool $isQueryTextExceeded
  264. * @param bool $isQueryTextShort
  265. * @return void
  266. */
  267. private function mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded, $isQueryTextShort)
  268. {
  269. $this->query->expects($this->once())
  270. ->method('loadByQueryText')
  271. ->withConsecutive([$cleanedRawText])
  272. ->willReturnSelf();
  273. $this->query->expects($this->once())
  274. ->method('getId')
  275. ->willReturn($queryId);
  276. $this->query->expects($this->once())
  277. ->method('setIsQueryTextExceeded')
  278. ->withConsecutive([$isQueryTextExceeded]);
  279. $this->query->expects($this->once())
  280. ->method('setIsQueryTextShort')
  281. ->withConsecutive([$isQueryTextShort]);
  282. }
  283. /**
  284. * @param string $cleanedRawText
  285. * @return void
  286. */
  287. private function mockSetQueryTextNeverExecute($cleanedRawText)
  288. {
  289. $this->query->expects($this->never())
  290. ->method('setQueryText')
  291. ->withConsecutive([$cleanedRawText])
  292. ->willReturnSelf();
  293. }
  294. /**
  295. * @param string $cleanedRawText
  296. * @return void
  297. */
  298. private function mockSetQueryTextOnceExecute($cleanedRawText)
  299. {
  300. $this->query->expects($this->once())
  301. ->method('setQueryText')
  302. ->withConsecutive([$cleanedRawText])
  303. ->willReturnSelf();
  304. }
  305. }