|
| 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