|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Mcp\Server\Tests\Unit\Dispatcher; |
| 6 | + |
| 7 | +use Mcp\Server\Dispatcher\Paginator; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Psr\Log\LoggerInterface; |
| 11 | +use Psr\Log\NullLogger; |
| 12 | + |
| 13 | +final class PaginatorTest extends TestCase |
| 14 | +{ |
| 15 | + private Paginator $paginator; |
| 16 | + private LoggerInterface&MockObject $logger; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 21 | + $this->paginator = new Paginator( |
| 22 | + paginationLimit: 3, |
| 23 | + logger: $this->logger, |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + public function testPaginateWithNullCursor(): void |
| 28 | + { |
| 29 | + $items = ['a', 'b', 'c', 'd', 'e', 'f']; |
| 30 | + |
| 31 | + $result = $this->paginator->paginate($items, null); |
| 32 | + |
| 33 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 34 | + $this->assertNotNull($result['nextCursor']); |
| 35 | + $this->assertSame('offset=3', base64_decode($result['nextCursor'])); |
| 36 | + } |
| 37 | + |
| 38 | + public function testPaginateWithValidCursor(): void |
| 39 | + { |
| 40 | + $items = ['a', 'b', 'c', 'd', 'e', 'f']; |
| 41 | + $cursor = base64_encode('offset=3'); |
| 42 | + |
| 43 | + $result = $this->paginator->paginate($items, $cursor); |
| 44 | + |
| 45 | + $this->assertSame(['d', 'e', 'f'], $result['items']); |
| 46 | + $this->assertNull($result['nextCursor']); |
| 47 | + } |
| 48 | + |
| 49 | + public function testPaginateEmptyItems(): void |
| 50 | + { |
| 51 | + $items = []; |
| 52 | + |
| 53 | + $result = $this->paginator->paginate($items, null); |
| 54 | + |
| 55 | + $this->assertSame([], $result['items']); |
| 56 | + $this->assertNull($result['nextCursor']); |
| 57 | + } |
| 58 | + |
| 59 | + public function testPaginateSinglePage(): void |
| 60 | + { |
| 61 | + $items = ['a', 'b']; |
| 62 | + |
| 63 | + $result = $this->paginator->paginate($items, null); |
| 64 | + |
| 65 | + $this->assertSame(['a', 'b'], $result['items']); |
| 66 | + $this->assertNull($result['nextCursor']); |
| 67 | + } |
| 68 | + |
| 69 | + public function testPaginateExactPageBoundary(): void |
| 70 | + { |
| 71 | + $items = ['a', 'b', 'c']; |
| 72 | + |
| 73 | + $result = $this->paginator->paginate($items, null); |
| 74 | + |
| 75 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 76 | + $this->assertNull($result['nextCursor']); |
| 77 | + } |
| 78 | + |
| 79 | + public function testPaginateWithOffsetBeyondItems(): void |
| 80 | + { |
| 81 | + $items = ['a', 'b', 'c']; |
| 82 | + $cursor = base64_encode('offset=10'); |
| 83 | + |
| 84 | + $result = $this->paginator->paginate($items, $cursor); |
| 85 | + |
| 86 | + $this->assertSame([], $result['items']); |
| 87 | + $this->assertNull($result['nextCursor']); |
| 88 | + } |
| 89 | + |
| 90 | + public function testPaginateWithPartialLastPage(): void |
| 91 | + { |
| 92 | + $items = ['a', 'b', 'c', 'd', 'e']; |
| 93 | + $cursor = base64_encode('offset=3'); |
| 94 | + |
| 95 | + $result = $this->paginator->paginate($items, $cursor); |
| 96 | + |
| 97 | + $this->assertSame(['d', 'e'], $result['items']); |
| 98 | + $this->assertNull($result['nextCursor']); |
| 99 | + } |
| 100 | + |
| 101 | + public function testPaginateWithMultiplePages(): void |
| 102 | + { |
| 103 | + $items = range('a', 'j'); // 10 items |
| 104 | + |
| 105 | + // First page |
| 106 | + $result1 = $this->paginator->paginate($items, null); |
| 107 | + $this->assertSame(['a', 'b', 'c'], $result1['items']); |
| 108 | + $this->assertNotNull($result1['nextCursor']); |
| 109 | + |
| 110 | + // Second page |
| 111 | + $result2 = $this->paginator->paginate($items, $result1['nextCursor']); |
| 112 | + $this->assertSame(['d', 'e', 'f'], $result2['items']); |
| 113 | + $this->assertNotNull($result2['nextCursor']); |
| 114 | + |
| 115 | + // Third page |
| 116 | + $result3 = $this->paginator->paginate($items, $result2['nextCursor']); |
| 117 | + $this->assertSame(['g', 'h', 'i'], $result3['items']); |
| 118 | + $this->assertNotNull($result3['nextCursor']); |
| 119 | + |
| 120 | + // Last page |
| 121 | + $result4 = $this->paginator->paginate($items, $result3['nextCursor']); |
| 122 | + $this->assertSame(['j'], $result4['items']); |
| 123 | + $this->assertNull($result4['nextCursor']); |
| 124 | + } |
| 125 | + |
| 126 | + public function testInvalidBase64Cursor(): void |
| 127 | + { |
| 128 | + $items = ['a', 'b', 'c', 'd']; |
| 129 | + $invalidCursor = 'invalid-base64!@#'; |
| 130 | + |
| 131 | + $this->logger |
| 132 | + ->expects($this->once()) |
| 133 | + ->method('warning') |
| 134 | + ->with( |
| 135 | + 'Received invalid pagination cursor (not base64)', |
| 136 | + ['cursor' => $invalidCursor] |
| 137 | + ); |
| 138 | + |
| 139 | + $result = $this->paginator->paginate($items, $invalidCursor); |
| 140 | + |
| 141 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 142 | + } |
| 143 | + |
| 144 | + public function testInvalidCursorFormat(): void |
| 145 | + { |
| 146 | + $items = ['a', 'b', 'c', 'd']; |
| 147 | + $invalidFormatCursor = base64_encode('invalid-format'); |
| 148 | + |
| 149 | + $this->logger |
| 150 | + ->expects($this->once()) |
| 151 | + ->method('warning') |
| 152 | + ->with( |
| 153 | + 'Received invalid pagination cursor format', |
| 154 | + ['cursor' => 'invalid-format'] |
| 155 | + ); |
| 156 | + |
| 157 | + $result = $this->paginator->paginate($items, $invalidFormatCursor); |
| 158 | + |
| 159 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 160 | + } |
| 161 | + |
| 162 | + public function testCursorWithNonNumericOffset(): void |
| 163 | + { |
| 164 | + $items = ['a', 'b', 'c', 'd']; |
| 165 | + $invalidOffsetCursor = base64_encode('offset=abc'); |
| 166 | + |
| 167 | + $this->logger |
| 168 | + ->expects($this->once()) |
| 169 | + ->method('warning') |
| 170 | + ->with( |
| 171 | + 'Received invalid pagination cursor format', |
| 172 | + ['cursor' => 'offset=abc'] |
| 173 | + ); |
| 174 | + |
| 175 | + $result = $this->paginator->paginate($items, $invalidOffsetCursor); |
| 176 | + |
| 177 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 178 | + } |
| 179 | + |
| 180 | + public function testCustomPaginationLimit(): void |
| 181 | + { |
| 182 | + $customPaginator = new Paginator(paginationLimit: 5, logger: $this->logger); |
| 183 | + $items = range(1, 12); |
| 184 | + |
| 185 | + $result = $customPaginator->paginate($items, null); |
| 186 | + |
| 187 | + $this->assertSame([1, 2, 3, 4, 5], $result['items']); |
| 188 | + $this->assertNotNull($result['nextCursor']); |
| 189 | + $this->assertSame('offset=5', base64_decode($result['nextCursor'])); |
| 190 | + } |
| 191 | + |
| 192 | + public function testDefaultPaginationLimit(): void |
| 193 | + { |
| 194 | + $defaultPaginator = new Paginator(); |
| 195 | + $items = range(1, 100); |
| 196 | + |
| 197 | + $result = $defaultPaginator->paginate($items, null); |
| 198 | + |
| 199 | + $this->assertCount(50, $result['items']); // Default limit is 50 |
| 200 | + $this->assertSame(range(1, 50), $result['items']); |
| 201 | + $this->assertNotNull($result['nextCursor']); |
| 202 | + } |
| 203 | + |
| 204 | + public function testArrayValuesReindexing(): void |
| 205 | + { |
| 206 | + $items = [ |
| 207 | + 'key1' => 'value1', |
| 208 | + 'key2' => 'value2', |
| 209 | + 'key3' => 'value3', |
| 210 | + ]; |
| 211 | + |
| 212 | + $result = $this->paginator->paginate($items, null); |
| 213 | + |
| 214 | + // array_values should reindex the array |
| 215 | + $this->assertSame(['value1', 'value2', 'value3'], $result['items']); |
| 216 | + $this->assertSame([0, 1, 2], array_keys($result['items'])); |
| 217 | + } |
| 218 | + |
| 219 | + public function testZeroOffsetCursor(): void |
| 220 | + { |
| 221 | + $items = ['a', 'b', 'c', 'd']; |
| 222 | + $zeroOffsetCursor = base64_encode('offset=0'); |
| 223 | + |
| 224 | + $result = $this->paginator->paginate($items, $zeroOffsetCursor); |
| 225 | + |
| 226 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 227 | + $this->assertNotNull($result['nextCursor']); |
| 228 | + } |
| 229 | + |
| 230 | + public function testCursorEncodingDecoding(): void |
| 231 | + { |
| 232 | + $offset = 42; |
| 233 | + $expectedCursorContent = "offset={$offset}"; |
| 234 | + $cursor = base64_encode($expectedCursorContent); |
| 235 | + |
| 236 | + // Verify we can decode what we encode |
| 237 | + $decoded = base64_decode($cursor, true); |
| 238 | + $this->assertSame($expectedCursorContent, $decoded); |
| 239 | + |
| 240 | + // Test with large offset |
| 241 | + $largeOffset = 999999; |
| 242 | + $largeCursorContent = "offset={$largeOffset}"; |
| 243 | + $largeCursor = base64_encode($largeCursorContent); |
| 244 | + $decodedLarge = base64_decode($largeCursor, true); |
| 245 | + $this->assertSame($largeCursorContent, $decodedLarge); |
| 246 | + } |
| 247 | + |
| 248 | + public function testEdgeCaseWithSingleItem(): void |
| 249 | + { |
| 250 | + $items = ['only-item']; |
| 251 | + |
| 252 | + $result = $this->paginator->paginate($items, null); |
| 253 | + |
| 254 | + $this->assertSame(['only-item'], $result['items']); |
| 255 | + $this->assertNull($result['nextCursor']); |
| 256 | + } |
| 257 | + |
| 258 | + public function testPaginateWithAssociativeArrayPreservesValues(): void |
| 259 | + { |
| 260 | + $items = [ |
| 261 | + ['id' => 1, 'name' => 'Alice'], |
| 262 | + ['id' => 2, 'name' => 'Bob'], |
| 263 | + ['id' => 3, 'name' => 'Charlie'], |
| 264 | + ['id' => 4, 'name' => 'Diana'], |
| 265 | + ]; |
| 266 | + |
| 267 | + $result = $this->paginator->paginate($items, null); |
| 268 | + |
| 269 | + $this->assertCount(3, $result['items']); |
| 270 | + $this->assertSame(['id' => 1, 'name' => 'Alice'], $result['items'][0]); |
| 271 | + $this->assertSame(['id' => 2, 'name' => 'Bob'], $result['items'][1]); |
| 272 | + $this->assertSame(['id' => 3, 'name' => 'Charlie'], $result['items'][2]); |
| 273 | + $this->assertNotNull($result['nextCursor']); |
| 274 | + } |
| 275 | + |
| 276 | + public function testPaginateWithNullLogger(): void |
| 277 | + { |
| 278 | + $paginatorWithNullLogger = new Paginator(paginationLimit: 2, logger: new NullLogger()); |
| 279 | + $items = ['a', 'b', 'c', 'd']; |
| 280 | + $invalidCursor = 'invalid-base64!@#'; |
| 281 | + |
| 282 | + // Should not throw any exceptions even with invalid cursor |
| 283 | + $result = $paginatorWithNullLogger->paginate($items, $invalidCursor); |
| 284 | + |
| 285 | + $this->assertSame(['a', 'b'], $result['items']); |
| 286 | + $this->assertNotNull($result['nextCursor']); |
| 287 | + } |
| 288 | + |
| 289 | + public function testPaginateWithLargeOffset(): void |
| 290 | + { |
| 291 | + $items = ['a', 'b', 'c']; |
| 292 | + $cursor = base64_encode('offset=1000'); |
| 293 | + |
| 294 | + $result = $this->paginator->paginate($items, $cursor); |
| 295 | + |
| 296 | + $this->assertSame([], $result['items']); |
| 297 | + $this->assertNull($result['nextCursor']); |
| 298 | + } |
| 299 | + |
| 300 | + public function testPaginateWithNegativeOffsetInCursor(): void |
| 301 | + { |
| 302 | + $items = ['a', 'b', 'c', 'd']; |
| 303 | + $negativeOffsetCursor = base64_encode('offset=-5'); |
| 304 | + |
| 305 | + $this->logger |
| 306 | + ->expects($this->once()) |
| 307 | + ->method('warning') |
| 308 | + ->with( |
| 309 | + 'Received invalid pagination cursor format', |
| 310 | + ['cursor' => 'offset=-5'] |
| 311 | + ); |
| 312 | + |
| 313 | + $result = $this->paginator->paginate($items, $negativeOffsetCursor); |
| 314 | + |
| 315 | + $this->assertSame(['a', 'b', 'c'], $result['items']); |
| 316 | + } |
| 317 | + |
| 318 | + public function testPaginateReturnsConsistentStructure(): void |
| 319 | + { |
| 320 | + $items = ['test']; |
| 321 | + |
| 322 | + $result = $this->paginator->paginate($items, null); |
| 323 | + |
| 324 | + $this->assertIsArray($result); |
| 325 | + $this->assertArrayHasKey('items', $result); |
| 326 | + $this->assertArrayHasKey('nextCursor', $result); |
| 327 | + $this->assertIsArray($result['items']); |
| 328 | + $this->assertTrue(is_string($result['nextCursor']) || is_null($result['nextCursor'])); |
| 329 | + } |
| 330 | +} |
0 commit comments