Skip to content

Commit fd955a6

Browse files
committed
Add more unit tests
1 parent ec9dec1 commit fd955a6

File tree

8 files changed

+288
-5
lines changed

8 files changed

+288
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased v2.x]
88

9+
### Added
10+
11+
- (dev) More unit tests
12+
913
## [2.2.0]
1014

1115
### Added

src/Redis/Initializer.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static function registerCommandsRediska(): void
138138
Rediska_Commands::add('__redisearch', RediskaRediSearchCommand::class);
139139
}
140140

141-
public static function getRediSearchVersion(Client $client): ?string
141+
public static function getRedisModuleVersion(Client $client, string $module): ?string
142142
{
143143
try {
144144
$modules = $client->executeRaw('module', 'list') ?? [];
@@ -150,17 +150,17 @@ public static function getRediSearchVersion(Client $client): ?string
150150
return null;
151151
}
152152

153-
foreach ($modules as $module) {
153+
foreach ($modules as $moduleData) {
154154
$data = array_column(
155155
array_chunk(
156-
$module,
156+
$moduleData,
157157
2
158158
),
159159
1,
160160
0
161161
);
162162

163-
if (!(($data['name'] ?? '') === 'search') || empty($data['ver'])) {
163+
if (!(($data['name'] ?? '') === $module) || empty($data['ver'])) {
164164
continue;
165165
}
166166

@@ -178,4 +178,14 @@ public static function getRediSearchVersion(Client $client): ?string
178178

179179
return null;
180180
}
181+
182+
public static function getRediSearchVersion(Client $client): ?string
183+
{
184+
return self::getRedisModuleVersion($client, 'search');
185+
}
186+
187+
public static function getRedisJSONVersion(Client $client): ?string
188+
{
189+
return self::getRedisModuleVersion($client, 'ReJSON');
190+
}
181191
}

tests/Redis/Command/AggregateTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121

2222
namespace MacFJA\RediSearch\tests\Redis\Command;
2323

24+
use MacFJA\RediSearch\Exception\UnexpectedServerResponseException;
2425
use MacFJA\RediSearch\Redis\Command\Aggregate;
2526
use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
2627
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
2728
use PHPUnit\Framework\TestCase;
2829

2930
/**
30-
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
31+
* @covers \MacFJA\RediSearch\Exception\UnexpectedServerResponseException
3132
*
33+
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
3234
* @covers \MacFJA\RediSearch\Redis\Command\Aggregate
3335
* @covers \MacFJA\RediSearch\Redis\Command\AggregateCommand\ApplyOption
3436
* @covers \MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption
@@ -230,4 +232,11 @@ public function testApplyOrder(): void
230232
'WITHCURSOR', 'COUNT', 20, 'MAXIDLE', 30,
231233
], $command->getArguments());
232234
}
235+
236+
public function testResponseError(): void
237+
{
238+
$this->expectException(UnexpectedServerResponseException::class);
239+
$command = new Aggregate();
240+
$command->parseResponse(false);
241+
}
233242
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\Redis\Command\Option;
23+
24+
use BadMethodCallException;
25+
use InvalidArgumentException;
26+
use MacFJA\RediSearch\Redis\Command\Option\NamelessOption;
27+
use MacFJA\RediSearch\tests\fixtures\Redis\Command\Option\FakeWithPublicGroupedSetter;
28+
use MacFJA\RediSearch\tests\fixtures\Redis\Command\Option\FakeWithPublicGroupedSetterInvalidSelf;
29+
use PHPUnit\Framework\TestCase;
30+
31+
/**
32+
* @covers \MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait
33+
*
34+
* @uses \MacFJA\RediSearch\Redis\Command\Option\GroupedOption
35+
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
36+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamelessOption
37+
*
38+
* @internal
39+
*/
40+
class WithPublicGroupedSetterTraitTest extends TestCase
41+
{
42+
public function testInvalidSelf(): void
43+
{
44+
$this->expectException(BadMethodCallException::class);
45+
$this->expectExceptionMessage('This method is not callable in '.FakeWithPublicGroupedSetterInvalidSelf::class);
46+
$class = new FakeWithPublicGroupedSetterInvalidSelf();
47+
$class->setName('John');
48+
}
49+
50+
public function testInvalidSetterName(): void
51+
{
52+
$this->expectException(BadMethodCallException::class);
53+
$this->expectExceptionMessage('Call undefined method setAge in '.FakeWithPublicGroupedSetter::class);
54+
$class = new FakeWithPublicGroupedSetter([], []);
55+
$class->setAge(50);
56+
}
57+
58+
public function testInvalidSetterArgCount(): void
59+
{
60+
$this->expectException(InvalidArgumentException::class);
61+
$this->expectExceptionMessage('The method '.FakeWithPublicGroupedSetter::class.'::setName need exactly one argument');
62+
$class = new FakeWithPublicGroupedSetter([], []);
63+
$class->setName('John', 'Doe');
64+
}
65+
66+
public function testUndefinedMethod(): void
67+
{
68+
$this->expectException(BadMethodCallException::class);
69+
$this->expectExceptionMessage('Call undefined method foo in '.FakeWithPublicGroupedSetter::class);
70+
$class = new FakeWithPublicGroupedSetter([], []);
71+
$class->foo();
72+
}
73+
74+
public function testSetter(): void
75+
{
76+
$class = new FakeWithPublicGroupedSetter(['name' => new NamelessOption()], []);
77+
$class->setName('John');
78+
static::assertSame('John', $class->getDataOfOption('name'));
79+
}
80+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\Redis\Command\ProfileCommand;
23+
24+
use MacFJA\RediSearch\Redis\Command\Aggregate;
25+
use MacFJA\RediSearch\Redis\Command\ProfileCommand\QueryOption;
26+
use MacFJA\RediSearch\Redis\Command\Search;
27+
use PHPUnit\Framework\TestCase;
28+
29+
/**
30+
* @covers \MacFJA\RediSearch\Redis\Command\ProfileCommand\QueryOption
31+
*
32+
* @uses \MacFJA\RediSearch\Redis\Command\Search
33+
* @uses \MacFJA\RediSearch\Redis\Command\Aggregate
34+
* @uses \MacFJA\RediSearch\Redis\Command\AbstractCommand
35+
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
36+
* @uses \MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption
37+
* @uses \MacFJA\RediSearch\Redis\Command\Option\DecoratedOptionTrait
38+
* @uses \MacFJA\RediSearch\Redis\Command\Option\FlagOption
39+
* @uses \MacFJA\RediSearch\Redis\Command\Option\GroupedOption
40+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamedOption
41+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamelessOption
42+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NotEmptyOption
43+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NumberedOption
44+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption
45+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\HighlightOption
46+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\LimitOption
47+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\SortByOption
48+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\SummarizeOption
49+
* @uses \MacFJA\RediSearch\Redis\Command\AggregateCommand\WithCursor
50+
*
51+
* @internal
52+
*/
53+
class QueryOptionTest extends TestCase
54+
{
55+
public function testGetOptionData(): void
56+
{
57+
$command = (new Search())->setIndex('idx')->setQuery('*');
58+
static::assertSame([
59+
'command' => $command,
60+
], (new QueryOption())->setCommand($command)->getOptionData());
61+
}
62+
63+
public function testIsSearch(): void
64+
{
65+
$search = (new Search())->setIndex('idx')->setQuery('*');
66+
$aggregate = (new Aggregate())->setIndex('idx')->setQuery('*');
67+
static::assertTrue((new QueryOption())->setCommand($search)->isSearch());
68+
static::assertFalse((new QueryOption())->setCommand($aggregate)->isSearch());
69+
}
70+
}

tests/Redis/InitializerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public function testRediSearchVersion(array $input, ?string $expected): void
4444
static::assertSame($expected, Initializer::getRediSearchVersion($client));
4545
}
4646

47+
/**
48+
* @param array<string> $input
49+
* @dataProvider dataProvider
50+
*/
51+
public function testRedisJSONVersion(array $input, ?string $expected): void
52+
{
53+
$client = $this->createMock(Client::class);
54+
$client->method('executeRaw')->with('module', 'list')->willReturn($input);
55+
56+
static::assertSame($expected, Initializer::getRedisJSONVersion($client));
57+
}
58+
4759
/**
4860
* @return Generator<array>
4961
*/
@@ -64,5 +76,20 @@ public function dataProvider(string $testName): Generator
6476

6577
yield [[['name', 'json', 'ver', '99999'], ['name', 'search', 'ver', '20005']], '2.0.5'];
6678
}
79+
if ('testRedisJSONVersion' === $testName) {
80+
yield [[['name', 'ReJSON', 'ver', '20005']], '2.0.5'];
81+
82+
yield [[['name', 'ReJSON', 'ver', '20000']], '2.0.0'];
83+
84+
yield [[['name', 'ReJSON', 'ver', '26512']], '2.65.12'];
85+
86+
yield [[['name', 'ReJSON', 'ver', '120569']], '12.5.69'];
87+
88+
yield [[['name', 'ReJSON', 'ver', '99999']], '9.99.99'];
89+
90+
yield [[['name', 'json', 'ver', '99999']], null];
91+
92+
yield [[['name', 'json', 'ver', '99999'], ['name', 'ReJSON', 'ver', '20005']], '2.0.5'];
93+
}
6794
}
6895
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\fixtures\Redis\Command\Option;
23+
24+
use MacFJA\RediSearch\Redis\Command\Option\GroupedOption;
25+
use MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait;
26+
27+
/**
28+
* @method void setName(string $name, $arg = null)
29+
* @method void setAge(int $age) Fake setter to please PHPStan
30+
* @method void foo() Fake method to please PHPStan
31+
*/
32+
class FakeWithPublicGroupedSetter extends GroupedOption
33+
{
34+
use WithPublicGroupedSetterTrait;
35+
36+
/**
37+
* @return string[]
38+
*/
39+
protected function publicSetter(): array
40+
{
41+
return ['name'];
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\fixtures\Redis\Command\Option;
23+
24+
use MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait;
25+
26+
/**
27+
* @method void setName(string $name)
28+
*/
29+
class FakeWithPublicGroupedSetterInvalidSelf
30+
{
31+
use WithPublicGroupedSetterTrait;
32+
33+
/**
34+
* @return array<string>
35+
*/
36+
protected function publicSetter(): array
37+
{
38+
return [];
39+
}
40+
}

0 commit comments

Comments
 (0)