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:
Daniel Siepmann 2018-03-06 09:36:39 +01:00
parent 79aba3c544
commit cf91251be3
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 75 additions and 12 deletions

View file

@ -158,14 +158,20 @@ class SearchRequest implements SearchRequestInterface
// Current implementation covers only paginate widget support. // Current implementation covers only paginate widget support.
public function execute($returnRawQueryResult = false) public function execute($returnRawQueryResult = false)
{ {
if ($this->connection instanceof ConnectionInterface) { if (! ($this->connection instanceof ConnectionInterface)) {
return $this->searchService->processResult($this->connection->search($this)); 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( return $this->searchService->processResult($this->connection->search($this));
'Connection was not set before, therefore execute can not work. Use `setConnection` before.',
1502197732
);
} }
public function setLimit($limit) public function setLimit($limit)

View file

@ -20,7 +20,10 @@ namespace Codappix\SearchCore\Tests\Unit\Domain\Model;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use Codappix\SearchCore\Connection\ConnectionInterface;
use Codappix\SearchCore\Connection\SearchResultInterface;
use Codappix\SearchCore\Domain\Model\SearchRequest; use Codappix\SearchCore\Domain\Model\SearchRequest;
use Codappix\SearchCore\Domain\Search\SearchService;
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase; use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
class SearchRequestTest extends AbstractUnitTestCase class SearchRequestTest extends AbstractUnitTestCase
@ -31,12 +34,12 @@ class SearchRequestTest extends AbstractUnitTestCase
*/ */
public function emptyFilterWillNotBeSet(array $filter) public function emptyFilterWillNotBeSet(array $filter)
{ {
$searchRequest = new SearchRequest(); $subject = new SearchRequest();
$searchRequest->setFilter($filter); $subject->setFilter($filter);
$this->assertSame( $this->assertSame(
[], [],
$searchRequest->getFilter(), $subject->getFilter(),
'Empty filter were set, even if they should not.' 'Empty filter were set, even if they should not.'
); );
} }
@ -68,13 +71,67 @@ class SearchRequestTest extends AbstractUnitTestCase
public function filterIsSet() public function filterIsSet()
{ {
$filter = ['someField' => 'someValue']; $filter = ['someField' => 'someValue'];
$searchRequest = new SearchRequest(); $subject = new SearchRequest();
$searchRequest->setFilter($filter); $subject->setFilter($filter);
$this->assertSame( $this->assertSame(
$filter, $filter,
$searchRequest->getFilter(), $subject->getFilter(),
'Filter was not set.' '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();
}
} }