Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

Commit

Permalink
Handle case where no results are returned
Browse files Browse the repository at this point in the history
When 0 results are returned, Forage returns a string "no results"
instead of a properly formatted JSON string. So json_decode() returns
null, and that gets passed to SearchResult. SearchResult only accepts an
array as input, and throws an E_RECOVERABLE_ERROR. Http Transport now
intercepts this case, and outputs a properly formatted array containing
no hits or facets, and set totalHits to 0.

PHP error:
Catchable fatal error: Argument 1 passed to
vierbergenlars\Forage\SearchResult\SearchResult::__construct() must be
an array, null given, called in
/var/www/bay/vendor/vierbergenlars/forage-client/lib/vierbergenlars/Forage/SearchQuery/Query.php
on line 174 and defined in
/var/www/bay/vendor/vierbergenlars/forage-client/lib/vierbergenlars/Forage/SearchResult/SearchResult.php
on line 42

XDebug Trace:
8	0.1450	21947952
vierbergenlars\Forage\ODM\SearchQuery->execute( )
../SearchController.php:43
9	0.1451	21947952
vierbergenlars\Forage\SearchQuery\Query->execute( )
../SearchQuery.php:44
10	0.1490	21999232
vierbergenlars\Forage\SearchResult\SearchResult->__construct(
$result_array = NULL )	../Query.php:174
  • Loading branch information
vierbergenlars committed Oct 5, 2013
1 parent 2ad7836 commit c61dde9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/vierbergenlars/Forage/Transport/Http.php
Expand Up @@ -145,7 +145,13 @@ public function search(
if(curl_errno($ch))
throw new TransportException('cURL error: '.curl_error($ch), curl_errno($ch));
curl_close($ch);

if($resp === 'no results') {
return array(
'totalHits'=>0,
'facets'=>array(),
'hits'=> array(),
);
}
return json_decode($resp, true);
}

Expand Down
12 changes: 11 additions & 1 deletion test/transport/http.php
Expand Up @@ -72,6 +72,17 @@ function testBasicSearch()
$this->assertEqual($lolCatSearch['totalHits'], 2);
}

function testSearchEmptyResultSet()
{
$search = $this->transport->search('s');
var_dump($search);
$this->assertTrue(is_array($search));
$this->assertEqual($search['totalHits'], 0);
$this->assertEqual($search['hits'], array());
$this->assertEqual($search['facets'], array());
}


function testFieldedSearch()
{
$lolSearch = $this->transport->search('Lol', array('title'));
Expand Down Expand Up @@ -131,5 +142,4 @@ function testDelete()
$lolSearch = $this->transport->search('Lol');
$this->assertEqual($lolSearch['totalHits'], 2);
}

}

0 comments on commit c61dde9

Please sign in to comment.