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

/tests/Solarium/Tests/QueryType/Select/Query/Component/FacetSetTest.php

http://github.com/basdenooijer/solarium
PHP | 432 lines | 303 code | 80 blank | 49 comment | 0 complexity | 504dd767b244fc9ca6833f8d91f02b4c MD5 | raw file
Possible License(s): BSD-3-Clause
  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\Query\Component;
  32. use Solarium\QueryType\Select\Query\Component\FacetSet;
  33. use Solarium\QueryType\Select\Query\Query;
  34. use Solarium\QueryType\Select\Query\Component\Facet\Query as FacetQuery;
  35. class FacetSetTest extends \PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * @var FacetSet
  39. */
  40. protected $facetSet;
  41. public function setUp()
  42. {
  43. $this->facetSet = new FacetSet;
  44. }
  45. public function testConfigMode()
  46. {
  47. $options = array(
  48. 'facet' => array(
  49. array('type' => 'query', 'key' => 'f1', 'query' => 'category:1'),
  50. 'f2' => array('type' => 'query', 'query' => 'category:2')
  51. ),
  52. 'prefix' => 'pr',
  53. 'sort' => 'index',
  54. 'mincount' => 10,
  55. 'missing' => 5,
  56. 'extractfromresponse' => true,
  57. );
  58. $this->facetSet->setOptions($options);
  59. $facets = $this->facetSet->getFacets();
  60. $this->assertEquals(2, count($facets));
  61. $this->assertEquals($options['prefix'], $this->facetSet->getPrefix());
  62. $this->assertEquals($options['sort'], $this->facetSet->getSort());
  63. $this->assertEquals($options['mincount'], $this->facetSet->getMincount());
  64. $this->assertEquals($options['missing'], $this->facetSet->getMissing());
  65. $this->assertEquals($options['extractfromresponse'], $this->facetSet->getExtractFromResponse());
  66. }
  67. public function testGetType()
  68. {
  69. $this->assertEquals(Query::COMPONENT_FACETSET, $this->facetSet->getType());
  70. }
  71. public function testGetResponseParser()
  72. {
  73. $this->assertInstanceOf(
  74. 'Solarium\QueryType\Select\ResponseParser\Component\FacetSet',
  75. $this->facetSet->getResponseParser()
  76. );
  77. }
  78. public function testGetRequestBuilder()
  79. {
  80. $this->assertInstanceOf(
  81. 'Solarium\QueryType\Select\RequestBuilder\Component\FacetSet',
  82. $this->facetSet->getRequestBuilder()
  83. );
  84. }
  85. public function testSetAndGetSort()
  86. {
  87. $this->facetSet->setSort('index');
  88. $this->assertEquals('index', $this->facetSet->getSort());
  89. }
  90. public function testSetAndGetPrefix()
  91. {
  92. $this->facetSet->setPrefix('xyz');
  93. $this->assertEquals('xyz', $this->facetSet->getPrefix());
  94. }
  95. public function testSetAndGetLimit()
  96. {
  97. $this->facetSet->setLimit(12);
  98. $this->assertEquals(12, $this->facetSet->getLimit());
  99. }
  100. public function testSetAndGetMinCount()
  101. {
  102. $this->facetSet->setMincount(100);
  103. $this->assertEquals(100, $this->facetSet->getMincount());
  104. }
  105. public function testSetAndGetMissing()
  106. {
  107. $this->facetSet->setMissing(true);
  108. $this->assertEquals(true, $this->facetSet->getMissing());
  109. }
  110. public function testAddAndGetFacet()
  111. {
  112. $fq = new FacetQuery;
  113. $fq->setKey('f1')->setQuery('category:1');
  114. $this->facetSet->addFacet($fq);
  115. $this->assertEquals(
  116. $fq,
  117. $this->facetSet->getFacet('f1')
  118. );
  119. }
  120. public function testAddFacetWithoutKey()
  121. {
  122. $fq = new FacetQuery;
  123. $fq->setQuery('category:1');
  124. $this->setExpectedException('Solarium\Exception\InvalidArgumentException');
  125. $this->facetSet->addFacet($fq);
  126. }
  127. public function testAddFacetWithUsedKey()
  128. {
  129. $fq1 = new FacetQuery;
  130. $fq1->setKey('f1')->setQuery('category:1');
  131. $fq2 = new FacetQuery;
  132. $fq2->setKey('f1')->setQuery('category:2');
  133. $this->facetSet->addFacet($fq1);
  134. $this->setExpectedException('Solarium\Exception\InvalidArgumentException');
  135. $this->facetSet->addFacet($fq2);
  136. }
  137. public function testGetInvalidFacet()
  138. {
  139. $this->assertEquals(
  140. null,
  141. $this->facetSet->getFacet('invalidtag')
  142. );
  143. }
  144. public function testAddFacets()
  145. {
  146. $fq1 = new FacetQuery;
  147. $fq1->setKey('f1')->setQuery('category:1');
  148. $fq2 = new FacetQuery;
  149. $fq2->setKey('f2')->setQuery('category:2');
  150. $facets = array('f1' => $fq1, 'f2' => $fq2);
  151. $this->facetSet->addFacets($facets);
  152. $this->assertEquals(
  153. $facets,
  154. $this->facetSet->getFacets()
  155. );
  156. }
  157. public function testAddFacetsWithConfig()
  158. {
  159. $facets = array(
  160. array('type' => 'query', 'key' => 'f1', 'query' => 'category:1'),
  161. 'f2' => array('type' => 'query', 'query' => 'category:2')
  162. );
  163. $this->facetSet->addFacets($facets);
  164. $this->assertEquals(
  165. 2,
  166. count($this->facetSet->getFacets())
  167. );
  168. }
  169. public function testRemoveFacet()
  170. {
  171. $fq1 = new FacetQuery;
  172. $fq1->setKey('f1')->setQuery('category:1');
  173. $fq2 = new FacetQuery;
  174. $fq2->setKey('f2')->setQuery('category:2');
  175. $facets = array('f1' => $fq1, 'f2' => $fq2);
  176. $this->facetSet->addFacets($facets);
  177. $this->facetSet->removeFacet('f1');
  178. $this->assertEquals(
  179. array('f2' => $fq2),
  180. $this->facetSet->getFacets()
  181. );
  182. }
  183. public function testRemoveFacetWithObjectInput()
  184. {
  185. $fq1 = new FacetQuery;
  186. $fq1->setKey('f1')->setQuery('category:1');
  187. $fq2 = new FacetQuery;
  188. $fq2->setKey('f2')->setQuery('category:2');
  189. $facets = array('f1' => $fq1, 'f2' => $fq2);
  190. $this->facetSet->addFacets($facets);
  191. $this->facetSet->removeFacet($fq1);
  192. $this->assertEquals(
  193. array('f2' => $fq2),
  194. $this->facetSet->getFacets()
  195. );
  196. }
  197. public function testRemoveInvalidFacet()
  198. {
  199. $fq1 = new FacetQuery;
  200. $fq1->setKey('f1')->setQuery('category:1');
  201. $fq2 = new FacetQuery;
  202. $fq2->setKey('f2')->setQuery('category:2');
  203. $facets = array('f1' => $fq1, 'f2' => $fq2);
  204. $this->facetSet->addFacets($facets);
  205. $this->facetSet->removeFacet('f3'); //continue silently
  206. $this->assertEquals(
  207. $facets,
  208. $this->facetSet->getFacets()
  209. );
  210. }
  211. public function testClearFacets()
  212. {
  213. $fq1 = new FacetQuery;
  214. $fq1->setKey('f1')->setQuery('category:1');
  215. $fq2 = new FacetQuery;
  216. $fq2->setKey('f2')->setQuery('category:2');
  217. $facets = array('f1' => $fq1, 'f2' => $fq2);
  218. $this->facetSet->addFacets($facets);
  219. $this->facetSet->clearFacets();
  220. $this->assertEquals(
  221. array(),
  222. $this->facetSet->getFacets()
  223. );
  224. }
  225. public function testSetFacets()
  226. {
  227. $fq1 = new FacetQuery;
  228. $fq1->setKey('f1')->setQuery('category:1');
  229. $fq2 = new FacetQuery;
  230. $fq2->setKey('f2')->setQuery('category:2');
  231. $facets = array('f1' => $fq1, 'f2' => $fq2);
  232. $this->facetSet->addFacets($facets);
  233. $fq3 = new FacetQuery;
  234. $fq3->setKey('f3')->setQuery('category:3');
  235. $fq4 = new FacetQuery;
  236. $fq4->setKey('f4')->setQuery('category:4');
  237. $facets = array('f3' => $fq3, 'f4' => $fq4);
  238. $this->facetSet->setFacets($facets);
  239. $this->assertEquals(
  240. $facets,
  241. $this->facetSet->getFacets()
  242. );
  243. }
  244. public function testCreateFacet()
  245. {
  246. $type = FacetSet::FACET_FIELD;
  247. $options = array('optionA' => 1, 'optionB' => 2);
  248. $facet = $this->facetSet->createFacet($type, $options);
  249. // check class mapping
  250. $this->assertEquals(
  251. $type,
  252. $facet->getType()
  253. );
  254. // check option forwarding
  255. $facetOptions = $facet->getOptions();
  256. $this->assertEquals(
  257. $options['optionB'],
  258. $facetOptions['optionB']
  259. );
  260. }
  261. public function testCreateFacetAdd()
  262. {
  263. $type = FacetSet::FACET_FIELD;
  264. $options = array('key' => 'mykey', 'optionA' => 1, 'optionB' => 2);
  265. $facet = $this->facetSet->createFacet($type, $options);
  266. $this->assertEquals($facet, $this->facetSet->getFacet('mykey'));
  267. }
  268. public function testCreateFacetAddWithString()
  269. {
  270. $type = FacetSet::FACET_FIELD;
  271. $options = 'mykey';
  272. $facet = $this->facetSet->createFacet($type, $options);
  273. $this->assertEquals($facet, $this->facetSet->getFacet('mykey'));
  274. }
  275. public function testCreateFacetWithInvalidType()
  276. {
  277. $this->setExpectedException('Solarium\Exception\OutOfBoundsException');
  278. $this->facetSet->createFacet('invalidtype');
  279. }
  280. public function createFacetAddProvider()
  281. {
  282. return array(
  283. array(true),
  284. array(false),
  285. );
  286. }
  287. /**
  288. * @dataProvider createFacetAddProvider
  289. */
  290. public function testCreateFacetField($add)
  291. {
  292. $options = array('optionA' => 1, 'optionB' => 2);
  293. $observer = $this->getMock('Solarium\QueryType\Select\Query\Component\FacetSet', array('createFacet'));
  294. $observer->expects($this->once())
  295. ->method('createFacet')
  296. ->with($this->equalTo(FacetSet::FACET_FIELD), $this->equalTo($options), $add);
  297. $observer->createFacetField($options, $add);
  298. }
  299. /**
  300. * @dataProvider createFacetAddProvider
  301. */
  302. public function testCreateFacetQuery($add)
  303. {
  304. $options = array('optionA' => 1, 'optionB' => 2);
  305. $observer = $this->getMock('Solarium\QueryType\Select\Query\Component\FacetSet', array('createFacet'));
  306. $observer->expects($this->once())
  307. ->method('createFacet')
  308. ->with($this->equalTo(FacetSet::FACET_QUERY), $this->equalTo($options), $add);
  309. $observer->createFacetQuery($options, $add);
  310. }
  311. /**
  312. * @dataProvider createFacetAddProvider
  313. */
  314. public function testCreateFacetMultiQuery($add)
  315. {
  316. $options = array('optionA' => 1, 'optionB' => 2);
  317. $observer = $this->getMock('Solarium\QueryType\Select\Query\Component\FacetSet', array('createFacet'));
  318. $observer->expects($this->once())
  319. ->method('createFacet')
  320. ->with($this->equalTo(FacetSet::FACET_MULTIQUERY), $this->equalTo($options), $add);
  321. $observer->createFacetMultiQuery($options, $add);
  322. }
  323. /**
  324. * @dataProvider createFacetAddProvider
  325. */
  326. public function testCreateFacetRange($add)
  327. {
  328. $options = array('optionA' => 1, 'optionB' => 2);
  329. $observer = $this->getMock('Solarium\QueryType\Select\Query\Component\FacetSet', array('createFacet'));
  330. $observer->expects($this->once())
  331. ->method('createFacet')
  332. ->with($this->equalTo(FacetSet::FACET_RANGE), $this->equalTo($options), $add);
  333. $observer->createFacetRange($options, $add);
  334. }
  335. /**
  336. * @dataProvider createFacetAddProvider
  337. */
  338. public function testCreateFacetPivot($add)
  339. {
  340. $options = array('optionA' => 1, 'optionB' => 2);
  341. $observer = $this->getMock('Solarium\QueryType\Select\Query\Component\FacetSet', array('createFacet'));
  342. $observer->expects($this->once())
  343. ->method('createFacet')
  344. ->with($this->equalTo(FacetSet::FACET_PIVOT), $this->equalTo($options), $add);
  345. $observer->createFacetPivot($options, $add);
  346. }
  347. public function testSetAndGetExtractFromResponse()
  348. {
  349. $this->facetSet->setExtractFromResponse(true);
  350. $this->assertEquals(true, $this->facetSet->getExtractFromResponse());
  351. }
  352. }