PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/solarium/solarium/tests/Solarium/Tests/QueryType/Select/ResponseParser/Component/FacetSetTest.php

https://gitlab.com/Blueprint-Marketing/solr-power
PHP | 289 lines | 223 code | 36 blank | 30 comment | 0 complexity | de26059042128826844b9372e737a37e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 Bas de Nooijer. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this listof conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * The views and conclusions contained in the software and documentation are
  28. * those of the authors and should not be interpreted as representing official
  29. * policies, either expressed or implied, of the copyright holder.
  30. */
  31. namespace Solarium\Tests\QueryType\Select\ResponseParser\Component;
  32. use Solarium\QueryType\Select\ResponseParser\Component\FacetSet as Parser;
  33. use Solarium\QueryType\Select\Query\Component\FacetSet;
  34. use Solarium\QueryType\Select\Query\Query;
  35. class FacetSetTest extends \PHPUnit_Framework_TestCase
  36. {
  37. protected $parser;
  38. protected $facetSet;
  39. protected $query;
  40. public function setUp()
  41. {
  42. $this->parser = new Parser;
  43. $this->facetSet = new FacetSet();
  44. $this->facetSet->createFacet('field', array('key' => 'keyA', 'field' => 'fieldA'));
  45. $this->facetSet->createFacet('query', array('key' => 'keyB'));
  46. $this->facetSet->createFacet(
  47. 'multiquery',
  48. array(
  49. 'key' => 'keyC',
  50. 'query' => array(
  51. 'keyC_A' => array('query' => 'id:1'),
  52. 'keyC_B' => array('query' => 'id:2'),
  53. ),
  54. )
  55. );
  56. $this->facetSet->createFacet('range', array('key' => 'keyD'));
  57. $this->facetSet->createFacet('pivot', array('key' => 'keyE', 'fields' => 'cat,price'));
  58. $this->query = new Query;
  59. }
  60. public function testParse()
  61. {
  62. $data = array(
  63. 'facet_counts' => array(
  64. 'facet_fields' => array(
  65. 'keyA' => array(
  66. 'value1',
  67. 12,
  68. 'value2',
  69. 3,
  70. ),
  71. ),
  72. 'facet_queries' => array(
  73. 'keyB' => 23,
  74. 'keyC_A' => 25,
  75. 'keyC_B' => 16,
  76. ),
  77. 'facet_ranges' => array(
  78. 'keyD' => array(
  79. 'before' => 3,
  80. 'after' => 5,
  81. 'between' => 4,
  82. 'counts' => array(
  83. '1.0',
  84. 1,
  85. '101.0',
  86. 2,
  87. '201.0',
  88. 1,
  89. )
  90. )
  91. ),
  92. 'facet_pivot' => array(
  93. 'cat,price' => array(
  94. array(
  95. 'field' => 'cat',
  96. 'value' => 'abc',
  97. 'count' => '123',
  98. 'pivot' => array(
  99. array('field' => 'price', 'value' => 1, 'count' => 12),
  100. array('field' => 'price', 'value' => 2, 'count' => 8),
  101. )
  102. )
  103. ),
  104. ),
  105. )
  106. );
  107. $result = $this->parser->parse($this->query, $this->facetSet, $data);
  108. $facets = $result->getFacets();
  109. $this->assertEquals(array('keyA', 'keyB', 'keyC', 'keyD', 'keyE'), array_keys($facets));
  110. $this->assertEquals(
  111. array('value1' => 12, 'value2' => 3),
  112. $facets['keyA']->getValues()
  113. );
  114. $this->assertEquals(
  115. 23,
  116. $facets['keyB']->getValue()
  117. );
  118. $this->assertEquals(
  119. array('keyC_A' => 25, 'keyC_B' => 16),
  120. $facets['keyC']->getValues()
  121. );
  122. $this->assertEquals(
  123. array('1.0' => 1, '101.0' => 2, '201.0' => 1),
  124. $facets['keyD']->getValues()
  125. );
  126. $this->assertEquals(
  127. 3,
  128. $facets['keyD']->getBefore()
  129. );
  130. $this->assertEquals(
  131. 4,
  132. $facets['keyD']->getBetween()
  133. );
  134. $this->assertEquals(
  135. 5,
  136. $facets['keyD']->getAfter()
  137. );
  138. $this->assertEquals(
  139. 1,
  140. count($facets['keyE'])
  141. );
  142. $this->query = new Query;
  143. }
  144. public function testParseExtractFromResponse()
  145. {
  146. $data = array(
  147. 'facet_counts' => array(
  148. 'facet_fields' => array(
  149. 'keyA' => array(
  150. 'value1',
  151. 12,
  152. 'value2',
  153. 3,
  154. ),
  155. ),
  156. 'facet_queries' => array(
  157. 'keyB' => 23,
  158. 'keyC_A' => 25,
  159. 'keyC_B' => 16,
  160. ),
  161. 'facet_ranges' => array(
  162. 'keyD' => array(
  163. 'before' => 3,
  164. 'after' => 5,
  165. 'between' => 4,
  166. 'counts' => array(
  167. '1.0',
  168. 1,
  169. '101.0',
  170. 2,
  171. '201.0',
  172. 1,
  173. )
  174. )
  175. ),
  176. 'facet_pivot' => array(
  177. 'cat,price' => array(
  178. array(
  179. 'field' => 'cat',
  180. 'value' => 'abc',
  181. 'count' => '123',
  182. 'pivot' => array(
  183. array('field' => 'price', 'value' => 1, 'count' => 12),
  184. array('field' => 'price', 'value' => 2, 'count' => 8),
  185. )
  186. )
  187. ),
  188. ),
  189. )
  190. );
  191. $facetSet = new FacetSet();
  192. $facetSet->setExtractFromResponse(true);
  193. $result = $this->parser->parse($this->query, $facetSet, $data);
  194. $facets = $result->getFacets();
  195. $this->assertEquals(array('keyA', 'keyB', 'keyC_A', 'keyC_B', 'keyD', 'cat,price'), array_keys($facets));
  196. $this->assertEquals(
  197. array('value1' => 12, 'value2' => 3),
  198. $facets['keyA']->getValues()
  199. );
  200. $this->assertEquals(
  201. 23,
  202. $facets['keyB']->getValue()
  203. );
  204. // As the multiquery facet is a Solarium virtual facet type, it cannot be detected based on Solr response data
  205. $this->assertEquals(
  206. 25,
  207. $facets['keyC_A']->getValue()
  208. );
  209. $this->assertEquals(
  210. 16,
  211. $facets['keyC_B']->getValue()
  212. );
  213. $this->assertEquals(
  214. array('1.0' => 1, '101.0' => 2, '201.0' => 1),
  215. $facets['keyD']->getValues()
  216. );
  217. $this->assertEquals(
  218. 3,
  219. $facets['keyD']->getBefore()
  220. );
  221. $this->assertEquals(
  222. 4,
  223. $facets['keyD']->getBetween()
  224. );
  225. $this->assertEquals(
  226. 5,
  227. $facets['keyD']->getAfter()
  228. );
  229. $this->assertEquals(
  230. 1,
  231. count($facets['cat,price'])
  232. );
  233. $this->query = new Query;
  234. }
  235. public function testParseNoData()
  236. {
  237. $result = $this->parser->parse($this->query, $this->facetSet, array());
  238. $this->assertEquals(array(), $result->getFacets());
  239. }
  240. public function testInvalidFacetType()
  241. {
  242. $facetStub = $this->getMock('Solarium\QueryType\Select\Query\Component\Facet\Field');
  243. $facetStub->expects($this->once())
  244. ->method('getType')
  245. ->will($this->returnValue('invalidfacettype'));
  246. $facetStub->expects($this->any())
  247. ->method('getKey')
  248. ->will($this->returnValue('facetkey'));
  249. $this->facetSet->addFacet($facetStub);
  250. $this->setExpectedException('Solarium\Exception\RuntimeException');
  251. $this->parser->parse($this->query, $this->facetSet, array());
  252. }
  253. }