mirror of
https://github.com/Codappix/search_core.git
synced 2024-12-25 13:56:09 +01:00
TASK :Add tests for SearchRequest
Make sure exceptions with helpful messages are thrown if one object is missing when execute is called. Also make sure the expected methods are called.
This commit is contained in:
parent
79aba3c544
commit
cf91251be3
2 changed files with 75 additions and 12 deletions
|
@ -158,14 +158,20 @@ class SearchRequest implements SearchRequestInterface
|
|||
// Current implementation covers only paginate widget support.
|
||||
public function execute($returnRawQueryResult = false)
|
||||
{
|
||||
if ($this->connection instanceof ConnectionInterface) {
|
||||
return $this->searchService->processResult($this->connection->search($this));
|
||||
if (! ($this->connection instanceof ConnectionInterface)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Connection was not set before, therefore execute can not work. Use `setConnection` before.',
|
||||
1502197732
|
||||
);
|
||||
}
|
||||
if (! ($this->searchService instanceof SearchService)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'SearchService was not set before, therefore execute can not work. Use `setSearchService` before.',
|
||||
1520325175
|
||||
);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(
|
||||
'Connection was not set before, therefore execute can not work. Use `setConnection` before.',
|
||||
1502197732
|
||||
);
|
||||
return $this->searchService->processResult($this->connection->search($this));
|
||||
}
|
||||
|
||||
public function setLimit($limit)
|
||||
|
|
|
@ -20,7 +20,10 @@ namespace Codappix\SearchCore\Tests\Unit\Domain\Model;
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use Codappix\SearchCore\Connection\ConnectionInterface;
|
||||
use Codappix\SearchCore\Connection\SearchResultInterface;
|
||||
use Codappix\SearchCore\Domain\Model\SearchRequest;
|
||||
use Codappix\SearchCore\Domain\Search\SearchService;
|
||||
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
|
||||
|
||||
class SearchRequestTest extends AbstractUnitTestCase
|
||||
|
@ -31,12 +34,12 @@ class SearchRequestTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function emptyFilterWillNotBeSet(array $filter)
|
||||
{
|
||||
$searchRequest = new SearchRequest();
|
||||
$searchRequest->setFilter($filter);
|
||||
$subject = new SearchRequest();
|
||||
$subject->setFilter($filter);
|
||||
|
||||
$this->assertSame(
|
||||
[],
|
||||
$searchRequest->getFilter(),
|
||||
$subject->getFilter(),
|
||||
'Empty filter were set, even if they should not.'
|
||||
);
|
||||
}
|
||||
|
@ -68,13 +71,67 @@ class SearchRequestTest extends AbstractUnitTestCase
|
|||
public function filterIsSet()
|
||||
{
|
||||
$filter = ['someField' => 'someValue'];
|
||||
$searchRequest = new SearchRequest();
|
||||
$searchRequest->setFilter($filter);
|
||||
$subject = new SearchRequest();
|
||||
$subject->setFilter($filter);
|
||||
|
||||
$this->assertSame(
|
||||
$filter,
|
||||
$searchRequest->getFilter(),
|
||||
$subject->getFilter(),
|
||||
'Filter was not set.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function exceptionIsThrownIfSearchServiceWasNotSet()
|
||||
{
|
||||
$subject = new SearchRequest();
|
||||
$subject->setConnection($this->getMockBuilder(ConnectionInterface::class)->getMock());
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$subject->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function exceptionIsThrownIfConnectionWasNotSet()
|
||||
{
|
||||
$subject = new SearchRequest();
|
||||
$subject->setSearchService(
|
||||
$this->getMockBuilder(SearchService::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock()
|
||||
);
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$subject->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function executionMakesUseOfProvidedConnectionAndSearchService()
|
||||
{
|
||||
$searchServiceMock = $this->getMockBuilder(SearchService::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$connectionMock = $this->getMockBuilder(ConnectionInterface::class)
|
||||
->getMock();
|
||||
$searchResultMock = $this->getMockBuilder(SearchResultInterface::class)
|
||||
->getMock();
|
||||
|
||||
$subject = new SearchRequest();
|
||||
$subject->setSearchService($searchServiceMock);
|
||||
$subject->setConnection($connectionMock);
|
||||
|
||||
$connectionMock->expects($this->once())
|
||||
->method('search')
|
||||
->with($subject)
|
||||
->willReturn($searchResultMock);
|
||||
$searchServiceMock->expects($this->once())
|
||||
->method('processResult')
|
||||
->with($searchResultMock);
|
||||
|
||||
$subject->execute();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue