Skip to content

Commit

Permalink
Fix fatal error on Query::addScriptField() (#1086)
Browse files Browse the repository at this point in the history
script_fields must have type ScriptFields
  • Loading branch information
s12v authored and ruflin committed May 6, 2016
1 parent fcf0737 commit 6460d24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file based on the

## [Unreleased](https://github.com/ruflin/Elastica/compare/3.2.0...HEAD)

### Bugfixes
- Fix fatal error on `Query::addScriptField()` if scripts were already set via `setScriptFields()` #1086

## [3.2.0](https://github.com/ruflin/Elastica/compare/3.1.1...3.2.0)

### Backward Compatibility Breaks
Expand Down
6 changes: 5 additions & 1 deletion lib/Elastica/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,11 @@ public function setScriptFields($scriptFields)
*/
public function addScriptField($name, AbstractScript $script)
{
$this->_params['script_fields'][$name] = $script;
if (isset($this->_params['script_fields'])) {
$this->_params['script_fields']->addScript($name, $script);
} else {
$this->setScriptFields(array($name => $script));
}

return $this;
}
Expand Down
25 changes: 25 additions & 0 deletions test/lib/Elastica/Test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,31 @@ public function testAddScriptFieldsToArrayCast()
$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}

/**
* @group unit
*/
public function testAddScriptFieldToExistingScriptFields()
{
$script1 = new Script('s1');
$script2 = new Script('s2');

// add script1, then add script2
$query = new Query();
$scriptFields1 = new ScriptFields();
$scriptFields1->addScript('script1', $script1);
$query->setScriptFields($scriptFields1);
$query->addScriptField('script2', $script2);

// add script1 and script2 at once
$anotherQuery = new Query();
$scriptFields2 = new ScriptFields();
$scriptFields2->addScript('script1', $script1);
$scriptFields2->addScript('script2', $script2);
$anotherQuery->setScriptFields($scriptFields2);

$this->assertEquals($query->toArray(), $anotherQuery->toArray());
}

/**
* @group unit
*/
Expand Down

0 comments on commit 6460d24

Please sign in to comment.