Skip to content

Commit

Permalink
Cleanup Fuzzy Query and Tests (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenExe authored and ruflin committed Jul 26, 2016
1 parent 7d8c60b commit a25b6c7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
12 changes: 6 additions & 6 deletions lib/Elastica/Query/Fuzzy.php
Expand Up @@ -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.');
}

Expand All @@ -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]);
}

/**
Expand Down
82 changes: 66 additions & 16 deletions test/lib/Elastica/Test/Query/FuzzyTest.php
Expand Up @@ -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
*/
Expand All @@ -89,44 +99,84 @@ 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());
}

/**
* @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']);
}

/**
* @group unit
*/
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');
Expand Down

0 comments on commit a25b6c7

Please sign in to comment.