DocumentTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. /**
  3. * Copyright (c) 2007-2011, Servigistics, Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - Neither the name of Servigistics, Inc. nor the names of
  15. * its contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
  31. * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
  32. *
  33. * @package Apache
  34. * @subpackage Solr
  35. * @author Donovan Jimenez <djimenez@conduit-it.com>
  36. */
  37. /**
  38. * Apache_Solr_Document Unit Test
  39. */
  40. class Apache_Solr_DocumentTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Fixture used for testing
  44. *
  45. * @var Apache_Solr_Document
  46. */
  47. private $_fixture;
  48. /**
  49. * Setup for the fixture before each unit test - part of test case API
  50. */
  51. protected function setup()
  52. {
  53. $this->_fixture = new Apache_Solr_Document();
  54. }
  55. /**
  56. * Teardown after each unit test - part of test case API
  57. */
  58. protected function tearDown()
  59. {
  60. unset($this->_fixture);
  61. }
  62. public function testDefaultStateAfterConstructor()
  63. {
  64. // document boost should be false
  65. $this->assertFalse($this->_fixture->getBoost());
  66. // document fields should be empty
  67. $this->assertEquals(0, count($this->_fixture->getFieldNames()));
  68. $this->assertEquals(0, count($this->_fixture->getFieldValues()));
  69. $this->assertEquals(0, count($this->_fixture->getFieldBoosts()));
  70. // document iterator should be empty
  71. $this->assertEquals(0, iterator_count($this->_fixture));
  72. }
  73. public function testSetAndGetField()
  74. {
  75. $field = 'field';
  76. $value = 'value';
  77. $boost = 0.5;
  78. // set the field
  79. $this->_fixture->setField($field, $value, $boost);
  80. $result = $this->_fixture->getField($field);
  81. // check the array values
  82. $this->assertTrue(is_array($result));
  83. $this->assertEquals($field, $result['name']);
  84. $this->assertEquals($value, $result['value']);
  85. $this->assertEquals($boost, $result['boost']);
  86. }
  87. public function testGetFieldReturnsFalseForNonExistentField()
  88. {
  89. $this->assertFalse($this->_fixture->getField('field'));
  90. }
  91. public function testMagicGetForFieldValues()
  92. {
  93. $field = 'field';
  94. $value = 'value';
  95. $this->_fixture->setField($field, $value);
  96. // test the __get value
  97. $this->assertEquals($value, $this->_fixture->{$field});
  98. }
  99. /**
  100. * Added for issue #48 (http://code.google.com/p/solr-php-client/issues/detail?id=48)
  101. */
  102. public function testMagicGetReturnsNullForNonExistentField()
  103. {
  104. $this->assertNull($this->_fixture->nonExistent);
  105. }
  106. public function testMagicSetForFieldValues()
  107. {
  108. $field = 'field';
  109. $value = 'value';
  110. // set field value with magic __set
  111. $this->_fixture->{$field} = $value;
  112. $fieldArray = $this->_fixture->getField($field);
  113. // set values
  114. $this->assertEquals($field, $fieldArray['name']);
  115. $this->assertEquals($value, $fieldArray['value']);
  116. $this->assertTrue($fieldArray['boost'] === false);
  117. }
  118. public function testMagicIssetForNonExistentField()
  119. {
  120. $this->assertFalse(isset($this->_fixture->field));
  121. }
  122. public function testMagicIssetForExistingField()
  123. {
  124. $field = 'field';
  125. $this->_fixture->{$field} = 'value';
  126. $this->assertTrue(isset($this->_fixture->{$field}));
  127. }
  128. public function testMagicUnsetForExistingField()
  129. {
  130. $field = 'field';
  131. $this->_fixture->{$field} = 'value';
  132. // now unset the field
  133. unset($this->_fixture->{$field});
  134. // now test that its unset
  135. $this->assertFalse(isset($this->_fixture->{$field}));
  136. }
  137. public function testMagicUnsetForNonExistingField()
  138. {
  139. $field = 'field';
  140. unset($this->_fixture->{$field});
  141. // now test that it still does not exist
  142. $this->assertFalse(isset($this->_fixture->{$field}));
  143. }
  144. public function testSetAndGetFieldBoostWithPositiveNumberSetsBoost()
  145. {
  146. $field = 'field';
  147. $boost = 0.5;
  148. $this->_fixture->setFieldBoost($field, $boost);
  149. // test the field boost
  150. $this->assertEquals($boost, $this->_fixture->getFieldBoost($field));
  151. }
  152. public function testSetAndGetFieldBoostWithZeroRemovesBoost()
  153. {
  154. $field = 'field';
  155. $boost = 0;
  156. $this->_fixture->setFieldBoost($field, $boost);
  157. // test the field boost
  158. $this->assertTrue($this->_fixture->getFieldBoost($field) === false);
  159. }
  160. public function testSetAndGetFieldBoostWithNegativeNumberRemovesBoost()
  161. {
  162. $field = 'field';
  163. $boost = -1;
  164. $this->_fixture->setFieldBoost($field, $boost);
  165. // test the field boost
  166. $this->assertTrue($this->_fixture->getFieldBoost($field) === false);
  167. }
  168. public function testSetAndGetFieldBoostWithNonNumberRemovesBoost()
  169. {
  170. $field = 'field';
  171. $boost = "i am not a number";
  172. $this->_fixture->setFieldBoost($field, $boost);
  173. // test the field boost
  174. $this->assertTrue($this->_fixture->getFieldBoost($field) === false);
  175. }
  176. public function testSetAndGetBoostWithPositiveNumberSetsBoost()
  177. {
  178. $boost = 0.5;
  179. $this->_fixture->setBoost($boost);
  180. // the boost should now be set
  181. $this->assertEquals($boost, $this->_fixture->getBoost());
  182. }
  183. public function testSetAndGetBoostWithZeroRemovesBoost()
  184. {
  185. $this->_fixture->setBoost(0);
  186. // should be boolean false
  187. $this->assertTrue($this->_fixture->getBoost() === false);
  188. }
  189. public function testSetAndGetBoostWithNegativeNumberRemovesBoost()
  190. {
  191. $this->_fixture->setBoost(-1);
  192. // should be boolean false
  193. $this->assertTrue($this->_fixture->getBoost() === false);
  194. }
  195. public function testSetAndGetBoostWithNonNumberRemovesBoost()
  196. {
  197. $this->_fixture->setBoost("i am not a number");
  198. // should be boolean false
  199. $this->assertTrue($this->_fixture->getBoost() === false);
  200. }
  201. public function testAddFieldCreatesMultiValueWhenFieldDoesNotExist()
  202. {
  203. $field = 'field';
  204. $value = 'value';
  205. $this->_fixture->addField($field, $value);
  206. // check that value is an array with correct values
  207. $fieldValue = $this->_fixture->{$field};
  208. $this->assertTrue(is_array($fieldValue));
  209. $this->assertEquals(1, count($fieldValue));
  210. $this->assertEquals($value, $fieldValue[0]);
  211. }
  212. /**
  213. * setMultiValue has been deprecated and defers to addField
  214. *
  215. * @deprecated
  216. */
  217. public function testSetMultiValueCreateMultiValueWhenFieldDoesNotExist()
  218. {
  219. $field = 'field';
  220. $value = 'value';
  221. $this->_fixture->setMultiValue($field, $value);
  222. // check that value is an array with correct values
  223. $fieldValue = $this->_fixture->{$field};
  224. $this->assertTrue(is_array($fieldValue));
  225. $this->assertEquals(1, count($fieldValue));
  226. $this->assertEquals($value, $fieldValue[0]);
  227. }
  228. public function testAddFieldCreatesMultiValueWhenFieldDoesExistAsSingleValue()
  229. {
  230. $field = 'field';
  231. $value1 = 'value1';
  232. $value2 = 'value2';
  233. // set first value as singular value
  234. $this->_fixture->{$field} = $value1;
  235. // add a second value with addField
  236. $this->_fixture->addField($field, $value2);
  237. // check that value is an array with correct values
  238. $fieldValue = $this->_fixture->{$field};
  239. $this->assertTrue(is_array($fieldValue));
  240. $this->assertEquals(2, count($fieldValue));
  241. $this->assertEquals($value1, $fieldValue[0]);
  242. $this->assertEquals($value2, $fieldValue[1]);
  243. }
  244. /**
  245. * setMultiValue has been deprecated and defers to addField
  246. *
  247. * @deprecated
  248. */
  249. public function testSetMultiValueCreatesMultiValueWhenFieldDoesExistAsSingleValue()
  250. {
  251. $field = 'field';
  252. $value1 = 'value1';
  253. $value2 = 'value2';
  254. // set first value as singular value
  255. $this->_fixture->{$field} = $value1;
  256. // add a second value with addField
  257. $this->_fixture->setMultiValue($field, $value2);
  258. // check that value is an array with correct values
  259. $fieldValue = $this->_fixture->{$field};
  260. $this->assertTrue(is_array($fieldValue));
  261. $this->assertEquals(2, count($fieldValue));
  262. $this->assertEquals($value1, $fieldValue[0]);
  263. $this->assertEquals($value2, $fieldValue[1]);
  264. }
  265. public function testAddFieldWithBoostSetsFieldBoost()
  266. {
  267. $field = 'field';
  268. $boost = 0.5;
  269. $this->_fixture->addField($field, 'value', $boost);
  270. // check the field boost
  271. $this->assertEquals($boost, $this->_fixture->getFieldBoost($field));
  272. }
  273. public function testAddFieldWithBoostMultipliesWithAPreexistingBoost()
  274. {
  275. $field = 'field';
  276. $boost = 0.5;
  277. // set a field with a boost
  278. $this->_fixture->setField($field, 'value1', $boost);
  279. // now add another value with the same boost
  280. $this->_fixture->addField($field, 'value2', $boost);
  281. // new boost should be $boost * $boost
  282. $this->assertEquals($boost * $boost, $this->_fixture->getFieldBoost($field));
  283. }
  284. public function testGetFieldNamesIsInitiallyEmpty()
  285. {
  286. $fieldNames = $this->_fixture->getFieldNames();
  287. $this->assertTrue(empty($fieldNames));
  288. }
  289. public function testGetFieldNamesAfterFieldIsSetIsNotEmpty()
  290. {
  291. $field = 'field';
  292. $this->_fixture->{$field} = 'value';
  293. $fieldNames = $this->_fixture->getFieldNames();
  294. $this->assertTrue(!empty($fieldNames));
  295. $this->assertEquals(1, count($fieldNames));
  296. $this->assertEquals($field, $fieldNames[0]);
  297. }
  298. public function testGetFieldValuesIsInitiallyEmpty()
  299. {
  300. $fieldValues = $this->_fixture->getFieldValues();
  301. $this->assertTrue(empty($fieldValues));
  302. }
  303. public function testGetFieldValuessAfterFieldIsSetIsNotEmpty()
  304. {
  305. $value = 'value';
  306. $this->_fixture->field = $value;
  307. $fieldValues = $this->_fixture->getFieldValues();
  308. $this->assertTrue(!empty($fieldValues));
  309. $this->assertEquals(1, count($fieldValues));
  310. $this->assertEquals($value, $fieldValues[0]);
  311. }
  312. public function testGetIteratorAfterFieldValueIsSet()
  313. {
  314. $field = 'field';
  315. $value = 'value';
  316. $this->_fixture->{$field} = $value;
  317. $itemCount = 0;
  318. foreach ($this->_fixture as $iteratedField => $iteratedValue)
  319. {
  320. ++$itemCount;
  321. // test field and value
  322. $this->assertEquals($field, $iteratedField);
  323. $this->assertEquals($value, $iteratedValue);
  324. }
  325. // test number of iterations is 1
  326. $this->assertEquals(1, $itemCount);
  327. }
  328. public function testClearReturnsDocumentToDefaultState()
  329. {
  330. // set the document boost
  331. $this->_fixture->setBoost(0.5);
  332. // set a field
  333. $this->_fixture->someField = "some value";
  334. // clear the document to remove boost and fields
  335. $this->_fixture->clear();
  336. // document boost should now be false
  337. $this->assertFalse($this->_fixture->getBoost());
  338. // document fields should now be empty
  339. $this->assertEquals(0, count($this->_fixture->getFieldNames()));
  340. $this->assertEquals(0, count($this->_fixture->getFieldValues()));
  341. $this->assertEquals(0, count($this->_fixture->getFieldBoosts()));
  342. // document iterator should now be empty
  343. $this->assertEquals(0, iterator_count($this->_fixture));
  344. }
  345. }