mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-22 09:16:21 +01:00
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:
parent
f4a9531fe5
commit
f453592b39
2 changed files with 45 additions and 2 deletions
|
@ -44,7 +44,7 @@ class SearchRequest implements SearchRequestInterface
|
||||||
*/
|
*/
|
||||||
public function __construct($query)
|
public function __construct($query)
|
||||||
{
|
{
|
||||||
$this->query = $query;
|
$this->query = (string) $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,7 +68,7 @@ class SearchRequest implements SearchRequestInterface
|
||||||
*/
|
*/
|
||||||
public function setFilter(array $filter)
|
public function setFilter(array $filter)
|
||||||
{
|
{
|
||||||
$this->filter = $filter;
|
$this->filter = array_map('strval', $filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -53,4 +53,47 @@ class QueryFactoryTest extends AbstractUnitTestCase
|
||||||
'Factory did not create the expected instance.'
|
'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.'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue