|
5 | 5 | use MongoDB\BSON\Binary;
|
6 | 6 | use MongoDB\GridFS\CollectionWrapper;
|
7 | 7 | use MongoDB\GridFS\ReadableStream;
|
| 8 | +use MongoDB\Tests\CommandObserver; |
| 9 | +use stdClass; |
8 | 10 |
|
9 | 11 | /**
|
10 | 12 | * Functional tests for the internal ReadableStream class.
|
@@ -200,31 +202,108 @@ public function testSeekOutOfRange()
|
200 | 202 | $stream->seek(11);
|
201 | 203 | }
|
202 | 204 |
|
203 |
| - public function testSeekPreviousChunk() |
| 205 | + /** |
| 206 | + * @dataProvider providePreviousChunkSeekOffsetAndBytes |
| 207 | + */ |
| 208 | + public function testSeekPreviousChunk($offset, $length, $expectedBytes) |
204 | 209 | {
|
205 | 210 | $fileDocument = $this->collectionWrapper->findFileById('length-10');
|
206 | 211 | $stream = new ReadableStream($this->collectionWrapper, $fileDocument);
|
207 | 212 |
|
208 |
| - $stream->readBytes(1); |
209 |
| - $stream->seek(5); |
210 |
| - $stream->seek(2); |
211 |
| - $stream->readBytes(1); |
| 213 | + // Read to initialize and advance the chunk iterator to the last chunk |
| 214 | + $this->assertSame('abcdefghij', $stream->readBytes(10)); |
| 215 | + |
| 216 | + $commands = []; |
| 217 | + |
| 218 | + (new CommandObserver)->observe( |
| 219 | + function() use ($stream, $offset, $length, $expectedBytes) { |
| 220 | + $stream->seek($offset); |
| 221 | + $this->assertSame($expectedBytes, $stream->readBytes($length)); |
| 222 | + }, |
| 223 | + function(stdClass $command) use (&$commands) { |
| 224 | + $commands[] = key((array) $command); |
| 225 | + } |
| 226 | + ); |
| 227 | + |
| 228 | + $this->assertSame(['find'], $commands); |
212 | 229 | }
|
213 | 230 |
|
214 |
| - public function testSeekSubsequentChunk() |
| 231 | + public function providePreviousChunkSeekOffsetAndBytes() |
| 232 | + { |
| 233 | + return [ |
| 234 | + [0, 4, 'abcd'], |
| 235 | + [2, 4, 'cdef'], |
| 236 | + [4, 4, 'efgh'], |
| 237 | + [6, 4, 'ghij'], |
| 238 | + ]; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @dataProvider provideSameChunkSeekOffsetAndBytes |
| 243 | + */ |
| 244 | + public function testSeekSameChunk($offset, $length, $expectedBytes) |
215 | 245 | {
|
216 | 246 | $fileDocument = $this->collectionWrapper->findFileById('length-10');
|
| 247 | + $stream = new ReadableStream($this->collectionWrapper, $fileDocument); |
| 248 | + |
| 249 | + // Read to initialize and advance the chunk iterator to the middle chunk |
| 250 | + $this->assertSame('abcdef', $stream->readBytes(6)); |
217 | 251 |
|
218 |
| - $observer = $this->getMockBuilder(ReadableStream::class) |
219 |
| - ->setConstructorArgs(array($this->collectionWrapper, $fileDocument)) |
220 |
| - ->getMock(); |
| 252 | + $commands = []; |
221 | 253 |
|
222 |
| - $observer->expects($this->never()) |
223 |
| - ->method('initChunksIterator'); |
| 254 | + (new CommandObserver)->observe( |
| 255 | + function() use ($stream, $offset, $length, $expectedBytes) { |
| 256 | + $stream->seek($offset); |
| 257 | + $this->assertSame($expectedBytes, $stream->readBytes($length)); |
| 258 | + }, |
| 259 | + function(stdClass $command) use (&$commands) { |
| 260 | + $commands[] = key((array) $command); |
| 261 | + } |
| 262 | + ); |
224 | 263 |
|
225 |
| - $observer->readBytes(1); |
226 |
| - $observer->seek(5); |
227 |
| - $observer->seek(2); |
228 |
| - $observer->readBytes(1); |
| 264 | + $this->assertSame([], $commands); |
| 265 | + } |
| 266 | + |
| 267 | + public function provideSameChunkSeekOffsetAndBytes() |
| 268 | + { |
| 269 | + return [ |
| 270 | + [4, 4, 'efgh'], |
| 271 | + [6, 4, 'ghij'], |
| 272 | + ]; |
| 273 | + } |
| 274 | + |
| 275 | + /** |
| 276 | + * @dataProvider provideSubsequentChunkSeekOffsetAndBytes |
| 277 | + */ |
| 278 | + public function testSeekSubsequentChunk($offset, $length, $expectedBytes) |
| 279 | + { |
| 280 | + $fileDocument = $this->collectionWrapper->findFileById('length-10'); |
| 281 | + $stream = new ReadableStream($this->collectionWrapper, $fileDocument); |
| 282 | + |
| 283 | + // Read to initialize the chunk iterator to the first chunk |
| 284 | + $this->assertSame('a', $stream->readBytes(1)); |
| 285 | + |
| 286 | + $commands = []; |
| 287 | + |
| 288 | + (new CommandObserver)->observe( |
| 289 | + function() use ($stream, $offset, $length, $expectedBytes) { |
| 290 | + $stream->seek($offset); |
| 291 | + $this->assertSame($expectedBytes, $stream->readBytes($length)); |
| 292 | + }, |
| 293 | + function(stdClass $command) use (&$commands) { |
| 294 | + $commands[] = key((array) $command); |
| 295 | + } |
| 296 | + ); |
| 297 | + |
| 298 | + $this->assertSame([], $commands); |
| 299 | + } |
| 300 | + |
| 301 | + public function provideSubsequentChunkSeekOffsetAndBytes() |
| 302 | + { |
| 303 | + return [ |
| 304 | + [4, 4, 'efgh'], |
| 305 | + [6, 4, 'ghij'], |
| 306 | + [8, 2, 'ij'], |
| 307 | + ]; |
229 | 308 | }
|
230 | 309 | }
|
0 commit comments