Skip to content

Commit 764fd38

Browse files
committed
Add integration tests
1 parent a681216 commit 764fd38

File tree

6 files changed

+254
-7
lines changed

6 files changed

+254
-7
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: analyze fix-code test coverage mutation-test validation
1+
.PHONY: analyze fix-code test coverage validation integration-test
22

33
analyze: | vendor
44
$(COMPOSER) exec -v parallel-lint -- src
@@ -22,7 +22,10 @@ coverage: | vendor
2222
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
2323
$(COMPOSER) exec -v phpunit -- --coverage-text --color
2424

25-
validation: fix-code analyze test coverage
25+
integration-test: | vendor
26+
$(COMPOSER) exec -v phpunit -- --group integration
27+
28+
validation: fix-code analyze test coverage integration-test
2629

2730
vendor: composer.json
2831
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist

phpunit.xml.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@
1111
<testsuites>
1212
<testsuite name="default">
1313
<directory suffix="Test.php">tests</directory>
14+
<exclude>tests/integration</exclude>
15+
</testsuite>
16+
<testsuite name="integration">
17+
<directory suffix="Test.php">tests/integration</directory>
1418
</testsuite>
1519
</testsuites>
1620

21+
<groups>
22+
<exclude>
23+
<group>integration</group>
24+
</exclude>
25+
</groups>
26+
1727
<filter>
1828
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
1929
<directory suffix=".php">src</directory>

src/Redis/Command/Aggregate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace MacFJA\RediSearch\Redis\Command;
2323

2424
use function assert;
25-
use function is_int;
2625
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ApplyOption;
2726
use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
2827
use MacFJA\RediSearch\Redis\Command\AggregateCommand\LimitOption;
@@ -198,7 +197,8 @@ protected function transformParsedResponse($data)
198197
}
199198

200199
$totalCount = array_shift($data);
201-
assert(is_int($totalCount));
200+
assert(is_numeric($totalCount));
201+
$totalCount = (int) $totalCount;
202202

203203
$items = array_map(static function (array $document) {
204204
return new AggregateResponseItem(self::getPairs($document));

src/Redis/Command/Search.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use function count;
2626
use InvalidArgumentException;
2727
use function is_array;
28-
use function is_int;
2928
use MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption as CV;
3029
use MacFJA\RediSearch\Redis\Command\Option\FlagOption;
3130
use MacFJA\RediSearch\Redis\Command\Option\NamedOption;
@@ -315,7 +314,8 @@ public function transformParsedResponse($data)
315314
}
316315

317316
$totalCount = array_shift($data);
318-
assert(is_int($totalCount));
317+
assert(is_numeric($totalCount));
318+
$totalCount = (int) $totalCount;
319319

320320
$useScores = $this->options['withscores']->isActive();
321321
$usePayloads = $this->options['withpayloads']->isActive();

src/Redis/Response/PaginatedResponse.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace MacFJA\RediSearch\Redis\Response;
2323

2424
use function count;
25+
use Countable;
2526
use function is_int;
2627
use Iterator;
2728
use MacFJA\RediSearch\Redis\Client;
@@ -31,7 +32,7 @@
3132
/**
3233
* @implements Iterator<int,AggregateResponseItem[]|SearchResponseItem[]>
3334
*/
34-
class PaginatedResponse implements Response, Iterator
35+
class PaginatedResponse implements Response, Iterator, Countable
3536
{
3637
/** @var array<AggregateResponseItem>|array<SearchResponseItem> */
3738
private $items;
@@ -125,6 +126,11 @@ public function getTotalCount(): int
125126
return $this->totalCount;
126127
}
127128

129+
public function count()
130+
{
131+
return $this->totalCount;
132+
}
133+
128134
private function updateWithLimit(int $offset, int $size): void
129135
{
130136
/** @var PaginatedCommand $nextCommand */

tests/integration/DockerTest.php

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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\integration;
23+
24+
use Amp\Redis\Config;
25+
use Amp\Redis\RemoteExecutor;
26+
use Closure;
27+
use MacFJA\RediSearch\Index;
28+
use MacFJA\RediSearch\IndexBuilder;
29+
use MacFJA\RediSearch\Query\Builder;
30+
use MacFJA\RediSearch\Query\Builder\Fuzzy;
31+
use MacFJA\RediSearch\Redis\Client;
32+
use MacFJA\RediSearch\Redis\Command\Aggregate;
33+
use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
34+
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
35+
use MacFJA\RediSearch\Redis\Command\DropIndex;
36+
use MacFJA\RediSearch\Redis\Command\IndexList;
37+
use MacFJA\RediSearch\Redis\Command\Search;
38+
use MacFJA\RediSearch\Redis\Command\SynDump;
39+
use MacFJA\RediSearch\Redis\Command\SynUpdate;
40+
use MacFJA\RediSearch\Redis\Response\PaginatedResponse;
41+
use PHPUnit\Framework\TestCase;
42+
use PHPUnit\Framework\TestResult;
43+
use Rediska;
44+
45+
/**
46+
* @covers \MacFJA\RediSearch\Index
47+
* @covers \MacFJA\RediSearch\IndexBuilder
48+
* @covers \MacFJA\RediSearch\Query\Builder
49+
* @covers \MacFJA\RediSearch\Redis\Client\AbstractClient
50+
* @covers \MacFJA\RediSearch\Redis\Client\AmpRedisClient
51+
* @covers \MacFJA\RediSearch\Redis\Client\CheprasovRedisClient
52+
* @covers \MacFJA\RediSearch\Redis\Client\PredisClient
53+
* @covers \MacFJA\RediSearch\Redis\Client\RedisentClient
54+
* @covers \MacFJA\RediSearch\Redis\Client\RediskaClient
55+
* @covers \MacFJA\RediSearch\Redis\Command\Aggregate
56+
* @covers \MacFJA\RediSearch\Redis\Command\DropIndex
57+
* @covers \MacFJA\RediSearch\Redis\Command\IndexList
58+
* @covers \MacFJA\RediSearch\Redis\Command\Info
59+
* @covers \MacFJA\RediSearch\Redis\Command\Search
60+
* @covers \MacFJA\RediSearch\Redis\Command\SynDump
61+
* @covers \MacFJA\RediSearch\Redis\Command\SynUpdate
62+
* @covers \MacFJA\RediSearch\Redis\Initializer
63+
* @covers \MacFJA\RediSearch\Redis\RediskaRediSearch
64+
* @covers \MacFJA\RediSearch\Redis\Response\InfoResponse
65+
* @covers \MacFJA\RediSearch\Redis\Response\PaginatedResponse
66+
*
67+
* @uses \MacFJA\RediSearch\Query\Builder\AbstractGroup
68+
* @uses \MacFJA\RediSearch\Query\Builder\AndGroup
69+
* @uses \MacFJA\RediSearch\Redis\Command\AbstractCommand
70+
* @uses \MacFJA\RediSearch\Redis\Command\AddFieldOptionTrait
71+
* @uses \MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption
72+
* @uses \MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption
73+
* @uses \MacFJA\RediSearch\Redis\Command\AggregateCommand\WithCursor
74+
* @uses \MacFJA\RediSearch\Redis\Command\Create
75+
* @uses \MacFJA\RediSearch\Redis\Command\CreateCommand\BaseCreateFieldOptionTrait
76+
* @uses \MacFJA\RediSearch\Redis\Command\CreateCommand\NumericFieldOption
77+
* @uses \MacFJA\RediSearch\Redis\Command\CreateCommand\TextFieldOption
78+
* @uses \MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption
79+
* @uses \MacFJA\RediSearch\Redis\Command\Option\CustomValidatorOption
80+
* @uses \MacFJA\RediSearch\Redis\Command\Option\DecoratedOptionTrait
81+
* @uses \MacFJA\RediSearch\Redis\Command\Option\FlagOption
82+
* @uses \MacFJA\RediSearch\Redis\Command\Option\GroupedOption
83+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamedOption
84+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamelessOption
85+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NotEmptyOption
86+
* @uses \MacFJA\RediSearch\Redis\Command\Option\NumberedOption
87+
* @uses \MacFJA\RediSearch\Redis\Command\Option\WithPublicGroupedSetterTrait
88+
* @uses \MacFJA\RediSearch\Redis\Command\PrefixFieldName
89+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption
90+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\HighlightOption
91+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\LimitOption
92+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\SortByOption
93+
* @uses \MacFJA\RediSearch\Redis\Command\SearchCommand\SummarizeOption
94+
* @uses \MacFJA\RediSearch\Redis\Command\AggregateCommand\SortByOption
95+
* @uses \MacFJA\RediSearch\Query\Builder\Fuzzy
96+
* @uses \MacFJA\RediSearch\Query\Escaper
97+
*
98+
* @group integration
99+
*
100+
* @internal
101+
*/
102+
class DockerTest extends TestCase
103+
{
104+
public static function setUpBeforeClass(): void
105+
{
106+
parent::setUpBeforeClass();
107+
exec('which docker', $output, $code);
108+
if ($code > 0) {
109+
static::markTestSkipped('Docker is missing');
110+
}
111+
exec('docker run --rm -p 16379:6379 -d redislabs/redisearch:latest');
112+
}
113+
114+
public static function tearDownAfterClass(): void
115+
{
116+
parent::tearDownAfterClass();
117+
exec('docker ps --filter publish=16379 -q', $output);
118+
foreach ($output as $container) {
119+
exec('docker stop '.escapeshellarg($container), $output);
120+
}
121+
}
122+
123+
/**
124+
* @dataProvider dataProvider
125+
* @large
126+
*
127+
* @param mixed $clientBuilder
128+
*/
129+
public function testIntegration($clientBuilder): void
130+
{
131+
$client = $clientBuilder();
132+
$resultObject = $this->getTestResultObject();
133+
if ($resultObject instanceof TestResult) {
134+
$resultObject->beStrictAboutOutputDuringTests(false);
135+
$resultObject->convertNoticesToExceptions(false);
136+
}
137+
138+
$list = $client->execute(new IndexList());
139+
static::assertEmpty($list);
140+
$builder = new IndexBuilder();
141+
$builder
142+
->withIndex('testDoc')
143+
->addTextField('lastname', false, null, null, true)
144+
->addTextField('firstname')
145+
->addNumericField('age')
146+
->create($client)
147+
;
148+
static::assertEquals(['testDoc'], $client->execute(new IndexList()));
149+
150+
$index = new Index('testDoc', $client);
151+
$docToRemove = [];
152+
$docToRemove[] = $index->addDocumentFromArray(['firstname' => 'Joe', 'lastname' => 'Doe', 'age' => 30]);
153+
$docToRemove[] = $index->addDocumentFromArray(['firstname' => 'Joe', 'age' => 30]);
154+
$docToRemove[] = $index->addDocumentFromArray(['firstname' => 'Joe', 'age' => 30, 'fullname' => 'eeeee']);
155+
156+
$search = new Search();
157+
158+
$query = (new Builder())
159+
->addElement(new Fuzzy('Joo'))
160+
;
161+
162+
$search
163+
->setIndex('testDoc')
164+
->setQuery($query->render())
165+
->setWithPayloads()
166+
->setHighlight(['lastname'], '<b>', '</b>')
167+
->setWithScores()
168+
;
169+
170+
/** @var PaginatedResponse $result */
171+
$result = $client->execute($search);
172+
static::assertCount(3, $result);
173+
$client->execute((new SynUpdate())->setIndex('testDoc')->setGroupId('Joe')->setTerms('John'));
174+
$syns = $client->execute((new SynDump())->setIndex('testDoc'));
175+
static::assertEquals([
176+
'john', ['Joe'],
177+
], $syns);
178+
179+
$result = $client->pipeline(
180+
(new Search())
181+
->setIndex('testDoc')
182+
->setQuery('%%Joe%%')
183+
->setWithPayloads()
184+
->setWithScores()
185+
->setHighlight(['lastname'], '<b>', '</b>'),
186+
(new Aggregate())
187+
->setIndex('testDoc')
188+
->setQuery('*')
189+
->addGroupBy(new GroupByOption(['lastname'], [ReduceOption::count('count')])),
190+
(new Aggregate())
191+
->setIndex('testDoc')
192+
->setQuery('*')
193+
->addGroupBy(new GroupByOption(['firstname'], [ReduceOption::count('count')])),
194+
(new Aggregate())
195+
->setIndex('testDoc')
196+
->setQuery('*')
197+
->addGroupBy(new GroupByOption(['age'], [ReduceOption::count('count')])),
198+
(new Aggregate())
199+
->setIndex('testDoc')
200+
->setQuery('*')
201+
->addGroupBy(new GroupByOption([], [ReduceOption::toList('age', 'list')]))
202+
);
203+
204+
static::assertCount(3, $result[0]);
205+
static::assertEquals('1', current($result[1])[0]->getValue('count'));
206+
static::assertEquals('2', current($result[1])[1]->getValue('count'));
207+
static::assertEquals('3', current($result[2])[0]->getValue('count'));
208+
static::assertEquals('3', current($result[3])[0]->getValue('count'));
209+
static::assertEquals(['30'], current($result[4])[0]->getValue('list'));
210+
211+
$client->execute((new DropIndex())->setIndex('testDoc')->setDeleteDocument());
212+
$client->executeRaw('del', ...$docToRemove);
213+
}
214+
215+
/**
216+
* @return Closure[][]
217+
*/
218+
public function dataProvider(): array
219+
{
220+
return [
221+
[static function () { return Client\PredisClient::make(new \Predis\Client(['scheme' => 'tcp', 'host' => 'localhost', 'port' => '16379', 'db' => 0])); }],
222+
[static function () { return Client\RediskaClient::make(new Rediska(['servers' => [['host' => 'localhost', 'port' => '16379', 'db' => 0]]])); }],
223+
[static function () { return Client\RedisentClient::make(new \redisent\Redis('redis://localhost:16379')); }],
224+
[static function () { return Client\CheprasovRedisClient::make(new \RedisClient\Client\Version\RedisClient6x0(['server' => 'localhost:16379', 'database' => 0])); }],
225+
[static function () { return Client\AmpRedisClient::make(new \Amp\Redis\Redis(new RemoteExecutor(Config::fromUri('redis://localhost:16379')))); }],
226+
];
227+
}
228+
}

0 commit comments

Comments
 (0)