Update existing unit tests

Make existing unit tests work with new code base.
Also add new tests for new code to existing tests.
This commit is contained in:
Daniel Siepmann 2017-07-06 13:53:29 +02:00
parent 975381cc4a
commit 05f846a1cf
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
5 changed files with 87 additions and 30 deletions

View file

@ -35,6 +35,10 @@ class FacetRequest implements FacetRequestInterface
protected $field = ''; protected $field = '';
/** /**
* TODO: Add validation / exception?
* As the facets come from configuration this might be a good idea to help
* integrators find issues.
*
* @param string $identifier * @param string $identifier
* @param string $field * @param string $field
*/ */

View file

@ -67,7 +67,6 @@ class QueryFactory
$this->addFilter($searchRequest); $this->addFilter($searchRequest);
$this->addFacets($searchRequest); $this->addFacets($searchRequest);
// TODO: Add logging here.
$this->logger->debug('Generated elasticsearch query.', [$this->query]); $this->logger->debug('Generated elasticsearch query.', [$this->query]);
return new \Elastica\Query($this->query); return new \Elastica\Query($this->query);
} }

View file

@ -24,4 +24,23 @@ use TYPO3\CMS\Core\Tests\UnitTestCase as CoreTestCase;
abstract class AbstractUnitTestCase extends CoreTestCase abstract class AbstractUnitTestCase extends CoreTestCase
{ {
/**
* @return \TYPO3\CMS\Core\Log\LogManager
*/
protected function getMockedLogger()
{
$logger = $this->getMockBuilder(\TYPO3\CMS\Core\Log\LogManager::class)
->disableOriginalConstructor()
->setMethods(['getLogger'])
->getMock();
$logger->expects($this->once())
->method('getLogger')
->will($this->returnValue(
$this->getMockBuilder(\TYPO3\CMS\Core\Log\Logger::class)
->disableOriginalConstructor()
->getMock()
));
return $logger;
}
} }

View file

@ -41,24 +41,13 @@ class TcaTableServiceTest extends AbstractUnitTestCase
parent::setUp(); parent::setUp();
$this->configuration = $this->getMockBuilder(ConfigurationContainerInterface::class)->getMock(); $this->configuration = $this->getMockBuilder(ConfigurationContainerInterface::class)->getMock();
$logger = $this->getMockBuilder(\TYPO3\CMS\Core\Log\LogManager::class)
->disableOriginalConstructor()
->setMethods(['getLogger'])
->getMock();
$logger->expects($this->once())
->method('getLogger')
->will($this->returnValue(
$this->getMockBuilder(\TYPO3\CMS\Core\Log\Logger::class)
->disableOriginalConstructor()
->getMock()
));
$this->subject = $this->getMockBuilder(TcaTableService::class) $this->subject = $this->getMockBuilder(TcaTableService::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethodsExcept(['getWhereClause', 'injectLogger', 'getTableName']) ->setMethodsExcept(['getWhereClause', 'injectLogger', 'getTableName'])
->getMock(); ->getMock();
$this->inject($this->subject, 'configuration', $this->configuration); $this->inject($this->subject, 'configuration', $this->configuration);
$this->inject($this->subject, 'logger', $logger); $this->inject($this->subject, 'logger', $this->getMockedLogger());
$this->inject($this->subject, 'tableName', 'table'); $this->inject($this->subject, 'tableName', 'table');
} }

View file

@ -20,7 +20,7 @@ namespace Leonmrni\SearchCore\Tests\Unit\Domain\Search;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use Leonmrni\SearchCore\Connection; use Leonmrni\SearchCore\Domain\Model\FacetRequest;
use Leonmrni\SearchCore\Domain\Model\SearchRequest; use Leonmrni\SearchCore\Domain\Model\SearchRequest;
use Leonmrni\SearchCore\Domain\Search\QueryFactory; use Leonmrni\SearchCore\Domain\Search\QueryFactory;
use Leonmrni\SearchCore\Tests\Unit\AbstractUnitTestCase; use Leonmrni\SearchCore\Tests\Unit\AbstractUnitTestCase;
@ -36,7 +36,7 @@ class QueryFactoryTest extends AbstractUnitTestCase
{ {
parent::setUp(); parent::setUp();
$this->subject = new QueryFactory; $this->subject = new QueryFactory($this->getMockedLogger());
} }
/** /**
@ -44,12 +44,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
*/ */
public function creatonOfQueryWorksInGeneral() public function creatonOfQueryWorksInGeneral()
{ {
$connection = $this->getMockBuilder(Connection\Elasticsearch::class)
->disableOriginalConstructor()
->getMock();
$searchRequest = new SearchRequest('SearchWord'); $searchRequest = new SearchRequest('SearchWord');
$query = $this->subject->create($connection, $searchRequest); $query = $this->subject->create($searchRequest);
$this->assertInstanceOf( $this->assertInstanceOf(
\Elastica\Query::class, \Elastica\Query::class,
$query, $query,
@ -62,32 +59,53 @@ class QueryFactoryTest extends AbstractUnitTestCase
*/ */
public function filterIsAddedToQuery() public function filterIsAddedToQuery()
{ {
$connection = $this->getMockBuilder(Connection\Elasticsearch::class)
->disableOriginalConstructor()
->getMock();
$searchRequest = new SearchRequest('SearchWord'); $searchRequest = new SearchRequest('SearchWord');
$searchRequest->setFilter(['field' => 'content']); $searchRequest->setFilter(['field' => 'content']);
$query = $this->subject->create($connection, $searchRequest); $query = $this->subject->create($searchRequest);
$this->assertSame( $this->assertSame(
['field' => 'content'], [
$query->toArray()['query']['bool']['filter']['term'], ['term' => ['field' => 'content']]
],
$query->toArray()['query']['bool']['filter'],
'Filter was not added to query.' 'Filter was not added to query.'
); );
} }
/**
* @test
*/
public function emptyFilterIsNotAddedToQuery()
{
$searchRequest = new SearchRequest('SearchWord');
$searchRequest->setFilter([
'field' => '',
'field1' => 0,
'field2' => false,
]);
$this->assertFalse(
$searchRequest->hasFilter(),
'Search request contains filter even if it should not.'
);
$query = $this->subject->create($searchRequest);
$this->assertSame(
null,
$query->toArray()['query']['bool']['filter'],
'Filter was added to query, even if no filter exists.'
);
}
/** /**
* @test * @test
*/ */
public function userInputIsAlwaysString() public function userInputIsAlwaysString()
{ {
$connection = $this->getMockBuilder(Connection\Elasticsearch::class)
->disableOriginalConstructor()
->getMock();
$searchRequest = new SearchRequest(10); $searchRequest = new SearchRequest(10);
$searchRequest->setFilter(['field' => 20]); $searchRequest->setFilter(['field' => 20]);
$query = $this->subject->create($connection, $searchRequest); $query = $this->subject->create($searchRequest);
$this->assertSame( $this->assertSame(
'10', '10',
$query->toArray()['query']['bool']['must'][0]['match']['_all'], $query->toArray()['query']['bool']['must'][0]['match']['_all'],
@ -95,8 +113,36 @@ class QueryFactoryTest extends AbstractUnitTestCase
); );
$this->assertSame( $this->assertSame(
'20', '20',
$query->toArray()['query']['bool']['filter']['term']['field'], $query->toArray()['query']['bool']['filter'][0]['term']['field'],
'Search word was not escaped as expected.' 'Search word was not escaped as expected.'
); );
} }
/**
* @test
*/
public function facetsAreAddedToQuery()
{
$searchRequest = new SearchRequest('SearchWord');
$searchRequest->addFacet(new FacetRequest('Identifier', 'FieldName'));
$searchRequest->addFacet(new FacetRequest('Identifier 2', 'FieldName 2'));
$query = $this->subject->create($searchRequest);
$this->assertSame(
[
'Identifier' => [
'terms' => [
'field' => 'FieldName',
],
],
'Identifier 2' => [
'terms' => [
'field' => 'FieldName 2',
],
],
],
$query->toArray()['aggs'],
'Facets were not added to query.'
);
}
} }