diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index d0c154c1..b5a7a9a5 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -15,6 +15,7 @@ namespace Predis; use Predis\Command\Argument\Geospatial\ByInterface; use Predis\Command\Argument\Geospatial\FromInterface; use Predis\Command\Argument\Search\CreateArguments; +use Predis\Command\Argument\Search\DropArguments; use Predis\Command\Argument\Search\Schema; use Predis\Command\Argument\Search\SearchArguments; use Predis\Command\Argument\Server\LimitOffsetCount; @@ -91,6 +92,10 @@ use Predis\Command\Redis\Container\FUNCTIONS; * @method $this ftaliasdel(string $alias) * @method $this ftaliasupdate(string $alias, string $index) * @method $this ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) + * @method $this ftdictadd(string $dict, ...$term) + * @method $this ftdictdel(string $dict, ...$term) + * @method $this ftdictdump(string $dict) + * @method $this ftdropindex(string $index, ?DropArguments $arguments = null) * @method $this ftinfo(string $index) * @method $this ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method $this get($key) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 93dc47b3..eef472e7 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -15,6 +15,7 @@ namespace Predis; use Predis\Command\Argument\Geospatial\ByInterface; use Predis\Command\Argument\Geospatial\FromInterface; use Predis\Command\Argument\Search\CreateArguments; +use Predis\Command\Argument\Search\DropArguments; use Predis\Command\Argument\Search\Schema; use Predis\Command\Argument\Search\SearchArguments; use Predis\Command\Argument\Server\LimitOffsetCount; @@ -100,6 +101,10 @@ use Predis\Response\Status; * @method Status ftaliasdel(string $alias) * @method Status ftaliasupdate(string $alias, string $index) * @method Status ftcreate(string $index, Schema $schema, ?CreateArguments $arguments = null) + * @method int ftdictadd(string $dict, ...$term) + * @method int ftdictdel(string $dict, ...$term) + * @method array ftdictdump(string $dict) + * @method Status ftdropindex(string $index, ?DropArguments $arguments = null) * @method array ftinfo(string $index) * @method array ftsearch(string $index, string $query, ?SearchArguments $arguments = null) * @method string|null get(string $key) diff --git a/src/Command/Argument/Search/DropArguments.php b/src/Command/Argument/Search/DropArguments.php new file mode 100644 index 00000000..0c631320 --- /dev/null +++ b/src/Command/Argument/Search/DropArguments.php @@ -0,0 +1,43 @@ +arguments[] = 'DD'; + + return $this; + } + + /** + * @return array + */ + public function toArray(): array + { + return $this->arguments; + } +} diff --git a/src/Command/Redis/Search/FTDICTADD.php b/src/Command/Redis/Search/FTDICTADD.php new file mode 100644 index 00000000..c0bc3da7 --- /dev/null +++ b/src/Command/Redis/Search/FTDICTADD.php @@ -0,0 +1,28 @@ +toArray(); + } + + parent::setArguments(array_merge( + [$index], + $commandArguments + )); + } +} diff --git a/tests/Predis/Command/Argument/Search/DropArgumentsTest.php b/tests/Predis/Command/Argument/Search/DropArgumentsTest.php new file mode 100644 index 00000000..c843013a --- /dev/null +++ b/tests/Predis/Command/Argument/Search/DropArgumentsTest.php @@ -0,0 +1,38 @@ +arguments = new DropArguments(); + } + + /** + * @return void + */ + public function testCreatesArgumentsWithLanguageModifier(): void + { + $this->arguments->dd(); + + $this->assertSame(['DD'], $this->arguments->toArray()); + } +} diff --git a/tests/Predis/Command/Redis/Search/FTDICTADD_Test.php b/tests/Predis/Command/Redis/Search/FTDICTADD_Test.php new file mode 100644 index 00000000..4c8b44d5 --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FTDICTADD_Test.php @@ -0,0 +1,70 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testAddTermsIntoGivenDictionary(): void + { + $redis = $this->getClient(); + + $actualResponse = $redis->ftdictadd('dict', 'foo', 'bar'); + + $this->assertSame(2, $actualResponse); + } +} diff --git a/tests/Predis/Command/Redis/Search/FTDICTDEL_Test.php b/tests/Predis/Command/Redis/Search/FTDICTDEL_Test.php new file mode 100644 index 00000000..d85b0906 --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FTDICTDEL_Test.php @@ -0,0 +1,98 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @dataProvider dictionariesProvider + * @param array $addArguments + * @param array $deleteArguments + * @param int $expectedResponse + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testRemovesTermsFromGivenDictionary( + array $addArguments, + array $deleteArguments, + int $expectedResponse + ): void { + $redis = $this->getClient(); + + $redis->ftdictadd(...$addArguments); + + $this->assertSame($expectedResponse, $redis->ftdictdel(...$deleteArguments)); + } + + public function dictionariesProvider(): array + { + return [ + 'removes existing term' => [ + ['dict', 'foo', 'bar'], + ['dict', 'foo'], + 1, + ], + 'removes non-existing term' => [ + ['dict', 'foo', 'bar'], + ['dict', 'baz'], + 0, + ], + 'removes from non-existing dict' => [ + ['dict', 'foo', 'bar'], + ['dict123', 'baz'], + 0, + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/Search/FTDICTDUMP_Test.php b/tests/Predis/Command/Redis/Search/FTDICTDUMP_Test.php new file mode 100644 index 00000000..39231ff4 --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FTDICTDUMP_Test.php @@ -0,0 +1,86 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testDumpTermsFromGivenDictionary(): void + { + $redis = $this->getClient(); + + $redis->ftdictadd('dict', 'foo', 'bar'); + + $this->assertSame(['bar', 'foo'], $redis->ftdictdump('dict')); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 1.4.0 + */ + public function testThrowsExceptionOnNonExistingDictionary(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('could not open dict key'); + + $redis->ftdictdump('dict'); + } +} diff --git a/tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php b/tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php new file mode 100644 index 00000000..a0e2032c --- /dev/null +++ b/tests/Predis/Command/Redis/Search/FTDROPINDEX_Test.php @@ -0,0 +1,102 @@ +getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 2.0.0 + */ + public function testDropRemovesGivenIndex(): void + { + $redis = $this->getClient(); + + $schema = new Schema(); + $schema->addTextField('text_field'); + + $this->assertEquals('OK', $redis->ftcreate('index', $schema)); + $this->assertEquals('OK', $redis->ftdropindex('index')); + } + + /** + * @group connected + * @return void + * @requiresRediSearchVersion >= 2.0.0 + */ + public function testThrowsExceptionOnNonExistingIndex(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('Unknown Index name'); + + $redis->ftdropindex('index'); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + ['index'], + ['index'], + ], + 'with DD modifier' => [ + ['index', (new DropArguments())->dd()], + ['index', 'DD'], + ], + ]; + } +}