Skip to content
Snippets Groups Projects
Commit 187ed0da authored by Demian Katz's avatar Demian Katz
Browse files

Refactored for clarity; improved test coverage.

parent d8d6c1ae
No related merge requests found
......@@ -99,14 +99,9 @@ class RecordCollection extends AbstractRecordCollection
public function getSpellcheck()
{
if (!$this->spellcheck) {
$params = isset($this->response['responseHeader']['params'])
? $this->response['responseHeader']['params'] : array();
$sq = isset($params['spellcheck.q'])
? $params['spellcheck.q']
: (isset($params['q']) ? $params['q'] : '');
$sugg = isset($this->response['spellcheck']['suggestions'])
? $this->response['spellcheck']['suggestions'] : array();
$this->spellcheck = new Spellcheck($sugg, $sq);
$this->spellcheck = new Spellcheck(
$this->getRawSpellcheckSuggestions(), $this->getSpellcheckQuery()
);
}
return $this->spellcheck;
}
......@@ -155,4 +150,39 @@ class RecordCollection extends AbstractRecordCollection
return isset($this->response['highlighting'])
? $this->response['highlighting'] : array();
}
/**
* Get raw Solr input parameters from the response.
*
* @return array
*/
protected function getSolrParameters()
{
return isset($this->response['responseHeader']['params'])
? $this->response['responseHeader']['params'] : array();
}
/**
* Extract the best matching Spellcheck query from the raw Solr input parameters.
*
* @return string
*/
protected function getSpellcheckQuery()
{
$params = $this->getSolrParameters();
return isset($params['spellcheck.q'])
? $params['spellcheck.q']
: (isset($params['q']) ? $params['q'] : '');
}
/**
* Get raw Solr Spellcheck suggestions.
*
* @return array
*/
protected function getRawSpellcheckSuggestions()
{
return isset($this->response['spellcheck']['suggestions'])
? $this->response['spellcheck']['suggestions'] : array();
}
}
\ No newline at end of file
......@@ -87,6 +87,66 @@ class RecordCollectionTest extends PHPUnit_Framework_TestCase
$this->assertEquals(6, $coll->key());
}
/**
* Test spelling query retrieval.
*
* @return void
*/
public function testSpellingQuery()
{
$input = array(
'responseHeader' => array(
'params' => array(
'spellcheck.q' => 'foo',
'q' => 'bar',
)
)
);
$coll = new RecordCollection($input);
$this->assertEquals('foo', $coll->getSpellcheck()->getQuery());
unset($input['responseHeader']['params']['spellcheck.q']);
$coll = new RecordCollection($input);
$this->assertEquals('bar', $coll->getSpellcheck()->getQuery());
}
/**
* Test spelling suggestion retrieval.
*
* @return void
*/
public function testSpellingSuggestions()
{
$input = array(
'spellcheck' => array(
'suggestions' => array(
array(
'frunkensteen',
array(
'numFound' => 6,
'startOffset' => 0,
'endOffset' => 12,
'origFreq' => 0,
'suggestion' => array(
array(
'word' => 'frankenstein',
'freq' => 218,
),
array(
'word' => 'funkenstein',
'freq' => 10,
),
),
),
),
array('correctlySpelled', false),
)
)
);
$coll = new RecordCollection($input);
$spell = $coll->getSpellcheck();
$this->assertEquals(1, count($spell));
}
/**
* Test the replace method.
*
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment