|
| 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\Response; |
| 23 | + |
| 24 | +use function array_slice; |
| 25 | +use MacFJA\RediSearch\Exception\MissingClientException; |
| 26 | +use MacFJA\RediSearch\Redis\Client; |
| 27 | +use MacFJA\RediSearch\Redis\Command\PaginatedCommand; |
| 28 | +use MacFJA\RediSearch\Redis\Response\PaginatedResponse; |
| 29 | +use MacFJA\RediSearch\Redis\Response\SearchResponseItem; |
| 30 | +use MacFJA\RediSearch\tests\fixtures\Redis\Command\FakePaginatedCommand; |
| 31 | +use PHPUnit\Framework\TestCase; |
| 32 | + |
| 33 | +/** |
| 34 | + * @covers \MacFJA\RediSearch\Exception\MissingClientException |
| 35 | + * @covers \MacFJA\RediSearch\Redis\Response\PaginatedResponse |
| 36 | + * |
| 37 | + * @uses \MacFJA\RediSearch\Redis\Command\AbstractCommand |
| 38 | + * |
| 39 | + * @internal |
| 40 | + */ |
| 41 | +class PaginatedResponseTest extends TestCase |
| 42 | +{ |
| 43 | + public function testMultiplePages(): void |
| 44 | + { |
| 45 | + [$client, $command] = $this->getClientAndCommand(); |
| 46 | + |
| 47 | + /** @var PaginatedResponse $response */ |
| 48 | + $response = $client->execute($command); |
| 49 | + |
| 50 | + static::assertInstanceOf(PaginatedResponse::class, $response); |
| 51 | + |
| 52 | + static::assertTrue($response->valid()); |
| 53 | + static::assertSame('foo', $response->current()[0]->getFieldValue('f1')); |
| 54 | + static::assertCount(4, $response); |
| 55 | + static::assertEquals(2, $response->getPageSize()); |
| 56 | + static::assertEquals(4, $response->getPageCount()); |
| 57 | + static::assertEquals(7, $response->getTotalCount()); |
| 58 | + static::assertEquals(0, $response->key()); |
| 59 | + |
| 60 | + $response->setClient($client); |
| 61 | + |
| 62 | + $response->next(); |
| 63 | + |
| 64 | + static::assertTrue($response->valid()); |
| 65 | + static::assertSame('bob', $response->current()[0]->getFieldValue('f1')); |
| 66 | + static::assertCount(4, $response); |
| 67 | + static::assertEquals(2, $response->getPageSize()); |
| 68 | + static::assertEquals(4, $response->getPageCount()); |
| 69 | + static::assertEquals(7, $response->getTotalCount()); |
| 70 | + static::assertEquals(1, $response->key()); |
| 71 | + |
| 72 | + $response->next(); |
| 73 | + |
| 74 | + static::assertTrue($response->valid()); |
| 75 | + static::assertSame('lorem', $response->current()[0]->getFieldValue('f1')); |
| 76 | + static::assertCount(4, $response); |
| 77 | + static::assertEquals(2, $response->getPageSize()); |
| 78 | + static::assertEquals(4, $response->getPageCount()); |
| 79 | + static::assertEquals(7, $response->getTotalCount()); |
| 80 | + static::assertEquals(2, $response->key()); |
| 81 | + |
| 82 | + $response->next(); |
| 83 | + |
| 84 | + static::assertTrue($response->valid()); |
| 85 | + static::assertSame('odd', $response->current()[0]->getFieldValue('f1')); |
| 86 | + static::assertCount(4, $response); |
| 87 | + static::assertEquals(2, $response->getPageSize()); |
| 88 | + static::assertEquals(4, $response->getPageCount()); |
| 89 | + static::assertEquals(7, $response->getTotalCount()); |
| 90 | + static::assertEquals(3, $response->key()); |
| 91 | + |
| 92 | + $response->next(); |
| 93 | + |
| 94 | + static::assertFalse($response->valid()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testMultiplePagesIterator(): void |
| 98 | + { |
| 99 | + [$client, $command] = $this->getClientAndCommand(); |
| 100 | + |
| 101 | + /** @var PaginatedResponse $response */ |
| 102 | + $response = $client->execute($command); |
| 103 | + |
| 104 | + static::assertInstanceOf(PaginatedResponse::class, $response); |
| 105 | + $response->setClient($client); |
| 106 | + |
| 107 | + foreach ($response as $index => $page) { |
| 108 | + if (0 === $index) { |
| 109 | + static::assertSame('foo', $response->current()[0]->getFieldValue('f1')); |
| 110 | + } |
| 111 | + if (1 === $index) { |
| 112 | + static::assertSame('bob', $response->current()[0]->getFieldValue('f1')); |
| 113 | + } |
| 114 | + if (2 === $index) { |
| 115 | + static::assertSame('lorem', $response->current()[0]->getFieldValue('f1')); |
| 116 | + } |
| 117 | + if (3 === $index) { |
| 118 | + static::assertSame('odd', $response->current()[0]->getFieldValue('f1')); |
| 119 | + } |
| 120 | + |
| 121 | + static::assertEquals(2, $response->getPageSize()); |
| 122 | + static::assertEquals(4, $response->getPageCount()); |
| 123 | + static::assertEquals(7, $response->getTotalCount()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public function testMissingClient(): void |
| 128 | + { |
| 129 | + $this->expectException(MissingClientException::class); |
| 130 | + [$client, $command] = $this->getClientAndCommand(); |
| 131 | + |
| 132 | + /** @var PaginatedResponse $response */ |
| 133 | + $response = $client->execute($command); |
| 134 | + |
| 135 | + static::assertInstanceOf(PaginatedResponse::class, $response); |
| 136 | + |
| 137 | + static::assertTrue($response->valid()); |
| 138 | + |
| 139 | + $response->next(); |
| 140 | + $response->current(); |
| 141 | + } |
| 142 | + |
| 143 | + public function testPageSizeSetToZero(): void |
| 144 | + { |
| 145 | + $command = new FakePaginatedCommand([]); |
| 146 | + $command->setLimit(0, 0); |
| 147 | + $response = new PaginatedResponse($command, 2, []); |
| 148 | + |
| 149 | + static::assertSame(0, $response->key()); |
| 150 | + static::assertSame(0, $response->getPageCount()); |
| 151 | + static::assertCount(0, $response); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @return array{0:Client,1:FakePaginatedCommand} |
| 156 | + */ |
| 157 | + private function getClientAndCommand(): array |
| 158 | + { |
| 159 | + $command = new FakePaginatedCommand([]); |
| 160 | + |
| 161 | + $client = $this->createMock(Client::class); |
| 162 | + $client->method('execute')->willReturnOnConsecutiveCalls( |
| 163 | + self::getResponses($command, 0, 2), |
| 164 | + self::getResponses($command, 2, 4), |
| 165 | + self::getResponses($command, 4, 6), |
| 166 | + self::getResponses($command, 6, 7) |
| 167 | + ); |
| 168 | + |
| 169 | + return [$client, $command]; |
| 170 | + } |
| 171 | + |
| 172 | + private static function getResponses(PaginatedCommand $command, int $from, int $to): PaginatedResponse |
| 173 | + { |
| 174 | + $response = [ |
| 175 | + new SearchResponseItem('foobar1', ['f1' => 'foo', 'f2' => 'bar']), |
| 176 | + new SearchResponseItem('foobar2', ['f1' => 'baz', 'f2' => 'alice']), |
| 177 | + new SearchResponseItem('foobar3', ['f1' => 'bob', 'f2' => 'charlie']), |
| 178 | + new SearchResponseItem('foobar4', ['f1' => 'john', 'f2' => 'doe']), |
| 179 | + new SearchResponseItem('foobar5', ['f1' => 'lorem', 'f2' => 'ipsum']), |
| 180 | + new SearchResponseItem('foobar6', ['f1' => 'foobar', 'f2' => 'foobaz']), |
| 181 | + new SearchResponseItem('foobar7', ['f1' => 'odd', 'f2' => 'page']), |
| 182 | + ]; |
| 183 | + |
| 184 | + return new PaginatedResponse( |
| 185 | + $command, |
| 186 | + 7, |
| 187 | + array_slice($response, $from, $to - $from, false) |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments