TASK: Add further tests and cast search input

* Map user input to string in any case.
* Add tests to check whether filter is added to query.
* Add test to check whether input is casted to string.
This commit is contained in:
Daniel Siepmann 2017-06-08 08:38:14 +02:00
parent f4a9531fe5
commit f453592b39
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 45 additions and 2 deletions

View file

@ -44,7 +44,7 @@ class SearchRequest implements SearchRequestInterface
*/
public function __construct($query)
{
$this->query = $query;
$this->query = (string) $query;
}
/**
@ -68,7 +68,7 @@ class SearchRequest implements SearchRequestInterface
*/
public function setFilter(array $filter)
{
$this->filter = $filter;
$this->filter = array_map('strval', $filter);
}
/**

View file

@ -53,4 +53,47 @@ class QueryFactoryTest extends AbstractUnitTestCase
'Factory did not create the expected instance.'
);
}
/**
* @test
*/
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);
$this->assertSame(
['field' => 'content'],
$query->toArray()['query']['bool']['filter']['term'],
'Filter was not added to query.'
);
}
/**
* @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);
$this->assertSame(
'10',
$query->toArray()['query']['bool']['must'][0]['match']['_all'],
'Search word was not escaped as expected.'
);
$this->assertSame(
'20',
$query->toArray()['query']['bool']['filter']['term']['field'],
'Search word was not escaped as expected.'
);
}
}