MapperTest.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Test\Unit\Request;
  7. use Magento\Framework\Search\Request\FilterInterface;
  8. use Magento\Framework\Search\Request\QueryInterface;
  9. use Magento\Framework\Search\Request\Query\Filter;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class MapperTest extends \PHPUnit\Framework\TestCase
  15. {
  16. const ROOT_QUERY = 'someQuery';
  17. /**
  18. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  19. */
  20. private $helper;
  21. /**
  22. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $objectManager;
  25. /**
  26. * @var \Magento\Framework\Search\Request\Query\Match|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $queryMatch;
  29. /**
  30. * @var \Magento\Framework\Search\Request\Query\BoolExpression|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $queryBool;
  33. /**
  34. * @var \Magento\Framework\Search\Request\Query\Filter|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $queryFilter;
  37. /**
  38. * @var \Magento\Framework\Search\Request\Filter\Term|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $filterTerm;
  41. /**
  42. * @var \Magento\Framework\Search\Request\Filter\Wildcard|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $filterWildcard;
  45. /**
  46. * @var \Magento\Framework\Search\Request\Filter\Range|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $filterRange;
  49. /**
  50. * @var \Magento\Framework\Search\Request\Filter\Bool|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $filterBool;
  53. protected function setUp()
  54. {
  55. $this->helper = new ObjectManager($this);
  56. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  57. $this->queryMatch = $this->getMockBuilder(\Magento\Framework\Search\Request\Query\Match::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->queryBool = $this->getMockBuilder(\Magento\Framework\Search\Request\Query\BoolExpression::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->queryFilter = $this->getMockBuilder(\Magento\Framework\Search\Request\Query\Filter::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->filterTerm = $this->getMockBuilder(\Magento\Framework\Search\Request\Filter\Term::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->filterRange = $this->getMockBuilder(\Magento\Framework\Search\Request\Filter\Range::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->filterBool = $this->getMockBuilder(\Magento\Framework\Search\Request\Filter\BoolExpression::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->filterWildcard = $this->getMockBuilder(\Magento\Framework\Search\Request\Filter\Wildcard::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. }
  79. /**
  80. * @param $queries
  81. * @dataProvider getQueryMatchProvider
  82. */
  83. public function testGetQueryMatch($queries)
  84. {
  85. $query = $queries[self::ROOT_QUERY];
  86. $this->objectManager->expects($this->once())->method('create')
  87. ->with(
  88. $this->equalTo(\Magento\Framework\Search\Request\Query\Match::class),
  89. $this->equalTo(
  90. [
  91. 'name' => $query['name'],
  92. 'value' => $query['value'],
  93. 'boost' => isset($query['boost']) ? $query['boost'] : 1,
  94. 'matches' => $query['match'],
  95. ]
  96. )
  97. )
  98. ->will($this->returnValue($this->queryMatch));
  99. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  100. $mapper = $this->helper->getObject(
  101. \Magento\Framework\Search\Request\Mapper::class,
  102. [
  103. 'objectManager' => $this->objectManager,
  104. 'queries' => $queries,
  105. 'rootQueryName' => self::ROOT_QUERY,
  106. 'aggregation' => [],
  107. 'filters' => []
  108. ]
  109. );
  110. $this->assertEquals($this->queryMatch, $mapper->getRootQuery());
  111. }
  112. /**
  113. * @expectedException \Magento\Framework\Exception\StateException
  114. */
  115. public function testGetQueryNotUsedStateException()
  116. {
  117. $queries = [
  118. self::ROOT_QUERY => [
  119. 'type' => QueryInterface::TYPE_MATCH,
  120. 'name' => 'someName',
  121. 'value' => 'someValue',
  122. 'boost' => 3,
  123. 'match' => 'someMatches',
  124. ],
  125. 'notUsedQuery' => [
  126. 'type' => QueryInterface::TYPE_MATCH,
  127. 'name' => 'someName',
  128. 'value' => 'someValue',
  129. 'boost' => 3,
  130. 'match' => 'someMatches',
  131. ],
  132. ];
  133. $query = $queries['someQuery'];
  134. $this->objectManager->expects($this->once())->method('create')
  135. ->with(
  136. $this->equalTo(\Magento\Framework\Search\Request\Query\Match::class),
  137. $this->equalTo(
  138. [
  139. 'name' => $query['name'],
  140. 'value' => $query['value'],
  141. 'boost' => isset($query['boost']) ? $query['boost'] : 1,
  142. 'matches' => $query['match'],
  143. ]
  144. )
  145. )
  146. ->will($this->returnValue($this->queryMatch));
  147. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  148. $mapper = $this->helper->getObject(
  149. \Magento\Framework\Search\Request\Mapper::class,
  150. [
  151. 'objectManager' => $this->objectManager,
  152. 'queries' => $queries,
  153. 'rootQueryName' => self::ROOT_QUERY,
  154. 'aggregation' => [],
  155. 'filters' => []
  156. ]
  157. );
  158. $this->assertEquals($this->queryMatch, $mapper->getRootQuery());
  159. }
  160. /**
  161. * @expectedException \Magento\Framework\Exception\StateException
  162. */
  163. public function testGetQueryUsedStateException()
  164. {
  165. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  166. $mapper = $this->helper->getObject(
  167. \Magento\Framework\Search\Request\Mapper::class,
  168. [
  169. 'objectManager' => $this->objectManager,
  170. 'queries' => [
  171. self::ROOT_QUERY => [
  172. 'type' => QueryInterface::TYPE_BOOL,
  173. 'name' => 'someName',
  174. 'queryReference' => [
  175. [
  176. 'clause' => 'someClause',
  177. 'ref' => 'someQuery',
  178. ],
  179. ],
  180. ],
  181. ],
  182. 'rootQueryName' => self::ROOT_QUERY,
  183. 'aggregation' => [],
  184. 'filters' => []
  185. ]
  186. );
  187. $this->assertEquals($this->queryMatch, $mapper->getRootQuery());
  188. }
  189. /**
  190. * @param $queries
  191. * @dataProvider getQueryFilterQueryReferenceProvider
  192. */
  193. public function testGetQueryFilterQueryReference($queries)
  194. {
  195. $query = $queries['someQueryMatch'];
  196. $this->objectManager->expects($this->at(0))->method('create')
  197. ->with(
  198. $this->equalTo(\Magento\Framework\Search\Request\Query\Match::class),
  199. $this->equalTo(
  200. [
  201. 'name' => $query['name'],
  202. 'value' => $query['value'],
  203. 'boost' => 1,
  204. 'matches' => 'someMatches',
  205. ]
  206. )
  207. )
  208. ->will($this->returnValue($this->queryMatch));
  209. $query = $queries[self::ROOT_QUERY];
  210. $this->objectManager->expects($this->at(1))->method('create')
  211. ->with(
  212. $this->equalTo(\Magento\Framework\Search\Request\Query\Filter::class),
  213. $this->equalTo(
  214. [
  215. 'name' => $query['name'],
  216. 'boost' => isset($query['boost']) ? $query['boost'] : 1,
  217. 'reference' => $this->queryMatch,
  218. 'referenceType' => Filter::REFERENCE_QUERY,
  219. ]
  220. )
  221. )
  222. ->will($this->returnValue($this->queryFilter));
  223. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  224. $mapper = $this->helper->getObject(
  225. \Magento\Framework\Search\Request\Mapper::class,
  226. [
  227. 'objectManager' => $this->objectManager,
  228. 'queries' => $queries,
  229. 'rootQueryName' => self::ROOT_QUERY,
  230. 'aggregation' => [],
  231. 'filters' => []
  232. ]
  233. );
  234. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  235. }
  236. /**
  237. * @expectedException \Exception
  238. * @expectedExceptionMessage Reference is not provided
  239. */
  240. public function testGetQueryFilterReferenceException()
  241. {
  242. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  243. $mapper = $this->helper->getObject(
  244. \Magento\Framework\Search\Request\Mapper::class,
  245. [
  246. 'objectManager' => $this->objectManager,
  247. 'queries' => [
  248. 'someQuery' => [
  249. 'type' => QueryInterface::TYPE_FILTER,
  250. ],
  251. ],
  252. 'rootQueryName' => self::ROOT_QUERY,
  253. 'aggregation' => [],
  254. 'filters' => []
  255. ]
  256. );
  257. $mapper->getRootQuery();
  258. }
  259. /**
  260. * @param $queries
  261. * @dataProvider getQueryBoolProvider
  262. */
  263. public function testGetQueryBool($queries)
  264. {
  265. $query = $queries['someQueryMatch'];
  266. $this->objectManager->expects($this->at(0))->method('create')
  267. ->with(
  268. $this->equalTo(\Magento\Framework\Search\Request\Query\Match::class),
  269. $this->equalTo(
  270. [
  271. 'name' => $query['name'],
  272. 'value' => $query['value'],
  273. 'boost' => 1,
  274. 'matches' => 'someMatches',
  275. ]
  276. )
  277. )
  278. ->will($this->returnValue($this->queryMatch));
  279. $query = $queries[self::ROOT_QUERY];
  280. $this->objectManager->expects($this->at(1))->method('create')
  281. ->with(
  282. $this->equalTo(\Magento\Framework\Search\Request\Query\BoolExpression::class),
  283. $this->equalTo(
  284. [
  285. 'name' => $query['name'],
  286. 'boost' => isset($query['boost']) ? $query['boost'] : 1,
  287. 'someClause' => ['someQueryMatch' => $this->queryMatch],
  288. ]
  289. )
  290. )
  291. ->will($this->returnValue($this->queryBool));
  292. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  293. $mapper = $this->helper->getObject(
  294. \Magento\Framework\Search\Request\Mapper::class,
  295. [
  296. 'objectManager' => $this->objectManager,
  297. 'queries' => $queries,
  298. 'rootQueryName' => self::ROOT_QUERY,
  299. 'aggregation' => [],
  300. 'filters' => []
  301. ]
  302. );
  303. $this->assertEquals($this->queryBool, $mapper->getRootQuery());
  304. }
  305. /**
  306. * #@expectedException \InvalidArgumentException
  307. */
  308. public function testGetQueryInvalidArgumentException()
  309. {
  310. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  311. $mapper = $this->helper->getObject(
  312. \Magento\Framework\Search\Request\Mapper::class,
  313. [
  314. 'objectManager' => $this->objectManager,
  315. 'queries' => [
  316. self::ROOT_QUERY => [
  317. 'type' => 'invalid_type',
  318. ],
  319. ],
  320. 'rootQueryName' => self::ROOT_QUERY,
  321. 'aggregation' => [],
  322. 'filters' => []
  323. ]
  324. );
  325. $mapper->getRootQuery();
  326. }
  327. /**
  328. * @expectedException \Exception
  329. */
  330. public function testGetQueryException()
  331. {
  332. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  333. $mapper = $this->helper->getObject(
  334. \Magento\Framework\Search\Request\Mapper::class,
  335. [
  336. 'objectManager' => $this->objectManager,
  337. 'queries' => [],
  338. 'rootQueryName' => self::ROOT_QUERY,
  339. 'filters' => []
  340. ]
  341. );
  342. $mapper->getRootQuery();
  343. }
  344. public function testGetFilterTerm()
  345. {
  346. $queries = [
  347. self::ROOT_QUERY => [
  348. 'type' => QueryInterface::TYPE_FILTER,
  349. 'name' => 'someName',
  350. 'filterReference' => [
  351. [
  352. 'ref' => 'someFilter',
  353. ],
  354. ],
  355. ],
  356. ];
  357. $filters = [
  358. 'someFilter' => [
  359. 'type' => FilterInterface::TYPE_TERM,
  360. 'name' => 'someName',
  361. 'field' => 'someField',
  362. 'value' => 'someValue',
  363. ],
  364. ];
  365. $filter = $filters['someFilter'];
  366. $this->objectManager->expects($this->at(0))->method('create')
  367. ->with(
  368. $this->equalTo(\Magento\Framework\Search\Request\Filter\Term::class),
  369. $this->equalTo(
  370. [
  371. 'name' => $filter['name'],
  372. 'field' => $filter['field'],
  373. 'value' => $filter['value'],
  374. ]
  375. )
  376. )
  377. ->will($this->returnValue($this->filterTerm));
  378. $query = $queries[self::ROOT_QUERY];
  379. $this->objectManager->expects($this->at(1))->method('create')
  380. ->with(
  381. $this->equalTo(\Magento\Framework\Search\Request\Query\Filter::class),
  382. $this->equalTo(
  383. [
  384. 'name' => $query['name'],
  385. 'boost' => 1,
  386. 'reference' => $this->filterTerm,
  387. 'referenceType' => Filter::REFERENCE_FILTER,
  388. ]
  389. )
  390. )
  391. ->will($this->returnValue($this->queryFilter));
  392. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  393. $mapper = $this->helper->getObject(
  394. \Magento\Framework\Search\Request\Mapper::class,
  395. [
  396. 'objectManager' => $this->objectManager,
  397. 'queries' => $queries,
  398. 'rootQueryName' => self::ROOT_QUERY,
  399. 'aggregation' => [],
  400. 'filters' => $filters
  401. ]
  402. );
  403. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  404. }
  405. public function testGetFilterWildcard()
  406. {
  407. $queries = [
  408. self::ROOT_QUERY => [
  409. 'type' => QueryInterface::TYPE_FILTER,
  410. 'name' => 'someName',
  411. 'filterReference' => [
  412. [
  413. 'ref' => 'someFilter',
  414. ],
  415. ],
  416. ],
  417. ];
  418. $filters = [
  419. 'someFilter' => [
  420. 'type' => FilterInterface::TYPE_WILDCARD,
  421. 'name' => 'someName',
  422. 'field' => 'someField',
  423. 'value' => 'someValue',
  424. ],
  425. ];
  426. $filter = $filters['someFilter'];
  427. $this->objectManager->expects($this->at(0))->method('create')
  428. ->with(
  429. $this->equalTo(\Magento\Framework\Search\Request\Filter\Wildcard::class),
  430. $this->equalTo(
  431. [
  432. 'name' => $filter['name'],
  433. 'field' => $filter['field'],
  434. 'value' => $filter['value'],
  435. ]
  436. )
  437. )
  438. ->will($this->returnValue($this->filterTerm));
  439. $query = $queries[self::ROOT_QUERY];
  440. $this->objectManager->expects($this->at(1))->method('create')
  441. ->with(
  442. $this->equalTo(\Magento\Framework\Search\Request\Query\Filter::class),
  443. $this->equalTo(
  444. [
  445. 'name' => $query['name'],
  446. 'boost' => 1,
  447. 'reference' => $this->filterTerm,
  448. 'referenceType' => Filter::REFERENCE_FILTER,
  449. ]
  450. )
  451. )
  452. ->will($this->returnValue($this->queryFilter));
  453. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  454. $mapper = $this->helper->getObject(
  455. \Magento\Framework\Search\Request\Mapper::class,
  456. [
  457. 'objectManager' => $this->objectManager,
  458. 'queries' => $queries,
  459. 'rootQueryName' => self::ROOT_QUERY,
  460. 'aggregation' => [],
  461. 'filters' => $filters
  462. ]
  463. );
  464. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  465. }
  466. public function testGetFilterRange()
  467. {
  468. $queries = [
  469. self::ROOT_QUERY => [
  470. 'type' => QueryInterface::TYPE_FILTER,
  471. 'name' => 'someName',
  472. 'filterReference' => [
  473. [
  474. 'ref' => 'someFilter',
  475. ],
  476. ],
  477. ],
  478. ];
  479. $filters = [
  480. 'someFilter' => [
  481. 'type' => FilterInterface::TYPE_RANGE,
  482. 'name' => 'someName',
  483. 'field' => 'someField',
  484. 'from' => 'from',
  485. 'to' => 'to',
  486. ],
  487. ];
  488. $filter = $filters['someFilter'];
  489. $this->objectManager->expects($this->at(0))->method('create')
  490. ->with(
  491. $this->equalTo(\Magento\Framework\Search\Request\Filter\Range::class),
  492. $this->equalTo(
  493. [
  494. 'name' => $filter['name'],
  495. 'field' => $filter['field'],
  496. 'from' => $filter['from'],
  497. 'to' => $filter['to'],
  498. ]
  499. )
  500. )
  501. ->will($this->returnValue($this->filterRange));
  502. $query = $queries[self::ROOT_QUERY];
  503. $this->objectManager->expects($this->at(1))->method('create')
  504. ->with(
  505. $this->equalTo(\Magento\Framework\Search\Request\Query\Filter::class),
  506. $this->equalTo(
  507. [
  508. 'name' => $query['name'],
  509. 'boost' => 1,
  510. 'reference' => $this->filterRange,
  511. 'referenceType' => Filter::REFERENCE_FILTER,
  512. ]
  513. )
  514. )
  515. ->will($this->returnValue($this->queryFilter));
  516. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  517. $mapper = $this->helper->getObject(
  518. \Magento\Framework\Search\Request\Mapper::class,
  519. [
  520. 'objectManager' => $this->objectManager,
  521. 'queries' => $queries,
  522. 'rootQueryName' => self::ROOT_QUERY,
  523. 'aggregation' => [],
  524. 'filters' => $filters
  525. ]
  526. );
  527. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  528. }
  529. public function testGetFilterBool()
  530. {
  531. $queries = [
  532. self::ROOT_QUERY => [
  533. 'type' => QueryInterface::TYPE_FILTER,
  534. 'name' => 'someName',
  535. 'filterReference' => [
  536. [
  537. 'ref' => 'someFilter',
  538. ],
  539. ],
  540. ],
  541. ];
  542. $filters = [
  543. 'someFilter' => [
  544. 'type' => FilterInterface::TYPE_BOOL,
  545. 'name' => 'someName',
  546. 'filterReference' => [
  547. [
  548. 'ref' => 'someFilterTerm',
  549. 'clause' => 'someClause',
  550. ],
  551. ],
  552. ],
  553. 'someFilterTerm' => [
  554. 'type' => FilterInterface::TYPE_TERM,
  555. 'name' => 'someName',
  556. 'field' => 'someField',
  557. 'value' => 'someValue',
  558. ],
  559. ];
  560. $filter = $filters['someFilterTerm'];
  561. $this->objectManager->expects($this->at(0))->method('create')
  562. ->with(
  563. $this->equalTo(\Magento\Framework\Search\Request\Filter\Term::class),
  564. $this->equalTo(
  565. [
  566. 'name' => $filter['name'],
  567. 'field' => $filter['field'],
  568. 'value' => $filter['value'],
  569. ]
  570. )
  571. )
  572. ->will($this->returnValue($this->filterTerm));
  573. $filter = $filters['someFilter'];
  574. $this->objectManager->expects($this->at(1))->method('create')
  575. ->with(
  576. $this->equalTo(\Magento\Framework\Search\Request\Filter\BoolExpression::class),
  577. $this->equalTo(
  578. [
  579. 'name' => $filter['name'],
  580. 'someClause' => ['someFilterTerm' => $this->filterTerm],
  581. ]
  582. )
  583. )
  584. ->will($this->returnValue($this->filterBool));
  585. $query = $queries[self::ROOT_QUERY];
  586. $this->objectManager->expects($this->at(2))->method('create')
  587. ->with(
  588. $this->equalTo(\Magento\Framework\Search\Request\Query\Filter::class),
  589. $this->equalTo(
  590. [
  591. 'name' => $query['name'],
  592. 'boost' => 1,
  593. 'reference' => $this->filterBool,
  594. 'referenceType' => Filter::REFERENCE_FILTER,
  595. ]
  596. )
  597. )
  598. ->will($this->returnValue($this->queryFilter));
  599. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  600. $mapper = $this->helper->getObject(
  601. \Magento\Framework\Search\Request\Mapper::class,
  602. [
  603. 'objectManager' => $this->objectManager,
  604. 'queries' => $queries,
  605. 'rootQueryName' => self::ROOT_QUERY,
  606. 'aggregation' => [],
  607. 'filters' => $filters
  608. ]
  609. );
  610. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  611. }
  612. /**
  613. * @expectedException \Magento\Framework\Exception\StateException
  614. */
  615. public function testGetFilterNotUsedStateException()
  616. {
  617. $queries = [
  618. self::ROOT_QUERY => [
  619. 'type' => QueryInterface::TYPE_FILTER,
  620. 'name' => 'someName',
  621. 'filterReference' => [
  622. [
  623. 'ref' => 'someFilter',
  624. ],
  625. ],
  626. ],
  627. ];
  628. $filters = [
  629. 'someFilter' => [
  630. 'type' => FilterInterface::TYPE_TERM,
  631. 'name' => 'someName',
  632. 'field' => 'someField',
  633. 'value' => 'someValue',
  634. ],
  635. 'notUsedFilter' => [
  636. 'type' => FilterInterface::TYPE_TERM,
  637. 'name' => 'someName',
  638. 'field' => 'someField',
  639. 'value' => 'someValue',
  640. ],
  641. ];
  642. $filter = $filters['someFilter'];
  643. $this->objectManager->expects($this->at(0))->method('create')
  644. ->with(
  645. $this->equalTo(\Magento\Framework\Search\Request\Filter\Term::class),
  646. $this->equalTo(
  647. [
  648. 'name' => $filter['name'],
  649. 'field' => $filter['field'],
  650. 'value' => $filter['value'],
  651. ]
  652. )
  653. )
  654. ->will($this->returnValue($this->filterTerm));
  655. $query = $queries[self::ROOT_QUERY];
  656. $this->objectManager->expects($this->at(1))->method('create')
  657. ->with(
  658. $this->equalTo(\Magento\Framework\Search\Request\Query\Filter::class),
  659. $this->equalTo(
  660. [
  661. 'name' => $query['name'],
  662. 'boost' => 1,
  663. 'reference' => $this->filterTerm,
  664. 'referenceType' => Filter::REFERENCE_FILTER,
  665. ]
  666. )
  667. )
  668. ->will($this->returnValue($this->queryFilter));
  669. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  670. $mapper = $this->helper->getObject(
  671. \Magento\Framework\Search\Request\Mapper::class,
  672. [
  673. 'objectManager' => $this->objectManager,
  674. 'queries' => $queries,
  675. 'rootQueryName' => self::ROOT_QUERY,
  676. 'aggregation' => [],
  677. 'filters' => $filters
  678. ]
  679. );
  680. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  681. }
  682. /**
  683. * @expectedException \Magento\Framework\Exception\StateException
  684. */
  685. public function testGetFilterUsedStateException()
  686. {
  687. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  688. $mapper = $this->helper->getObject(
  689. \Magento\Framework\Search\Request\Mapper::class,
  690. [
  691. 'objectManager' => $this->objectManager,
  692. 'queries' => [
  693. self::ROOT_QUERY => [
  694. 'type' => QueryInterface::TYPE_FILTER,
  695. 'name' => 'someName',
  696. 'filterReference' => [
  697. [
  698. 'ref' => 'someFilter',
  699. ],
  700. ],
  701. ],
  702. ],
  703. 'rootQueryName' => self::ROOT_QUERY,
  704. 'filters' => [
  705. 'someFilter' => [
  706. 'type' => FilterInterface::TYPE_BOOL,
  707. 'name' => 'someName',
  708. 'filterReference' => [
  709. [
  710. 'ref' => 'someFilter',
  711. 'clause' => 'someClause',
  712. ],
  713. ],
  714. ],
  715. ],
  716. 'aggregation' => [],
  717. ]
  718. );
  719. $this->assertEquals($this->queryMatch, $mapper->getRootQuery());
  720. }
  721. /**
  722. * @expectedException \InvalidArgumentException
  723. * @expectedExceptionMessage Invalid filter type
  724. */
  725. public function testGetFilterInvalidArgumentException()
  726. {
  727. $queries = [
  728. self::ROOT_QUERY => [
  729. 'type' => QueryInterface::TYPE_FILTER,
  730. 'name' => 'someName',
  731. 'filterReference' => [
  732. [
  733. 'ref' => 'someFilter',
  734. ],
  735. ],
  736. ],
  737. ];
  738. $filters = [
  739. 'someFilter' => [
  740. 'type' => 'invalid_type',
  741. ],
  742. ];
  743. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  744. $mapper = $this->helper->getObject(
  745. \Magento\Framework\Search\Request\Mapper::class,
  746. [
  747. 'objectManager' => $this->objectManager,
  748. 'queries' => $queries,
  749. 'rootQueryName' => self::ROOT_QUERY,
  750. 'aggregation' => [],
  751. 'filters' => $filters
  752. ]
  753. );
  754. $this->assertEquals($this->queryFilter, $mapper->getRootQuery());
  755. }
  756. /**
  757. * @expectedException \Exception
  758. */
  759. public function testGetFilterException()
  760. {
  761. $queries = [
  762. self::ROOT_QUERY => [
  763. 'type' => QueryInterface::TYPE_FILTER,
  764. 'name' => 'someName',
  765. 'boost' => 3,
  766. 'filterReference' => [
  767. [
  768. 'ref' => 'someQueryMatch',
  769. 'clause' => 'someClause',
  770. ],
  771. ],
  772. ],
  773. ];
  774. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  775. $mapper = $this->helper->getObject(
  776. \Magento\Framework\Search\Request\Mapper::class,
  777. [
  778. 'objectManager' => $this->objectManager,
  779. 'queries' => $queries,
  780. 'rootQueryName' => self::ROOT_QUERY,
  781. 'filters' => []
  782. ]
  783. );
  784. $this->assertEquals($this->queryBool, $mapper->getRootQuery());
  785. }
  786. /**
  787. * @return array
  788. */
  789. public function getQueryMatchProvider()
  790. {
  791. return [
  792. [
  793. [
  794. self::ROOT_QUERY => [
  795. 'type' => QueryInterface::TYPE_MATCH,
  796. 'name' => 'someName',
  797. 'value' => 'someValue',
  798. 'boost' => 3,
  799. 'match' => 'someMatches',
  800. ],
  801. ],
  802. ],
  803. [
  804. [
  805. self::ROOT_QUERY => [
  806. 'type' => QueryInterface::TYPE_MATCH,
  807. 'name' => 'someName',
  808. 'value' => 'someValue',
  809. 'match' => 'someMatches',
  810. ],
  811. ]
  812. ]
  813. ];
  814. }
  815. /**
  816. * @return array
  817. */
  818. public function getQueryFilterQueryReferenceProvider()
  819. {
  820. return [
  821. [
  822. [
  823. self::ROOT_QUERY => [
  824. 'type' => QueryInterface::TYPE_FILTER,
  825. 'name' => 'someName',
  826. 'boost' => 3,
  827. 'queryReference' => [
  828. [
  829. 'ref' => 'someQueryMatch',
  830. 'clause' => 'someClause',
  831. ],
  832. ],
  833. ],
  834. 'someQueryMatch' => [
  835. 'type' => QueryInterface::TYPE_MATCH,
  836. 'value' => 'someValue',
  837. 'name' => 'someName',
  838. 'match' => 'someMatches',
  839. ],
  840. ],
  841. ],
  842. [
  843. [
  844. self::ROOT_QUERY => [
  845. 'type' => QueryInterface::TYPE_FILTER,
  846. 'name' => 'someName',
  847. 'queryReference' => [
  848. [
  849. 'ref' => 'someQueryMatch',
  850. 'clause' => 'someClause',
  851. ],
  852. ],
  853. ],
  854. 'someQueryMatch' => [
  855. 'type' => QueryInterface::TYPE_MATCH,
  856. 'value' => 'someValue',
  857. 'name' => 'someName',
  858. 'match' => 'someMatches',
  859. ],
  860. ]
  861. ]
  862. ];
  863. }
  864. /**
  865. * @return array
  866. */
  867. public function getQueryBoolProvider()
  868. {
  869. return [
  870. [
  871. [
  872. self::ROOT_QUERY => [
  873. 'type' => QueryInterface::TYPE_BOOL,
  874. 'name' => 'someName',
  875. 'boost' => 3,
  876. 'queryReference' => [
  877. [
  878. 'ref' => 'someQueryMatch',
  879. 'clause' => 'someClause',
  880. ],
  881. ],
  882. ],
  883. 'someQueryMatch' => [
  884. 'type' => QueryInterface::TYPE_MATCH,
  885. 'value' => 'someValue',
  886. 'name' => 'someName',
  887. 'match' => 'someMatches',
  888. ],
  889. ],
  890. ],
  891. [
  892. [
  893. self::ROOT_QUERY => [
  894. 'type' => QueryInterface::TYPE_BOOL,
  895. 'name' => 'someName',
  896. 'queryReference' => [
  897. [
  898. 'ref' => 'someQueryMatch',
  899. 'clause' => 'someClause',
  900. ],
  901. ],
  902. ],
  903. 'someQueryMatch' => [
  904. 'type' => QueryInterface::TYPE_MATCH,
  905. 'value' => 'someValue',
  906. 'name' => 'someName',
  907. 'match' => 'someMatches',
  908. ],
  909. ]
  910. ]
  911. ];
  912. }
  913. public function testGetBucketsTermBucket()
  914. {
  915. $queries = [
  916. self::ROOT_QUERY => [
  917. 'type' => QueryInterface::TYPE_MATCH,
  918. 'value' => 'someValue',
  919. 'name' => 'someName',
  920. 'match' => 'someMatches',
  921. ],
  922. ];
  923. $bucket = [
  924. "name" => "category_bucket",
  925. "field" => "category",
  926. "metric" => [
  927. ["type" => "sum"],
  928. ["type" => "count"],
  929. ["type" => "min"],
  930. ["type" => "max"],
  931. ],
  932. "type" => "termBucket",
  933. ];
  934. $metricClass = \Magento\Framework\Search\Request\Aggregation\Metric::class;
  935. $bucketClass = \Magento\Framework\Search\Request\Aggregation\TermBucket::class;
  936. $queryClass = \Magento\Framework\Search\Request\Query\Match::class;
  937. $queryArguments = [
  938. 'name' => $queries[self::ROOT_QUERY]['name'],
  939. 'value' => $queries[self::ROOT_QUERY]['value'],
  940. 'boost' => 1,
  941. 'matches' => $queries[self::ROOT_QUERY]['match'],
  942. ];
  943. $arguments = [
  944. 'name' => $bucket['name'],
  945. 'field' => $bucket['field'],
  946. 'metrics' => [null, null, null, null],
  947. ];
  948. $this->objectManager->expects($this->any())->method('create')
  949. ->withConsecutive(
  950. [$this->equalTo($queryClass), $this->equalTo($queryArguments)],
  951. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][0]['type']])],
  952. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][1]['type']])],
  953. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][2]['type']])],
  954. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][3]['type']])],
  955. [$this->equalTo($bucketClass), $this->equalTo($arguments)]
  956. )
  957. ->will($this->returnValue(null));
  958. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  959. $mapper = $this->helper->getObject(
  960. \Magento\Framework\Search\Request\Mapper::class,
  961. [
  962. 'objectManager' => $this->objectManager,
  963. 'queries' => $queries,
  964. 'rootQueryName' => self::ROOT_QUERY,
  965. 'aggregation' => [$bucket]
  966. ]
  967. );
  968. $mapper->getBuckets();
  969. }
  970. public function testGetBucketsRangeBucket()
  971. {
  972. $queries = [
  973. self::ROOT_QUERY => [
  974. 'type' => QueryInterface::TYPE_MATCH,
  975. 'value' => 'someValue',
  976. 'name' => 'someName',
  977. 'match' => 'someMatches',
  978. ],
  979. ];
  980. $bucket = [
  981. "name" => "price_bucket",
  982. "field" => "price",
  983. "metric" => [
  984. ["type" => "sum"],
  985. ["type" => "count"],
  986. ["type" => "min"],
  987. ["type" => "max"],
  988. ],
  989. "range" => [
  990. ["from" => "", "to" => "50"],
  991. ["from" => "50", "to" => "100"],
  992. ["from" => "100", "to" => ""],
  993. ],
  994. "type" => "rangeBucket",
  995. ];
  996. $metricClass = \Magento\Framework\Search\Request\Aggregation\Metric::class;
  997. $bucketClass = \Magento\Framework\Search\Request\Aggregation\RangeBucket::class;
  998. $rangeClass = \Magento\Framework\Search\Request\Aggregation\Range::class;
  999. $queryClass = \Magento\Framework\Search\Request\Query\Match::class;
  1000. $queryArguments = [
  1001. 'name' => $queries[self::ROOT_QUERY]['name'],
  1002. 'value' => $queries[self::ROOT_QUERY]['value'],
  1003. 'boost' => 1,
  1004. 'matches' => $queries[self::ROOT_QUERY]['match'],
  1005. ];
  1006. $arguments = [
  1007. 'name' => $bucket['name'],
  1008. 'field' => $bucket['field'],
  1009. 'metrics' => [null, null, null, null],
  1010. 'ranges' => [null, null, null],
  1011. ];
  1012. $this->objectManager->expects($this->any())->method('create')
  1013. ->withConsecutive(
  1014. [$this->equalTo($queryClass), $this->equalTo($queryArguments)],
  1015. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][0]['type']])],
  1016. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][1]['type']])],
  1017. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][2]['type']])],
  1018. [$this->equalTo($metricClass), $this->equalTo(['type' => $bucket['metric'][3]['type']])],
  1019. [
  1020. $this->equalTo($rangeClass),
  1021. $this->equalTo(['from' => $bucket['range'][0]['from'], 'to' => $bucket['range'][0]['to']])
  1022. ],
  1023. [
  1024. $this->equalTo($rangeClass),
  1025. $this->equalTo(['from' => $bucket['range'][1]['from'], 'to' => $bucket['range'][1]['to']])
  1026. ],
  1027. [
  1028. $this->equalTo($rangeClass),
  1029. $this->equalTo(['from' => $bucket['range'][2]['from'], 'to' => $bucket['range'][2]['to']])
  1030. ],
  1031. [
  1032. $this->equalTo($bucketClass),
  1033. $this->equalTo($arguments)
  1034. ]
  1035. )
  1036. ->will($this->returnValue(null));
  1037. /** @var \Magento\Framework\Search\Request\Mapper $mapper */
  1038. $mapper = $this->helper->getObject(
  1039. \Magento\Framework\Search\Request\Mapper::class,
  1040. [
  1041. 'objectManager' => $this->objectManager,
  1042. 'queries' => $queries,
  1043. 'rootQueryName' => self::ROOT_QUERY,
  1044. 'aggregation' => [$bucket]
  1045. ]
  1046. );
  1047. $mapper->getBuckets();
  1048. }
  1049. }