|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Model; |
| 4 | + |
| 5 | +use MongoDB\Collection; |
| 6 | +use MongoDB\Driver\Exception\LogicException; |
| 7 | +use MongoDB\Model\TailableCursorIterator; |
| 8 | +use MongoDB\Operation\Find; |
| 9 | +use MongoDB\Operation\CreateCollection; |
| 10 | +use MongoDB\Operation\DropCollection; |
| 11 | +use MongoDB\Tests\CommandObserver; |
| 12 | +use MongoDB\Tests\FunctionalTestCase; |
| 13 | + |
| 14 | +class TailableCursorIteratorTest extends FunctionalTestCase |
| 15 | +{ |
| 16 | + private $collection; |
| 17 | + |
| 18 | + public function setUp() |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName()); |
| 23 | + $operation->execute($this->getPrimaryServer()); |
| 24 | + |
| 25 | + $operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), ['capped' => true, 'size' => 8192]); |
| 26 | + $operation->execute($this->getPrimaryServer()); |
| 27 | + |
| 28 | + $this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testFirstBatchIsEmpty() |
| 32 | + { |
| 33 | + $this->collection->insertOne(['x' => 1]); |
| 34 | + |
| 35 | + $cursor = $this->collection->find(['x' => ['$gt' => 1]], ['cursorType' => Find::TAILABLE]); |
| 36 | + $iterator = new TailableCursorIterator($cursor, true); |
| 37 | + |
| 38 | + $this->assertNoCommandExecuted(function() use ($iterator) { $iterator->rewind(); }); |
| 39 | + $this->assertFalse($iterator->valid()); |
| 40 | + |
| 41 | + $this->collection->insertOne(['x' => 2]); |
| 42 | + |
| 43 | + $iterator->next(); |
| 44 | + $this->assertTrue($iterator->valid()); |
| 45 | + $this->assertMatchesDocument(['x' => 2], $iterator->current()); |
| 46 | + |
| 47 | + $this->expectException(LogicException::class); |
| 48 | + $iterator->rewind(); |
| 49 | + } |
| 50 | + |
| 51 | + public function testFirstBatchIsNotEmpty() |
| 52 | + { |
| 53 | + $this->collection->insertOne(['x' => 1]); |
| 54 | + |
| 55 | + $cursor = $this->collection->find([], ['cursorType' => Find::TAILABLE]); |
| 56 | + $iterator = new TailableCursorIterator($cursor, false); |
| 57 | + |
| 58 | + $this->assertNoCommandExecuted(function() use ($iterator) { $iterator->rewind(); }); |
| 59 | + $this->assertTrue($iterator->valid()); |
| 60 | + $this->assertMatchesDocument(['x' => 1], $iterator->current()); |
| 61 | + |
| 62 | + $this->collection->insertOne(['x' => 2]); |
| 63 | + |
| 64 | + $iterator->next(); |
| 65 | + $this->assertTrue($iterator->valid()); |
| 66 | + $this->assertMatchesDocument(['x' => 2], $iterator->current()); |
| 67 | + |
| 68 | + $this->expectException(LogicException::class); |
| 69 | + $iterator->rewind(); |
| 70 | + } |
| 71 | + |
| 72 | + private function assertNoCommandExecuted(callable $callable) |
| 73 | + { |
| 74 | + $commands = []; |
| 75 | + |
| 76 | + (new CommandObserver)->observe( |
| 77 | + $callable, |
| 78 | + function(array $event) use (&$commands) { |
| 79 | + $this->fail(sprintf('"%s" command was executed', $event['started']->getCommandName())); |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + $this->assertEmpty($commands); |
| 84 | + } |
| 85 | +} |
0 commit comments