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

/vendor/solarium/solarium/tests/Solarium/Tests/QueryType/Select/Query/Component/Stats/StatsTest.php

https://gitlab.com/Blueprint-Marketing/solr-power
PHP | 360 lines | 256 code | 69 blank | 35 comment | 0 complexity | 1eb7f1668291d408d2250cc8d37da4e0 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\Query\Component\Stats;
  32. use Solarium\QueryType\Select\Query\Component\Stats\Stats;
  33. use Solarium\QueryType\Select\Query\Component\Stats\Field;
  34. use Solarium\QueryType\Select\Query\Query;
  35. class StatsTest extends \PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * @var Stats
  39. */
  40. protected $stats;
  41. public function setUp()
  42. {
  43. $this->stats = new Stats;
  44. }
  45. public function testGetType()
  46. {
  47. $this->assertEquals(Query::COMPONENT_STATS, $this->stats->getType());
  48. }
  49. public function testGetResponseParser()
  50. {
  51. $this->assertInstanceOf(
  52. 'Solarium\QueryType\Select\ResponseParser\Component\Stats',
  53. $this->stats->getResponseParser()
  54. );
  55. }
  56. public function testGetRequestBuilder()
  57. {
  58. $this->assertInstanceOf(
  59. 'Solarium\QueryType\Select\RequestBuilder\Component\Stats',
  60. $this->stats->getRequestBuilder()
  61. );
  62. }
  63. public function testConfigMode()
  64. {
  65. $options = array(
  66. 'facet' => 'field1, field2',
  67. 'field' => array(
  68. 'f1' => array(),
  69. 'f2' => array(),
  70. )
  71. );
  72. $this->stats->setOptions($options);
  73. $this->assertEquals(array('field1', 'field2'), $this->stats->getFacets());
  74. $this->assertEquals(array('f1', 'f2'), array_keys($this->stats->getFields()));
  75. }
  76. public function testCreateFieldWithKey()
  77. {
  78. $field = $this->stats->createField('mykey');
  79. // check class
  80. $this->assertThat($field, $this->isInstanceOf('Solarium\QueryType\Select\Query\Component\Stats\Field'));
  81. $this->assertEquals(
  82. 'mykey',
  83. $field->getKey()
  84. );
  85. }
  86. public function testCreateFieldWithOptions()
  87. {
  88. $options = array('key' => 'testkey');
  89. $field = $this->stats->createField($options);
  90. // check class
  91. $this->assertThat($field, $this->isInstanceOf('Solarium\QueryType\Select\Query\Component\Stats\Field'));
  92. // check option forwarding
  93. $fieldOptions = $field->getOptions();
  94. $this->assertEquals(
  95. $options['key'],
  96. $field->getKey()
  97. );
  98. }
  99. public function testAddAndGetField()
  100. {
  101. $field = new Field;
  102. $field->setKey('f1');
  103. $this->stats->addField($field);
  104. $this->assertEquals(
  105. $field,
  106. $this->stats->getField('f1')
  107. );
  108. }
  109. public function testAddFieldWithOptions()
  110. {
  111. $this->stats->addField(array('key' => 'f1'));
  112. $this->assertEquals(
  113. 'f1',
  114. $this->stats->getField('f1')->getKey()
  115. );
  116. }
  117. public function testAddAndGetFieldWithKey()
  118. {
  119. $key = 'f1';
  120. $fld = $this->stats->createField($key, true);
  121. $this->assertEquals(
  122. $key,
  123. $fld->getKey()
  124. );
  125. $this->assertEquals(
  126. $fld,
  127. $this->stats->getField('f1')
  128. );
  129. }
  130. public function testAddFieldWithoutKey()
  131. {
  132. $fld = new Field;
  133. $this->setExpectedException('Solarium\Exception\InvalidArgumentException');
  134. $this->stats->addField($fld);
  135. }
  136. public function testAddFieldWithUsedKey()
  137. {
  138. $f1 = new Field;
  139. $f1->setKey('f1');
  140. $f2 = new Field;
  141. $f2->setKey('f1');
  142. $this->stats->addField($f1);
  143. $this->setExpectedException('Solarium\Exception\InvalidArgumentException');
  144. $this->stats->addField($f2);
  145. }
  146. public function testGetInvalidField()
  147. {
  148. $this->assertEquals(
  149. null,
  150. $this->stats->getField('invalidkey')
  151. );
  152. }
  153. public function testAddFields()
  154. {
  155. $f1 = new Field;
  156. $f1->setKey('f1');
  157. $f2 = new Field;
  158. $f2->setKey('f2');
  159. $fields = array('f1' => $f1, 'f2' => $f2);
  160. $this->stats->addFields($fields);
  161. $this->assertEquals(
  162. $fields,
  163. $this->stats->getFields()
  164. );
  165. }
  166. public function testAddFieldsWithOptions()
  167. {
  168. $fields = array(
  169. 'f1' => array(''),
  170. array('key' => 'f2')
  171. );
  172. $this->stats->addFields($fields);
  173. $fields = $this->stats->getFields();
  174. $this->assertEquals(array('f1', 'f2'), array_keys($fields));
  175. }
  176. public function testRemoveField()
  177. {
  178. $f1 = new Field;
  179. $f1->setKey('f1');
  180. $f2 = new Field;
  181. $f2->setKey('f2');
  182. $fields = array('f1' => $f1, 'f2' => $f2);
  183. $this->stats->addFields($fields);
  184. $this->stats->removeField('f1');
  185. $this->assertEquals(
  186. array('f2' => $f2),
  187. $this->stats->getFields()
  188. );
  189. }
  190. public function testRemoveFieldWithObjectInput()
  191. {
  192. $f1 = new Field;
  193. $f1->setKey('f1');
  194. $f2 = new Field;
  195. $f2->setKey('f2');
  196. $fields = array($f1, $f2);
  197. $this->stats->addFields($fields);
  198. $this->stats->removeField($f1);
  199. $this->assertEquals(
  200. array('f2' => $f2),
  201. $this->stats->getFields()
  202. );
  203. }
  204. public function testRemoveInvalidField()
  205. {
  206. $f1 = new Field;
  207. $f1->setKey('f1');
  208. $f2 = new Field;
  209. $f2->setKey('f2');
  210. $fields = array('f1' => $f1, 'f2' => $f2);
  211. $this->stats->addFields($fields);
  212. $this->stats->removeField('f3'); //continue silently
  213. $this->assertEquals(
  214. $fields,
  215. $this->stats->getFields()
  216. );
  217. }
  218. public function testClearFields()
  219. {
  220. $f1 = new Field;
  221. $f1->setKey('f1');
  222. $f2 = new Field;
  223. $f2->setKey('f2');
  224. $fields = array($f1, $f2);
  225. $this->stats->addFields($fields);
  226. $this->stats->clearFields();
  227. $this->assertEquals(
  228. array(),
  229. $this->stats->getFields()
  230. );
  231. }
  232. public function testSetFields()
  233. {
  234. $f1 = new Field;
  235. $f1->setKey('f1');
  236. $f2 = new Field;
  237. $f2->setKey('f2');
  238. $fields = array($f1, $f2);
  239. $this->stats->addFields($fields);
  240. $f3 = new Field;
  241. $f3->setKey('f3');
  242. $f4 = new Field;
  243. $f4->setKey('f4');
  244. $fields2 = array('f3' => $f3, 'f4' => $f4);
  245. $this->stats->setFields($fields2);
  246. $this->assertEquals(
  247. $fields2,
  248. $this->stats->getFields()
  249. );
  250. }
  251. public function testAddFacet()
  252. {
  253. $expectedFacets = $this->stats->getFacets();
  254. $expectedFacets[] = 'newfacet';
  255. $this->stats->addFacet('newfacet');
  256. $this->assertEquals($expectedFacets, $this->stats->getFacets());
  257. }
  258. public function testClearFacets()
  259. {
  260. $this->stats->addFacet('newfacet');
  261. $this->stats->clearFacets();
  262. $this->assertEquals(array(), $this->stats->getFacets());
  263. }
  264. public function testAddFacets()
  265. {
  266. $facets = array('facet1', 'facet2');
  267. $this->stats->clearFacets();
  268. $this->stats->addFacets($facets);
  269. $this->assertEquals($facets, $this->stats->getFacets());
  270. }
  271. public function testAddFacetsAsStringWithTrim()
  272. {
  273. $this->stats->clearFacets();
  274. $this->stats->addFacets('facet1, facet2');
  275. $this->assertEquals(array('facet1', 'facet2'), $this->stats->getFacets());
  276. }
  277. public function testRemoveFacet()
  278. {
  279. $this->stats->clearFacets();
  280. $this->stats->addFacets(array('facet1', 'facet2'));
  281. $this->stats->removeFacet('facet1');
  282. $this->assertEquals(array('facet2'), $this->stats->getFacets());
  283. }
  284. public function testSetFacets()
  285. {
  286. $this->stats->clearFacets();
  287. $this->stats->addFacets(array('facet1', 'facet2'));
  288. $this->stats->setFacets(array('facet3', 'facet4'));
  289. $this->assertEquals(array('facet3', 'facet4'), $this->stats->getFacets());
  290. }
  291. }