Skip to content

Add more unit tests #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Path validation for JSON fields
- Helper function get current RedisJSON version + (abstraction)
- (dev) More unit tests

### Changed

Expand Down
18 changes: 14 additions & 4 deletions src/Redis/Initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function registerCommandsRediska(): void
Rediska_Commands::add('__redisearch', RediskaRediSearchCommand::class);
}

public static function getRediSearchVersion(Client $client): ?string
public static function getRedisModuleVersion(Client $client, string $module): ?string
{
try {
$modules = $client->executeRaw('module', 'list') ?? [];
Expand All @@ -151,17 +151,17 @@ public static function getRediSearchVersion(Client $client): ?string
return null;
}

foreach ($modules as $module) {
foreach ($modules as $moduleData) {
$data = array_column(
array_chunk(
$module,
$moduleData,
2
),
1,
0
);

if (!(($data['name'] ?? '') === 'search') || empty($data['ver'])) {
if (!(($data['name'] ?? '') === $module) || empty($data['ver'])) {
continue;
}

Expand All @@ -179,4 +179,14 @@ public static function getRediSearchVersion(Client $client): ?string

return null;
}

public static function getRediSearchVersion(Client $client): ?string
{
return self::getRedisModuleVersion($client, 'search');
}

public static function getRedisJSONVersion(Client $client): ?string
{
return self::getRedisModuleVersion($client, 'ReJSON');
}
}
9 changes: 9 additions & 0 deletions tests/Redis/Command/AggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

namespace MacFJA\RediSearch\tests\Redis\Command;

use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
use MacFJA\RediSearch\Redis\Command\Aggregate;
use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
use PHPUnit\Framework\TestCase;

/**
* @covers \MacFJA\RediSearch\Exception\UnexpectedServerResponseException
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
* @covers \MacFJA\RediSearch\Redis\Command\Aggregate
* @covers \MacFJA\RediSearch\Redis\Command\AggregateCommand\ApplyOption
Expand Down Expand Up @@ -228,4 +230,11 @@ public function testApplyOrder(): void
'WITHCURSOR', 'COUNT', 20, 'MAXIDLE', 30,
], $command->getArguments());
}

public function testResponseError(): void
{
$this->expectException(UnexpectedServerResponseException::class);
$command = new Aggregate();
$command->parseResponse(false);
}
}
80 changes: 80 additions & 0 deletions tests/Redis/Command/Option/WithPublicGroupedSetterTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\tests\Redis\Command\Option;

use BadMethodCallException;
use InvalidArgumentException;
use MacFJA\RediSearch\Redis\Command\Option\NamelessOption;
use MacFJA\RediSearch\tests\fixtures\Redis\Command\Option\FakeWithPublicGroupedSetter;
use MacFJA\RediSearch\tests\fixtures\Redis\Command\Option\FakeWithPublicGroupedSetterInvalidSelf;
use PHPUnit\Framework\TestCase;

/**
* @covers \MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait
*
* @uses \MacFJA\RediSearch\Redis\Command\Option\GroupedOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamelessOption
*
* @internal
*/
class WithPublicGroupedSetterTraitTest extends TestCase
{
public function testInvalidSelf(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('This method is not callable in '.FakeWithPublicGroupedSetterInvalidSelf::class);
$class = new FakeWithPublicGroupedSetterInvalidSelf();
$class->setName('John');
}

public function testInvalidSetterName(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call undefined method setAge in '.FakeWithPublicGroupedSetter::class);
$class = new FakeWithPublicGroupedSetter([], []);
$class->setAge(50);
}

public function testInvalidSetterArgCount(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The method '.FakeWithPublicGroupedSetter::class.'::setName need exactly one argument');
$class = new FakeWithPublicGroupedSetter([], []);
$class->setName('John', 'Doe');
}

public function testUndefinedMethod(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call undefined method foo in '.FakeWithPublicGroupedSetter::class);
$class = new FakeWithPublicGroupedSetter([], []);
$class->foo();
}

public function testSetter(): void
{
$class = new FakeWithPublicGroupedSetter(['name' => new NamelessOption()], []);
$class->setName('John');
static::assertSame('John', $class->getDataOfOption('name'));
}
}
70 changes: 70 additions & 0 deletions tests/Redis/Command/ProfileCommand/QueryOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\tests\Redis\Command\ProfileCommand;

use MacFJA\RediSearch\Redis\Command\Aggregate;
use MacFJA\RediSearch\Redis\Command\ProfileCommand\QueryOption;
use MacFJA\RediSearch\Redis\Command\Search;
use PHPUnit\Framework\TestCase;

/**
* @covers \MacFJA\RediSearch\Redis\Command\ProfileCommand\QueryOption
*
* @uses \MacFJA\RediSearch\Redis\Command\Search
* @uses \MacFJA\RediSearch\Redis\Command\Aggregate
* @uses \MacFJA\RediSearch\Redis\Command\AbstractCommand
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\DecoratedOptionTrait
* @uses \MacFJA\RediSearch\Redis\Command\Option\FlagOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\GroupedOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamedOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamelessOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NotEmptyOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NumberedOption
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\HighlightOption
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\LimitOption
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\SortByOption
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\SummarizeOption
* @uses \MacFJA\RediSearch\Redis\Command\AggregateCommand\WithCursor
*
* @internal
*/
class QueryOptionTest extends TestCase
{
public function testGetOptionData(): void
{
$command = (new Search())->setIndex('idx')->setQuery('*');
static::assertSame([
'command' => $command,
], (new QueryOption())->setCommand($command)->getOptionData());
}

public function testIsSearch(): void
{
$search = (new Search())->setIndex('idx')->setQuery('*');
$aggregate = (new Aggregate())->setIndex('idx')->setQuery('*');
static::assertTrue((new QueryOption())->setCommand($search)->isSearch());
static::assertFalse((new QueryOption())->setCommand($aggregate)->isSearch());
}
}
28 changes: 28 additions & 0 deletions tests/Redis/InitializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public function testRediSearchVersion(array $input, ?string $expected): void
static::assertSame($expected, Initializer::getRediSearchVersion($client));
}

/**
* @param array<string> $input
*
* @dataProvider dataProvider
*/
public function testRedisJSONVersion(array $input, ?string $expected): void
{
$client = $this->createMock(Client::class);
$client->method('executeRaw')->with('module', 'list')->willReturn($input);

static::assertSame($expected, Initializer::getRedisJSONVersion($client));
}

/**
* @return Generator<array>
*/
Expand All @@ -66,5 +79,20 @@ public function dataProvider(string $testName): Generator

yield [[['name', 'json', 'ver', '99999'], ['name', 'search', 'ver', '20005']], '2.0.5'];
}
if ('testRedisJSONVersion' === $testName) {
yield [[['name', 'ReJSON', 'ver', '20005']], '2.0.5'];

yield [[['name', 'ReJSON', 'ver', '20000']], '2.0.0'];

yield [[['name', 'ReJSON', 'ver', '26512']], '2.65.12'];

yield [[['name', 'ReJSON', 'ver', '120569']], '12.5.69'];

yield [[['name', 'ReJSON', 'ver', '99999']], '9.99.99'];

yield [[['name', 'json', 'ver', '99999']], null];

yield [[['name', 'json', 'ver', '99999'], ['name', 'ReJSON', 'ver', '20005']], '2.0.5'];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\tests\fixtures\Redis\Command\Option;

use MacFJA\RediSearch\Redis\Command\Option\GroupedOption;
use MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait;

/**
* @method void setName(string $name, $arg = null)
* @method void setAge(int $age) Fake setter to please PHPStan
* @method void foo() Fake method to please PHPStan
*/
class FakeWithPublicGroupedSetter extends GroupedOption
{
use WithPublicGroupedSetterTrait;

/**
* @return string[]
*/
protected function publicSetter(): array
{
return ['name'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\tests\fixtures\Redis\Command\Option;

use MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait;

/**
* @method void setName(string $name)
*/
class FakeWithPublicGroupedSetterInvalidSelf
{
use WithPublicGroupedSetterTrait;

/**
* @return array<string>
*/
protected function publicSetter(): array
{
return [];
}
}