Skip to content

Commit 837bf52

Browse files
authored
Merge pull request #55 from MacFJA/more-ut
Add more unit tests
2 parents 072371f + f87998a commit 837bf52

File tree

8 files changed

+286
-4
lines changed

8 files changed

+286
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010

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

1315
### Changed
1416

src/Redis/Initializer.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static function registerCommandsRediska(): void
139139
Rediska_Commands::add('__redisearch', RediskaRediSearchCommand::class);
140140
}
141141

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

154-
foreach ($modules as $module) {
154+
foreach ($modules as $moduleData) {
155155
$data = array_column(
156156
array_chunk(
157-
$module,
157+
$moduleData,
158158
2
159159
),
160160
1,
161161
0
162162
);
163163

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

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

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

tests/Redis/Command/AggregateTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
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
/**
31+
* @covers \MacFJA\RediSearch\Exception\UnexpectedServerResponseException
3032
* @covers \MacFJA\RediSearch\Redis\Command\AbstractCommand
3133
* @covers \MacFJA\RediSearch\Redis\Command\Aggregate
3234
* @covers \MacFJA\RediSearch\Redis\Command\AggregateCommand\ApplyOption
@@ -228,4 +230,11 @@ public function testApplyOrder(): void
228230
'WITHCURSOR', 'COUNT', 20, 'MAXIDLE', 30,
229231
], $command->getArguments());
230232
}
233+
234+
public function testResponseError(): void
235+
{
236+
$this->expectException(UnexpectedServerResponseException::class);
237+
$command = new Aggregate();
238+
$command->parseResponse(false);
239+
}
231240
}
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+
}
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

+28
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ public function testRediSearchVersion(array $input, ?string $expected): void
4646
static::assertSame($expected, Initializer::getRediSearchVersion($client));
4747
}
4848

49+
/**
50+
* @param array<string> $input
51+
*
52+
* @dataProvider dataProvider
53+
*/
54+
public function testRedisJSONVersion(array $input, ?string $expected): void
55+
{
56+
$client = $this->createMock(Client::class);
57+
$client->method('executeRaw')->with('module', 'list')->willReturn($input);
58+
59+
static::assertSame($expected, Initializer::getRedisJSONVersion($client));
60+
}
61+
4962
/**
5063
* @return Generator<array>
5164
*/
@@ -66,5 +79,20 @@ public function dataProvider(string $testName): Generator
6679

6780
yield [[['name', 'json', 'ver', '99999'], ['name', 'search', 'ver', '20005']], '2.0.5'];
6881
}
82+
if ('testRedisJSONVersion' === $testName) {
83+
yield [[['name', 'ReJSON', 'ver', '20005']], '2.0.5'];
84+
85+
yield [[['name', 'ReJSON', 'ver', '20000']], '2.0.0'];
86+
87+
yield [[['name', 'ReJSON', 'ver', '26512']], '2.65.12'];
88+
89+
yield [[['name', 'ReJSON', 'ver', '120569']], '12.5.69'];
90+
91+
yield [[['name', 'ReJSON', 'ver', '99999']], '9.99.99'];
92+
93+
yield [[['name', 'json', 'ver', '99999']], null];
94+
95+
yield [[['name', 'json', 'ver', '99999'], ['name', 'ReJSON', 'ver', '20005']], '2.0.5'];
96+
}
6997
}
7098
}
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+
}
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)