PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-search/Test/Unit/Model/QueryFactoryTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 265 lines | 220 code | 26 blank | 19 comment | 0 complexity | 987a09f795a9051d062d71bb9c685f48 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class QueryFactoryTest extends \PHPUnit_Framework_TestCase
  9. {
  10. const XML_PATH_MAX_QUERY_LENGTH = 'catalog/search/max_query_length';
  11. const QUERY_VAR_NAME = 'q';
  12. /** @var \Magento\Search\Model\Query|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $queryMock;
  14. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $requestMock;
  16. /** @var \Magento\Search\Model\QueryFactory */
  17. protected $queryFactory;
  18. /** @var ObjectManagerHelper */
  19. protected $objectManagerHelper;
  20. /** @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $contextMock;
  22. /** @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $objectManagerMock;
  24. /** @var \Magento\Framework\Stdlib\StringUtils|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $stringMock;
  26. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $scopeConfigMock;
  28. protected function setUp()
  29. {
  30. $this->requestMock = $this->getMockBuilder('\Magento\Framework\App\RequestInterface')
  31. ->disableOriginalConstructor()
  32. ->setMethods([])
  33. ->getMock();
  34. $this->contextMock = $this->getMockBuilder('Magento\Framework\App\Helper\Context')
  35. ->setMethods(['getRequest', 'getScopeConfig'])
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->contextMock->expects($this->once())
  39. ->method('getRequest')
  40. ->will($this->returnValue($this->requestMock));
  41. $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface')
  42. ->setMethods([])
  43. ->getMock();
  44. $this->stringMock = $this->getMockBuilder('Magento\Framework\Stdlib\StringUtils')
  45. ->setMethods(['cleanString', 'substr'])
  46. ->getMock();
  47. $this->stringMock->expects($this->any())
  48. ->method('cleanString')
  49. ->will($this->returnArgument(0));
  50. $this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
  51. ->setMethods([])
  52. ->getMock();
  53. $this->contextMock->expects($this->once())
  54. ->method('getScopeConfig')
  55. ->will($this->returnValue($this->scopeConfigMock));
  56. $this->queryMock = $this->getMockBuilder('\Magento\Search\Model\Query')
  57. ->disableOriginalConstructor()
  58. ->setMethods(['setIsQueryTextExceeded', 'getId', 'setQueryText', 'loadByQuery'])
  59. ->getMock();
  60. $this->objectManagerHelper = new ObjectManagerHelper($this);
  61. $this->queryFactory = $this->objectManagerHelper->getObject(
  62. 'Magento\Search\Model\QueryFactory',
  63. [
  64. 'context' => $this->contextMock,
  65. 'objectManager' => $this->objectManagerMock,
  66. 'string' => $this->stringMock,
  67. ]
  68. );
  69. }
  70. public function testGetNewQuery()
  71. {
  72. $queryId = null;
  73. $this->mapScopeConfig(
  74. [
  75. self::XML_PATH_MAX_QUERY_LENGTH => 120,
  76. ]
  77. );
  78. $rawQueryText = 'Simple product';
  79. $preparedQueryText = $rawQueryText;
  80. $this->requestMock->expects($this->once())
  81. ->method('getParam')
  82. ->with($this->equalTo(self::QUERY_VAR_NAME))
  83. ->will($this->returnValue($rawQueryText));
  84. $this->objectManagerMock->expects($this->once())
  85. ->method('create')
  86. ->with($this->equalTo('Magento\Search\Model\Query'))
  87. ->will($this->returnValue($this->queryMock));
  88. $this->queryMock->expects($this->once())
  89. ->method('loadByQuery')
  90. ->with($this->equalTo($preparedQueryText))
  91. ->will($this->returnSelf());
  92. $this->queryMock->expects($this->once())
  93. ->method('getId')
  94. ->will($this->returnValue($queryId));
  95. $this->queryMock->expects($this->once())
  96. ->method('setQueryText')
  97. ->with($preparedQueryText)
  98. ->will($this->returnSelf());
  99. $this->queryMock->expects($this->once())
  100. ->method('setIsQueryTextExceeded')
  101. ->with($this->equalTo(false))
  102. ->will($this->returnSelf());
  103. $query = $this->queryFactory->get();
  104. $this->assertSame($this->queryMock, $query);
  105. }
  106. public function testGetLoadedQuery()
  107. {
  108. $queryId = 123;
  109. $this->mapScopeConfig(
  110. [
  111. self::XML_PATH_MAX_QUERY_LENGTH => 20,
  112. ]
  113. );
  114. $rawQueryText = 'Simple product';
  115. $preparedQueryText = $rawQueryText;
  116. $this->requestMock->expects($this->once())
  117. ->method('getParam')
  118. ->with($this->equalTo(self::QUERY_VAR_NAME))
  119. ->will($this->returnValue($rawQueryText));
  120. $this->objectManagerMock->expects($this->once())
  121. ->method('create')
  122. ->with($this->equalTo('Magento\Search\Model\Query'))
  123. ->will($this->returnValue($this->queryMock));
  124. $this->queryMock->expects($this->once())
  125. ->method('loadByQuery')
  126. ->with($this->equalTo($preparedQueryText))
  127. ->will($this->returnSelf());
  128. $this->queryMock->expects($this->once())
  129. ->method('getId')
  130. ->will($this->returnValue($queryId));
  131. $this->queryMock->expects($this->once())
  132. ->method('setIsQueryTextExceeded')
  133. ->with($this->equalTo(false))
  134. ->will($this->returnSelf());
  135. $query = $this->queryFactory->get();
  136. $this->assertSame($this->queryMock, $query);
  137. }
  138. public function testGetTooLongQuery()
  139. {
  140. $queryId = 123;
  141. $this->mapScopeConfig(
  142. [
  143. self::XML_PATH_MAX_QUERY_LENGTH => 12,
  144. ]
  145. );
  146. $rawQueryText = 'This is very long search query text';
  147. $preparedQueryText = 'This is very';
  148. $this->stringMock->expects($this->once())
  149. ->method('substr')
  150. ->with($this->equalTo($rawQueryText))
  151. ->will($this->returnValue($preparedQueryText));
  152. $this->requestMock->expects($this->once())
  153. ->method('getParam')
  154. ->with($this->equalTo(self::QUERY_VAR_NAME))
  155. ->will($this->returnValue($rawQueryText));
  156. $this->objectManagerMock->expects($this->once())
  157. ->method('create')
  158. ->with($this->equalTo('Magento\Search\Model\Query'))
  159. ->will($this->returnValue($this->queryMock));
  160. $this->queryMock->expects($this->once())
  161. ->method('loadByQuery')
  162. ->with($this->equalTo($preparedQueryText))
  163. ->will($this->returnSelf());
  164. $this->queryMock->expects($this->once())
  165. ->method('getId')
  166. ->will($this->returnValue($queryId));
  167. $this->queryMock->expects($this->once())
  168. ->method('setIsQueryTextExceeded')
  169. ->with($this->equalTo(true))
  170. ->will($this->returnSelf());
  171. $query = $this->queryFactory->get();
  172. $this->assertSame($this->queryMock, $query);
  173. }
  174. /**
  175. * @depends testGetNewQuery
  176. * @param $query
  177. */
  178. public function testGetQueryTwice($query)
  179. {
  180. $queryId = null;
  181. $this->mapScopeConfig(
  182. [
  183. self::XML_PATH_MAX_QUERY_LENGTH => 120,
  184. ]
  185. );
  186. $rawQueryText = 'Simple product';
  187. $preparedQueryText = $rawQueryText;
  188. $this->requestMock->expects($this->once())
  189. ->method('getParam')
  190. ->with($this->equalTo(self::QUERY_VAR_NAME))
  191. ->will($this->returnValue($rawQueryText));
  192. $this->objectManagerMock->expects($this->once())
  193. ->method('create')
  194. ->with($this->equalTo('Magento\Search\Model\Query'))
  195. ->will($this->returnValue($this->queryMock));
  196. $this->queryMock->expects($this->once())
  197. ->method('loadByQuery')
  198. ->with($this->equalTo($preparedQueryText))
  199. ->will($this->returnSelf());
  200. $this->queryMock->expects($this->once())
  201. ->method('getId')
  202. ->will($this->returnValue($queryId));
  203. $this->queryMock->expects($this->once())
  204. ->method('setQueryText')
  205. ->with($preparedQueryText)
  206. ->will($this->returnSelf());
  207. $this->queryMock->expects($this->once())
  208. ->method('setIsQueryTextExceeded')
  209. ->with($this->equalTo(false))
  210. ->will($this->returnSelf());
  211. $query = $this->queryFactory->get();
  212. $this->assertSame($this->queryMock, $query);
  213. $this->assertSame($query, $this->queryFactory->get());
  214. }
  215. public function testCreate()
  216. {
  217. $this->objectManagerMock->expects($this->once())
  218. ->method('create')
  219. ->with($this->equalTo('Magento\Search\Model\Query'))
  220. ->will($this->returnValue($this->queryMock));
  221. $query = $this->queryFactory->create();
  222. $this->assertSame($this->queryMock, $query);
  223. }
  224. /**
  225. * @param array $map
  226. */
  227. private function mapScopeConfig(array $map)
  228. {
  229. $this->scopeConfigMock->expects($this->atLeastOnce())
  230. ->method('getValue')
  231. ->will(
  232. $this->returnCallback(
  233. function ($path) use ($map) {
  234. return isset($map[$path]) ? $map[$path] : null;
  235. }
  236. )
  237. );
  238. }
  239. }