mirror of
https://github.com/Codappix/search_core.git
synced 2024-05-14 13:57:39 +02:00
149 lines
4.2 KiB
PHP
149 lines
4.2 KiB
PHP
<?php
|
|
namespace Codappix\SearchCore\Tests\Unit\Domain\Search;
|
|
|
|
/*
|
|
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
use Codappix\SearchCore\Domain\Model\FacetRequest;
|
|
use Codappix\SearchCore\Domain\Model\SearchRequest;
|
|
use Codappix\SearchCore\Domain\Search\QueryFactory;
|
|
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
|
|
|
|
class QueryFactoryTest extends AbstractUnitTestCase
|
|
{
|
|
/**
|
|
* @var QueryFactory
|
|
*/
|
|
protected $subject;
|
|
|
|
public function setUp()
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->subject = new QueryFactory($this->getMockedLogger());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function creatonOfQueryWorksInGeneral()
|
|
{
|
|
$searchRequest = new SearchRequest('SearchWord');
|
|
|
|
$query = $this->subject->create($searchRequest);
|
|
$this->assertInstanceOf(
|
|
\Elastica\Query::class,
|
|
$query,
|
|
'Factory did not create the expected instance.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function filterIsAddedToQuery()
|
|
{
|
|
$searchRequest = new SearchRequest('SearchWord');
|
|
$searchRequest->setFilter(['field' => 'content']);
|
|
|
|
$query = $this->subject->create($searchRequest);
|
|
$this->assertSame(
|
|
[
|
|
['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()
|
|
{
|
|
$searchRequest = new SearchRequest(10);
|
|
$searchRequest->setFilter(['field' => 20]);
|
|
|
|
$query = $this->subject->create($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'][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.'
|
|
);
|
|
}
|
|
}
|