Set behaviour of searchProvider as expected

refs #223 refs #377
This commit is contained in:
Julien Fastré 2015-01-03 23:17:53 +01:00
parent 94b213ccf3
commit eea91de0e0
4 changed files with 61 additions and 24 deletions

View File

@ -124,20 +124,55 @@ class SearchProvider
* @param number $start * @param number $start
* @param number $limit * @param number $limit
* @return array of html results * @return array of html results
* @throws UnknowSearchDomainException if the domain is unknow
*/ */
public function getResults($pattern, $start = 0, $limit = 50) public function getSearchResults($pattern, $start = 0, $limit = 50)
{ {
$terms = $this->parse($pattern); $terms = $this->parse($pattern);
$results = array(); $results = array();
foreach ($this->searchServices as $service) { //sort searchServices by order
if ($service->supports($terms['_domain'])) { $sortedSearchServices = array();
$results[] = $service->renderResult($terms, $start, $limit); foreach($this->searchServices as $service) {
$sortedSearchServices[$service->getOrder()] = $service;
}
if ($terms['_domain'] !== NULL) {
foreach ($sortedSearchServices as $service) {
if ($service->supports($terms['_domain'])) {
$results[] = $service->renderResult($terms, $start, $limit);
}
}
if (count($results) === 0) {
throw new UnknowSearchDomainException($terms['_domain']);
}
} else { // no domain provided, we use default search
foreach($sortedSearchServices as $service) {
if ($service->isActiveByDefault()) {
$results[] = $service->renderResult($terms, $start, $limit);
}
} }
} }
//sort array
ksort($results);
return $results; return $results;
} }
public function getResultByName($pattern, $name, $start = 0, $limit = 50)
{
$terms = $this->parse($pattern);
$search = $this->getByName($name);
if ($terms['_domain'] !== NULL && !$search->supports($terms['_domain']))
{
throw new ParsingException("The domain is not supported for the search name");
}
return $search->renderResult($terms, $start, $limit);
}
/** /**
* return search services with a specific name, defined in service * return search services with a specific name, defined in service
@ -149,7 +184,7 @@ class SearchProvider
public function getByName($name) public function getByName($name)
{ {
if (isset($this->searchServices[$name])) { if (isset($this->searchServices[$name])) {
return $this->searchServices; return $this->searchServices[$name];
} else { } else {
throw new UnknowSearchNameException($name); throw new UnknowSearchNameException($name);
} }

View File

@ -25,11 +25,19 @@ namespace Chill\MainBundle\Search;
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
*/ */
class UnknowSearchNameException extends \Exception class UnknowSearchDomainException extends \Exception
{ {
public function __construct($name)
private $domain;
public function __construct($domain)
{ {
parent::__construct( "The module search with name $name " parent::__construct( "The domain $domain is not found");
. "is not found"); $this->domain = $domain;
}
public function getDomain()
{
return $this->domain;
} }
} }

View File

@ -29,7 +29,6 @@ class UnknowSearchNameException extends \Exception
{ {
public function __construct($name) public function __construct($name)
{ {
parent::__construct( "The module search with name $name " parent::__construct( "No module search supports with the name $name");
. "is not found");
} }
} }

View File

@ -42,7 +42,7 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
); );
//add a domain service //add a domain service
$this->addSearchService( $this->addSearchService(
$this->createDefaultSearchService('I am domain bar', 20), 'bar' $this->createNonDefaultDomainSearchService('I am domain bar', 20, FALSE), 'bar'
); );
} }
@ -169,11 +169,11 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
* Test the behaviour when no domain is provided in the search pattern : * Test the behaviour when no domain is provided in the search pattern :
* the default search should be enabled * the default search should be enabled
*/ */
public function testDefaultSearch() public function testSearchResultDefault()
{ {
$response = $this->search->getSearchResults('default search'); $response = $this->search->getSearchResults('default search');
$this->markTestSkipped(); //$this->markTestSkipped();
$this->assertEquals(array( $this->assertEquals(array(
"I am default" "I am default"
@ -183,15 +183,15 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException \Chill\MainBundle\Search\UnknowSearchDomainException * @expectedException \Chill\MainBundle\Search\UnknowSearchDomainException
*/ */
public function testDomainUnknow() public function testSearchResultDomainUnknow()
{ {
$response = $this->search->getSearchResults('@unknow domain'); $response = $this->search->getSearchResults('@unknow domain');
$this->markTestSkipped(); //$this->markTestSkipped();
} }
public function testDomainSearch() public function testSearchResultDomainSearch()
{ {
//add a search service which will be supported //add a search service which will be supported
$this->addSearchService( $this->addSearchService(
@ -208,7 +208,6 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
public function testSearchWithinSpecificSearchName() public function testSearchWithinSpecificSearchName()
{ {
$this->markTestSkipped();
//add a search service which will be supported //add a search service which will be supported
$this->addSearchService( $this->addSearchService(
$this->createNonDefaultDomainSearchService("I am domain foo", 100, TRUE), 'foo' $this->createNonDefaultDomainSearchService("I am domain foo", 100, TRUE), 'foo'
@ -216,9 +215,7 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
$response = $this->search->getResultByName('@foo search', 'foo'); $response = $this->search->getResultByName('@foo search', 'foo');
$this->assertEquals(array( $this->assertEquals('I am domain foo', $response);
'I am domain foo'
), $response);
} }
@ -227,8 +224,6 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testSearchWithinSpecificSearchNameInConflictWithSupport() public function testSearchWithinSpecificSearchNameInConflictWithSupport()
{ {
$this->markTestSkipped();
$response = $this->search->getResultByName('@foo default search', 'bar'); $response = $this->search->getResultByName('@foo default search', 'bar');
} }