diff --git a/lib/Elastica/Query/Fuzzy.php b/lib/Elastica/Query/Fuzzy.php index 2ed803a943..f2ac849146 100644 --- a/lib/Elastica/Query/Fuzzy.php +++ b/lib/Elastica/Query/Fuzzy.php @@ -38,7 +38,7 @@ public function setField($fieldName, $value) if (!is_string($value) || !is_string($fieldName)) { throw new InvalidException('The field and value arguments must be of type string.'); } - if (count($this->getParams()) > 0 && array_shift(array_keys($this->getParams())) != $fieldName) { + if (count($this->getParams()) > 0 && key($this->getParams()) !== $fieldName) { throw new InvalidException('Fuzzy query can only support a single field.'); } @@ -48,22 +48,22 @@ public function setField($fieldName, $value) /** * Set optional parameters on the existing query. * - * @param string $param option name + * @param string $option option name * @param mixed $value Value of the parameter * * @return $this */ - public function setFieldOption($param, $value) + public function setFieldOption($option, $value) { //Retrieve the single existing field for alteration. $params = $this->getParams(); if (count($params) < 1) { throw new InvalidException('No field has been set'); } - $keyArray = array_keys($params); - $params[$keyArray[0]][$param] = $value; + $key = key($params); + $params[$key][$option] = $value; - return $this->setParam($keyArray[0], $params[$keyArray[0]]); + return $this->setParam($key, $params[$key]); } /** diff --git a/test/lib/Elastica/Test/Query/FuzzyTest.php b/test/lib/Elastica/Test/Query/FuzzyTest.php index 3ce3ced7f3..a55008f261 100644 --- a/test/lib/Elastica/Test/Query/FuzzyTest.php +++ b/test/lib/Elastica/Test/Query/FuzzyTest.php @@ -68,6 +68,16 @@ public function testToArray() $this->assertEquals($expectedArray, $fuzzy->toArray()); } + /** + * @group unit + */ + public function testNeedSetFieldBeforeOption() + { + $fuzzy = new Fuzzy(); + $this->setExpectedException('Elastica\Exception\InvalidException', 'No field has been set'); + $fuzzy->setFieldOption('boost', 1.0); + } + /** * @group functional */ @@ -89,10 +99,10 @@ public function testQuery() $field = 'name'; - $query = new Fuzzy(); - $query->setField($field, 'Baden'); + $fuzzy = new Fuzzy(); + $fuzzy->setField($field, 'Baden'); - $resultSet = $index->search($query); + $resultSet = $index->search($fuzzy); $this->assertEquals(2, $resultSet->count()); } @@ -100,23 +110,63 @@ public function testQuery() /** * @group unit */ - public function testBadArguments() + public function testAddSingleField() { - $this->setExpectedException('Elastica\Exception\InvalidException'); - $query = new Fuzzy(); + $this->setExpectedException('Elastica\Exception\InvalidException', 'Fuzzy query can only support a single field.'); + $fuzzy = new Fuzzy(); $this->hideDeprecated(); - $query->addField('name', [['value' => 'Baden']]); + $fuzzy->addField('name', [['value' => 'Baden']]); $this->showDeprecated(); + } + + /** + * @group unit + */ + public function testResetSingleField() + { + $fuzzy = new Fuzzy(); + $fuzzy->setField('name', 'value'); + $fuzzy->setField('name', 'other'); + $expected = [ + 'fuzzy' => [ + 'name' => [ + 'value' => 'other', + ], + ], + ]; + $this->assertEquals($expected, $fuzzy->toArray()); + } - $this->setExpectedException('Elastica\Exception\InvalidException'); - $query = new Fuzzy(); - $query->setField('name', []); + /** + * @group unit + */ + public function testOnlySetSingleField() + { + $fuzzy = new Fuzzy(); + $fuzzy->setField('name', 'value'); + $this->setExpectedException('Elastica\Exception\InvalidException', 'Fuzzy query can only support a single field.'); + $fuzzy->setField('name1', 'value1'); + } - $this->setExpectedException('Elastica\Exception\InvalidException'); - $query = new Fuzzy(); - $query->setField('name', 'value'); - $query->setField('name1', 'value1'); + /** + * @group unit + */ + public function testFieldNameMustBeString() + { + $fuzzy = new Fuzzy(); + $this->setExpectedException('Elastica\Exception\InvalidException', 'The field and value arguments must be of type string.'); + $fuzzy->setField(['name'], 'value'); + } + + /** + * @group unit + */ + public function testValueMustBeString() + { + $fuzzy = new Fuzzy(); + $this->setExpectedException('Elastica\Exception\InvalidException', 'The field and value arguments must be of type string.'); + $fuzzy->setField('name', ['value']); } /** @@ -124,9 +174,9 @@ public function testBadArguments() */ public function testAddFieldDeprecated() { - $query = new Fuzzy(); + $fuzzy = new Fuzzy(); $errorCollector = $this->startCollectErrors(); - $query->addField('user', ['value' => 'Nicolas', 'boost' => 1.0]); + $fuzzy->addField('user', ['value' => 'Nicolas', 'boost' => 1.0]); $this->finishCollectErrors(); $errorCollector->assertOnlyOneDeprecatedError('Query\Fuzzy::addField is deprecated. Use setField and setFieldOption instead. This method will be removed in further Elastica releases');