mirror of
https://github.com/Codappix/search_core.git
synced 2024-12-22 22:56:10 +01:00
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:
parent
975381cc4a
commit
05f846a1cf
5 changed files with 87 additions and 30 deletions
|
@ -35,6 +35,10 @@ class FacetRequest implements FacetRequestInterface
|
|||
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 $field
|
||||
*/
|
||||
|
|
|
@ -67,7 +67,6 @@ class QueryFactory
|
|||
$this->addFilter($searchRequest);
|
||||
$this->addFacets($searchRequest);
|
||||
|
||||
// TODO: Add logging here.
|
||||
$this->logger->debug('Generated elasticsearch query.', [$this->query]);
|
||||
return new \Elastica\Query($this->query);
|
||||
}
|
||||
|
|
|
@ -24,4 +24,23 @@ use TYPO3\CMS\Core\Tests\UnitTestCase as 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,24 +41,13 @@ class TcaTableServiceTest extends AbstractUnitTestCase
|
|||
parent::setUp();
|
||||
|
||||
$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)
|
||||
->disableOriginalConstructor()
|
||||
->setMethodsExcept(['getWhereClause', 'injectLogger', 'getTableName'])
|
||||
->getMock();
|
||||
$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');
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace Leonmrni\SearchCore\Tests\Unit\Domain\Search;
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use Leonmrni\SearchCore\Connection;
|
||||
use Leonmrni\SearchCore\Domain\Model\FacetRequest;
|
||||
use Leonmrni\SearchCore\Domain\Model\SearchRequest;
|
||||
use Leonmrni\SearchCore\Domain\Search\QueryFactory;
|
||||
use Leonmrni\SearchCore\Tests\Unit\AbstractUnitTestCase;
|
||||
|
@ -36,7 +36,7 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->subject = new QueryFactory;
|
||||
$this->subject = new QueryFactory($this->getMockedLogger());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,12 +44,9 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function creatonOfQueryWorksInGeneral()
|
||||
{
|
||||
$connection = $this->getMockBuilder(Connection\Elasticsearch::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
|
||||
$query = $this->subject->create($connection, $searchRequest);
|
||||
$query = $this->subject->create($searchRequest);
|
||||
$this->assertInstanceOf(
|
||||
\Elastica\Query::class,
|
||||
$query,
|
||||
|
@ -62,32 +59,53 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
*/
|
||||
public function filterIsAddedToQuery()
|
||||
{
|
||||
$connection = $this->getMockBuilder(Connection\Elasticsearch::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$searchRequest = new SearchRequest('SearchWord');
|
||||
$searchRequest->setFilter(['field' => 'content']);
|
||||
|
||||
$query = $this->subject->create($connection, $searchRequest);
|
||||
$query = $this->subject->create($searchRequest);
|
||||
$this->assertSame(
|
||||
['field' => 'content'],
|
||||
$query->toArray()['query']['bool']['filter']['term'],
|
||||
[
|
||||
['term' => ['field' => 'content']]
|
||||
],
|
||||
$query->toArray()['query']['bool']['filter'],
|
||||
'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
|
||||
*/
|
||||
public function userInputIsAlwaysString()
|
||||
{
|
||||
$connection = $this->getMockBuilder(Connection\Elasticsearch::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$searchRequest = new SearchRequest(10);
|
||||
$searchRequest->setFilter(['field' => 20]);
|
||||
|
||||
$query = $this->subject->create($connection, $searchRequest);
|
||||
$query = $this->subject->create($searchRequest);
|
||||
$this->assertSame(
|
||||
'10',
|
||||
$query->toArray()['query']['bool']['must'][0]['match']['_all'],
|
||||
|
@ -95,8 +113,36 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
|||
);
|
||||
$this->assertSame(
|
||||
'20',
|
||||
$query->toArray()['query']['bool']['filter']['term']['field'],
|
||||
$query->toArray()['query']['bool']['filter'][0]['term']['field'],
|
||||
'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.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue