Create test for search provider

refs #223 refs #377
This commit is contained in:
Julien Fastré 2015-01-03 22:03:16 +01:00
parent 3a9c1dcc07
commit 94b213ccf3
2 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
/*
* Chill is a software for social workers
* Copyright (C) 2014 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Search;
/**
* Throw by search provider when the search name is not found
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class UnknowSearchNameException extends \Exception
{
public function __construct($name)
{
parent::__construct( "The module search with name $name "
. "is not found");
}
}

View File

@ -20,13 +20,30 @@
namespace Chill\MainBundle\Test\Search;
use Chill\MainBundle\Search\SearchProvider;
use Chill\MainBundle\Search\SearchInterface;
class SearchProviderTest extends \PHPUnit_Framework_TestCase
{
/**
*
* @var SearchProvider
*/
private $search;
public function setUp()
{
$this->search = new SearchProvider();
//add a default service
$this->addSearchService(
$this->createDefaultSearchService('I am default', 10), 'default'
);
//add a domain service
$this->addSearchService(
$this->createDefaultSearchService('I am domain bar', 20), 'bar'
);
}
/**
@ -148,6 +165,74 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
), $terms);
}
/**
* Test the behaviour when no domain is provided in the search pattern :
* the default search should be enabled
*/
public function testDefaultSearch()
{
$response = $this->search->getSearchResults('default search');
$this->markTestSkipped();
$this->assertEquals(array(
"I am default"
), $response);
}
/**
* @expectedException \Chill\MainBundle\Search\UnknowSearchDomainException
*/
public function testDomainUnknow()
{
$response = $this->search->getSearchResults('@unknow domain');
$this->markTestSkipped();
}
public function testDomainSearch()
{
//add a search service which will be supported
$this->addSearchService(
$this->createNonDefaultDomainSearchService("I am domain foo", 100, TRUE), 'foo'
);
$response = $this->search->getSearchResults('@foo default search');
$this->assertEquals(array(
"I am domain foo"
), $response);
}
public function testSearchWithinSpecificSearchName()
{
$this->markTestSkipped();
//add a search service which will be supported
$this->addSearchService(
$this->createNonDefaultDomainSearchService("I am domain foo", 100, TRUE), 'foo'
);
$response = $this->search->getResultByName('@foo search', 'foo');
$this->assertEquals(array(
'I am domain foo'
), $response);
}
/**
* @expectedException \Chill\MainBundle\Search\ParsingException
*/
public function testSearchWithinSpecificSearchNameInConflictWithSupport()
{
$this->markTestSkipped();
$response = $this->search->getResultByName('@foo default search', 'bar');
}
/**
* shortcut for executing parse method
*
@ -158,4 +243,66 @@ class SearchProviderTest extends \PHPUnit_Framework_TestCase
{
return $this->search->parse($pattern);
}
/**
* Add a search service to the chill.main.search_provider
*
* Useful for mocking the SearchInterface
*
* @param SearchInterface $search
* @param string $name
*/
private function addSearchService(SearchInterface $search, $name)
{
$this->search
->addSearchService($search, $name);
}
private function createDefaultSearchService($result, $order)
{
$mock = $this
->getMockForAbstractClass('Chill\MainBundle\Search\AbstractSearch');
//set the mock as default
$mock->expects($this->any())
->method('isActiveByDefault')
->will($this->returnValue(TRUE));
$mock->expects($this->any())
->method('getOrder')
->will($this->returnValue($order));
//set the return value
$mock->expects($this->any())
->method('renderResult')
->will($this->returnValue($result));
return $mock;
}
private function createNonDefaultDomainSearchService($result, $order, $domain)
{
$mock = $this
->getMockForAbstractClass('Chill\MainBundle\Search\AbstractSearch');
//set the mock as default
$mock->expects($this->any())
->method('isActiveByDefault')
->will($this->returnValue(FALSE));
$mock->expects($this->any())
->method('getOrder')
->will($this->returnValue($order));
$mock->expects($this->any())
->method('supports')
->will($this->returnValue($domain));
//set the return value
$mock->expects($this->any())
->method('renderResult')
->will($this->returnValue($result));
return $mock;
}
}