mirror of
https://github.com/predis/predis.git
synced 2026-08-25 10:49:41 +00:00
5a9f62ef78
* Added support for new arguments for BITPOS, BITCOUNT commands (#1045) * Added support for new arguments for EXPIRE, EXPIREAT commands (#1046) * Extended core support by implementing SORT_RO command (#1044) * Added support for SORT_RO command * Codestyle fixes * Added command description --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> * fix deprecated call * Added support for container commands (#1049) * Added support for container commands FUNCTION LOAD, FUNCTION DELETE and FCALL * Changed ContainerInterface and AbstractContainer * Re-implement logic of abstract methods --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> * Added stream commands to KeyPrefixProcessor (#1051) Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> * Fix return type of ReplicationInterface::getSlaves (#1111) * Codestyle fixes * Changed return annotation * Added support for FT.SUGGET command, added examples for suggestion API usage * Include SUGGET command files to codespell ignore * Added file extension --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> Co-authored-by: Till Krüss <till@kruss.io> Co-authored-by: Stephan <glaubinix@users.noreply.github.com>
338 lines
9.2 KiB
PHP
338 lines
9.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) 2009-2020 Daniele Alessandri
|
|
* (c) 2021-2023 Till Krüss
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Command\Argument\Search;
|
|
|
|
use InvalidArgumentException;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class SearchArgumentsTest extends TestCase
|
|
{
|
|
/**
|
|
* @var SearchArguments
|
|
*/
|
|
private $arguments;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->arguments = new SearchArguments();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithNoContentModifier(): void
|
|
{
|
|
$this->arguments->noContent();
|
|
|
|
$this->assertSame(['NOCONTENT'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithVerbatimModifier(): void
|
|
{
|
|
$this->arguments->verbatim();
|
|
|
|
$this->assertSame(['VERBATIM'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithWithSortKeysModifier(): void
|
|
{
|
|
$this->arguments->withSortKeys();
|
|
|
|
$this->assertSame(['WITHSORTKEYS'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithSearchFilterModifier(): void
|
|
{
|
|
$this->arguments->searchFilter(['numeric_field', 1, 10], ['numeric_field1', 2, 5]);
|
|
|
|
$this->assertSame(
|
|
['FILTER', 'numeric_field', 1, 10, 'FILTER', 'numeric_field1', 2, 5],
|
|
$this->arguments->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithGeoFilterModifier(): void
|
|
{
|
|
$this->arguments->geoFilter(
|
|
['geo_field', 13.2321, 14.2321, 300, 'km'],
|
|
['geo_field1', 15.231, 16.234, 210, 'km']
|
|
);
|
|
|
|
$this->assertSame(
|
|
['GEOFILTER', 'geo_field', 13.2321, 14.2321, 300, 'km', 'GEOFILTER', 'geo_field1', 15.231, 16.234, 210, 'km'],
|
|
$this->arguments->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithInKeysModifier(): void
|
|
{
|
|
$this->arguments->inKeys(['key1', 'key2']);
|
|
|
|
$this->assertSame(['INKEYS', 2, 'key1', 'key2'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithInFieldsModifier(): void
|
|
{
|
|
$this->arguments->inFields(['field1', 'field2']);
|
|
|
|
$this->assertSame(['INFIELDS', 2, 'field1', 'field2'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithReturnModifier(): void
|
|
{
|
|
$this->arguments->addReturn(2, 'identifier', true, 'property', 'identifier2', 'identifier3');
|
|
|
|
$this->assertSame(
|
|
['RETURN', 2, 'identifier', 'AS', 'property', 'identifier2', 'identifier3'],
|
|
$this->arguments->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider summarizeProvider
|
|
* @param array $arguments
|
|
* @param array $expectedResponse
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithSummarizeModifier(array $arguments, array $expectedResponse): void
|
|
{
|
|
$this->arguments->summarize(...$arguments);
|
|
|
|
$this->assertSame($expectedResponse, $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider highlightProvider
|
|
* @param array $arguments
|
|
* @param array $expectedResponse
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithHighlightModifier(array $arguments, array $expectedResponse): void
|
|
{
|
|
$this->arguments->highlight(...$arguments);
|
|
|
|
$this->assertSame($expectedResponse, $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithSlopModifier(): void
|
|
{
|
|
$this->arguments->slop(2);
|
|
|
|
$this->assertSame(['SLOP', 2], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithTimeoutModifier(): void
|
|
{
|
|
$this->arguments->timeout(2);
|
|
|
|
$this->assertSame(['TIMEOUT', 2], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithInOrderModifier(): void
|
|
{
|
|
$this->arguments->inOrder();
|
|
|
|
$this->assertSame(['INORDER'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithExpanderModifier(): void
|
|
{
|
|
$this->arguments->expander('expander');
|
|
|
|
$this->assertSame(['EXPANDER', 'expander'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithScorerModifier(): void
|
|
{
|
|
$this->arguments->scorer('scorer');
|
|
|
|
$this->assertSame(['SCORER', 'scorer'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsExplainScoreModifier(): void
|
|
{
|
|
$this->arguments->explainScore();
|
|
|
|
$this->assertSame(['EXPLAINSCORE'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider sortByProvider
|
|
* @param array $arguments
|
|
* @param array $expectedResponse
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithSortByModifier(array $arguments, array $expectedResponse): void
|
|
{
|
|
$this->arguments->sortBy(...$arguments);
|
|
|
|
$this->assertSame($expectedResponse, $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testThrowsExceptionOnSortByWrongOrderByModifier(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Wrong order direction value given. Currently supports: ASC, DESC');
|
|
|
|
$this->arguments->sortBy('sort_attribute', 'wrong');
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithLimitModifier(): void
|
|
{
|
|
$this->arguments->limit(2, 2);
|
|
|
|
$this->assertSame(['LIMIT', 2, 2], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesArgumentsWithParamsModifier(): void
|
|
{
|
|
$this->arguments->params(['name1', 'value1', 'name2', 'value2']);
|
|
|
|
$this->assertSame(['PARAMS', 4, 'name1', 'value1', 'name2', 'value2'], $this->arguments->toArray());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCreatesCorrectFTSearchArgumentsSetOnMethodsChainCall(): void
|
|
{
|
|
$this->arguments->withScores();
|
|
$this->arguments->withPayloads();
|
|
$this->arguments->searchFilter(['numeric_field', 1, 10]);
|
|
$this->arguments->addReturn(2, 'identifier', true, 'property');
|
|
|
|
$this->assertSame(
|
|
['WITHSCORES', 'WITHPAYLOADS', 'FILTER', 'numeric_field', 1, 10, 'RETURN', 2, 'identifier', 'AS', 'property'],
|
|
$this->arguments->toArray()
|
|
);
|
|
}
|
|
|
|
public function sortByProvider(): array
|
|
{
|
|
return [
|
|
'with default arguments' => [
|
|
['sort_attribute'],
|
|
['SORTBY', 'sort_attribute', 'ASC'],
|
|
],
|
|
'with DESC modifier' => [
|
|
['sort_attribute', 'desc'],
|
|
['SORTBY', 'sort_attribute', 'DESC'],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function summarizeProvider(): array
|
|
{
|
|
return [
|
|
'with no arguments' => [
|
|
[],
|
|
['SUMMARIZE'],
|
|
],
|
|
'with fields only' => [
|
|
[['field1', 'field2']],
|
|
['SUMMARIZE', 'FIELDS', 2, 'field1', 'field2'],
|
|
],
|
|
'with non-default FRAGS' => [
|
|
[['field1', 'field2'], 2],
|
|
['SUMMARIZE', 'FIELDS', 2, 'field1', 'field2', 'FRAGS', 2],
|
|
],
|
|
'with non-default LEN' => [
|
|
[['field1', 'field2'], 0, 2],
|
|
['SUMMARIZE', 'FIELDS', 2, 'field1', 'field2', 'LEN', 2],
|
|
],
|
|
'with non-default SEPARATOR' => [
|
|
[['field1', 'field2'], 0, 0, ','],
|
|
['SUMMARIZE', 'FIELDS', 2, 'field1', 'field2', 'SEPARATOR', ','],
|
|
],
|
|
'with all arguments' => [
|
|
[['field1', 'field2'], 2, 2, ','],
|
|
['SUMMARIZE', 'FIELDS', 2, 'field1', 'field2', 'FRAGS', 2, 'LEN', 2, 'SEPARATOR', ','],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function highlightProvider(): array
|
|
{
|
|
return [
|
|
'with no arguments' => [
|
|
[],
|
|
['HIGHLIGHT'],
|
|
],
|
|
'with fields only' => [
|
|
[['field1', 'field2']],
|
|
['HIGHLIGHT', 'FIELDS', 2, 'field1', 'field2'],
|
|
],
|
|
'with open tag only' => [
|
|
[['field1', 'field2'], 'openTag'],
|
|
['HIGHLIGHT', 'FIELDS', 2, 'field1', 'field2'],
|
|
],
|
|
'with close tag only' => [
|
|
[['field1', 'field2'], '', 'closeTag'],
|
|
['HIGHLIGHT', 'FIELDS', 2, 'field1', 'field2'],
|
|
],
|
|
'with both tags' => [
|
|
[['field1', 'field2'], 'openTag', 'closeTag'],
|
|
['HIGHLIGHT', 'FIELDS', 2, 'field1', 'field2', 'TAGS', 'openTag', 'closeTag'],
|
|
],
|
|
];
|
|
}
|
|
}
|