123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- /**
- * Copyright (c) 2007-2011, Servigistics, Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * - Neither the name of Servigistics, Inc. nor the names of
- * its contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
- * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
- *
- * @package Apache
- * @subpackage Solr
- * @author Donovan Jimenez <djimenez@conduit-it.com>
- */
- /**
- * Apache_Solr_Document Unit Test
- */
- class Apache_Solr_DocumentTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Fixture used for testing
- *
- * @var Apache_Solr_Document
- */
- private $_fixture;
- /**
- * Setup for the fixture before each unit test - part of test case API
- */
- protected function setup()
- {
- $this->_fixture = new Apache_Solr_Document();
- }
- /**
- * Teardown after each unit test - part of test case API
- */
- protected function tearDown()
- {
- unset($this->_fixture);
- }
- public function testDefaultStateAfterConstructor()
- {
- // document boost should be false
- $this->assertFalse($this->_fixture->getBoost());
- // document fields should be empty
- $this->assertEquals(0, count($this->_fixture->getFieldNames()));
- $this->assertEquals(0, count($this->_fixture->getFieldValues()));
- $this->assertEquals(0, count($this->_fixture->getFieldBoosts()));
- // document iterator should be empty
- $this->assertEquals(0, iterator_count($this->_fixture));
- }
- public function testSetAndGetField()
- {
- $field = 'field';
- $value = 'value';
- $boost = 0.5;
- // set the field
- $this->_fixture->setField($field, $value, $boost);
- $result = $this->_fixture->getField($field);
- // check the array values
- $this->assertTrue(is_array($result));
- $this->assertEquals($field, $result['name']);
- $this->assertEquals($value, $result['value']);
- $this->assertEquals($boost, $result['boost']);
- }
- public function testGetFieldReturnsFalseForNonExistentField()
- {
- $this->assertFalse($this->_fixture->getField('field'));
- }
- public function testMagicGetForFieldValues()
- {
- $field = 'field';
- $value = 'value';
- $this->_fixture->setField($field, $value);
- // test the __get value
- $this->assertEquals($value, $this->_fixture->{$field});
- }
-
- /**
- * Added for issue #48 (http://code.google.com/p/solr-php-client/issues/detail?id=48)
- */
- public function testMagicGetReturnsNullForNonExistentField()
- {
- $this->assertNull($this->_fixture->nonExistent);
- }
- public function testMagicSetForFieldValues()
- {
- $field = 'field';
- $value = 'value';
- // set field value with magic __set
- $this->_fixture->{$field} = $value;
- $fieldArray = $this->_fixture->getField($field);
- // set values
- $this->assertEquals($field, $fieldArray['name']);
- $this->assertEquals($value, $fieldArray['value']);
- $this->assertTrue($fieldArray['boost'] === false);
- }
- public function testMagicIssetForNonExistentField()
- {
- $this->assertFalse(isset($this->_fixture->field));
- }
- public function testMagicIssetForExistingField()
- {
- $field = 'field';
- $this->_fixture->{$field} = 'value';
- $this->assertTrue(isset($this->_fixture->{$field}));
- }
- public function testMagicUnsetForExistingField()
- {
- $field = 'field';
- $this->_fixture->{$field} = 'value';
- // now unset the field
- unset($this->_fixture->{$field});
- // now test that its unset
- $this->assertFalse(isset($this->_fixture->{$field}));
- }
- public function testMagicUnsetForNonExistingField()
- {
- $field = 'field';
- unset($this->_fixture->{$field});
- // now test that it still does not exist
- $this->assertFalse(isset($this->_fixture->{$field}));
- }
- public function testSetAndGetFieldBoostWithPositiveNumberSetsBoost()
- {
- $field = 'field';
- $boost = 0.5;
- $this->_fixture->setFieldBoost($field, $boost);
- // test the field boost
- $this->assertEquals($boost, $this->_fixture->getFieldBoost($field));
- }
- public function testSetAndGetFieldBoostWithZeroRemovesBoost()
- {
- $field = 'field';
- $boost = 0;
- $this->_fixture->setFieldBoost($field, $boost);
- // test the field boost
- $this->assertTrue($this->_fixture->getFieldBoost($field) === false);
- }
- public function testSetAndGetFieldBoostWithNegativeNumberRemovesBoost()
- {
- $field = 'field';
- $boost = -1;
- $this->_fixture->setFieldBoost($field, $boost);
- // test the field boost
- $this->assertTrue($this->_fixture->getFieldBoost($field) === false);
- }
- public function testSetAndGetFieldBoostWithNonNumberRemovesBoost()
- {
- $field = 'field';
- $boost = "i am not a number";
- $this->_fixture->setFieldBoost($field, $boost);
- // test the field boost
- $this->assertTrue($this->_fixture->getFieldBoost($field) === false);
- }
- public function testSetAndGetBoostWithPositiveNumberSetsBoost()
- {
- $boost = 0.5;
- $this->_fixture->setBoost($boost);
- // the boost should now be set
- $this->assertEquals($boost, $this->_fixture->getBoost());
- }
- public function testSetAndGetBoostWithZeroRemovesBoost()
- {
- $this->_fixture->setBoost(0);
- // should be boolean false
- $this->assertTrue($this->_fixture->getBoost() === false);
- }
- public function testSetAndGetBoostWithNegativeNumberRemovesBoost()
- {
- $this->_fixture->setBoost(-1);
- // should be boolean false
- $this->assertTrue($this->_fixture->getBoost() === false);
- }
- public function testSetAndGetBoostWithNonNumberRemovesBoost()
- {
- $this->_fixture->setBoost("i am not a number");
- // should be boolean false
- $this->assertTrue($this->_fixture->getBoost() === false);
- }
- public function testAddFieldCreatesMultiValueWhenFieldDoesNotExist()
- {
- $field = 'field';
- $value = 'value';
- $this->_fixture->addField($field, $value);
- // check that value is an array with correct values
- $fieldValue = $this->_fixture->{$field};
- $this->assertTrue(is_array($fieldValue));
- $this->assertEquals(1, count($fieldValue));
- $this->assertEquals($value, $fieldValue[0]);
- }
- /**
- * setMultiValue has been deprecated and defers to addField
- *
- * @deprecated
- */
- public function testSetMultiValueCreateMultiValueWhenFieldDoesNotExist()
- {
- $field = 'field';
- $value = 'value';
- $this->_fixture->setMultiValue($field, $value);
- // check that value is an array with correct values
- $fieldValue = $this->_fixture->{$field};
- $this->assertTrue(is_array($fieldValue));
- $this->assertEquals(1, count($fieldValue));
- $this->assertEquals($value, $fieldValue[0]);
- }
- public function testAddFieldCreatesMultiValueWhenFieldDoesExistAsSingleValue()
- {
- $field = 'field';
- $value1 = 'value1';
- $value2 = 'value2';
- // set first value as singular value
- $this->_fixture->{$field} = $value1;
- // add a second value with addField
- $this->_fixture->addField($field, $value2);
- // check that value is an array with correct values
- $fieldValue = $this->_fixture->{$field};
- $this->assertTrue(is_array($fieldValue));
- $this->assertEquals(2, count($fieldValue));
- $this->assertEquals($value1, $fieldValue[0]);
- $this->assertEquals($value2, $fieldValue[1]);
- }
- /**
- * setMultiValue has been deprecated and defers to addField
- *
- * @deprecated
- */
- public function testSetMultiValueCreatesMultiValueWhenFieldDoesExistAsSingleValue()
- {
- $field = 'field';
- $value1 = 'value1';
- $value2 = 'value2';
- // set first value as singular value
- $this->_fixture->{$field} = $value1;
- // add a second value with addField
- $this->_fixture->setMultiValue($field, $value2);
- // check that value is an array with correct values
- $fieldValue = $this->_fixture->{$field};
- $this->assertTrue(is_array($fieldValue));
- $this->assertEquals(2, count($fieldValue));
- $this->assertEquals($value1, $fieldValue[0]);
- $this->assertEquals($value2, $fieldValue[1]);
- }
- public function testAddFieldWithBoostSetsFieldBoost()
- {
- $field = 'field';
- $boost = 0.5;
- $this->_fixture->addField($field, 'value', $boost);
- // check the field boost
- $this->assertEquals($boost, $this->_fixture->getFieldBoost($field));
- }
- public function testAddFieldWithBoostMultipliesWithAPreexistingBoost()
- {
- $field = 'field';
- $boost = 0.5;
- // set a field with a boost
- $this->_fixture->setField($field, 'value1', $boost);
- // now add another value with the same boost
- $this->_fixture->addField($field, 'value2', $boost);
- // new boost should be $boost * $boost
- $this->assertEquals($boost * $boost, $this->_fixture->getFieldBoost($field));
- }
- public function testGetFieldNamesIsInitiallyEmpty()
- {
- $fieldNames = $this->_fixture->getFieldNames();
- $this->assertTrue(empty($fieldNames));
- }
- public function testGetFieldNamesAfterFieldIsSetIsNotEmpty()
- {
- $field = 'field';
- $this->_fixture->{$field} = 'value';
- $fieldNames = $this->_fixture->getFieldNames();
- $this->assertTrue(!empty($fieldNames));
- $this->assertEquals(1, count($fieldNames));
- $this->assertEquals($field, $fieldNames[0]);
- }
- public function testGetFieldValuesIsInitiallyEmpty()
- {
- $fieldValues = $this->_fixture->getFieldValues();
- $this->assertTrue(empty($fieldValues));
- }
- public function testGetFieldValuessAfterFieldIsSetIsNotEmpty()
- {
- $value = 'value';
- $this->_fixture->field = $value;
- $fieldValues = $this->_fixture->getFieldValues();
- $this->assertTrue(!empty($fieldValues));
- $this->assertEquals(1, count($fieldValues));
- $this->assertEquals($value, $fieldValues[0]);
- }
- public function testGetIteratorAfterFieldValueIsSet()
- {
- $field = 'field';
- $value = 'value';
- $this->_fixture->{$field} = $value;
- $itemCount = 0;
- foreach ($this->_fixture as $iteratedField => $iteratedValue)
- {
- ++$itemCount;
- // test field and value
- $this->assertEquals($field, $iteratedField);
- $this->assertEquals($value, $iteratedValue);
- }
- // test number of iterations is 1
- $this->assertEquals(1, $itemCount);
- }
- public function testClearReturnsDocumentToDefaultState()
- {
- // set the document boost
- $this->_fixture->setBoost(0.5);
- // set a field
- $this->_fixture->someField = "some value";
- // clear the document to remove boost and fields
- $this->_fixture->clear();
- // document boost should now be false
- $this->assertFalse($this->_fixture->getBoost());
- // document fields should now be empty
- $this->assertEquals(0, count($this->_fixture->getFieldNames()));
- $this->assertEquals(0, count($this->_fixture->getFieldValues()));
- $this->assertEquals(0, count($this->_fixture->getFieldBoosts()));
- // document iterator should now be empty
- $this->assertEquals(0, iterator_count($this->_fixture));
- }
- }
|