diff --git a/composer.json b/composer.json index f3ffa68..f4e066f 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "ext-json": "*", "ext-mongodb": "*", "symfony/messenger": "^5.0 || ^6.0 || ^7.0", - "mongodb/mongodb": "^1.12" + "mongodb/mongodb": "^1.12 || ^2.1" }, "require-dev": { "symfony/serializer": "^5.0 || ^6.0 || ^7.0", diff --git a/docker/Dockerfile b/docker/Dockerfile index b66450c..313de8b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -7,7 +7,7 @@ RUN apk update \ && apk add --no-cache git zip make autoconf g++ openssl-dev linux-headers \ && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS -RUN pecl -q install mongodb-1.17.2 \ +RUN pecl -q install mongodb-2.1.1 \ && docker-php-ext-enable mongodb \ && pecl -q install xdebug \ && docker-php-ext-enable xdebug diff --git a/src/MongoTransport.php b/src/MongoTransport.php index b2d0e7e..b96786e 100644 --- a/src/MongoTransport.php +++ b/src/MongoTransport.php @@ -161,7 +161,7 @@ public function send(Envelope $envelope): Envelope return $envelope->with(new TransportMessageIdStamp($objectId)); } - public function all(int $limit = null): iterable + public function all(int $limit = null): \Traversable { $documents = $this->collection->find([], ['limit' => $limit]); diff --git a/tests/Unit/MongoTransportTest.php b/tests/Unit/MongoTransportTest.php index 7abf596..51a5ecf 100644 --- a/tests/Unit/MongoTransportTest.php +++ b/tests/Unit/MongoTransportTest.php @@ -8,6 +8,7 @@ use EmagTechLabs\MessengerMongoBundle\Tests\Unit\Fixtures\HelloMessage; use MongoDB\BSON\ObjectId; use MongoDB\Collection; +use MongoDB\Driver\CursorInterface; use MongoDB\InsertOneResult; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -128,11 +129,11 @@ public function itShouldListAllMessages(): void $collection = $this->createMock(Collection::class); $collection->method('find') - ->willReturn([ + ->willReturn($this->createCursor([ $this->createDocument(), $this->createDocument(), $this->createDocument(), - ]); + ])); $transport = new MongoTransport( $collection, @@ -290,4 +291,76 @@ private function createSerializer(): SerializerInterface { return new Serializer(); } + + /** + * Zwraca prosty kursor zgodny z MongoDB\Driver\CursorInterface, + * który iteruje po przekazanej tablicy dokumentów. + * + * @param array> $documents + */ + private function createCursor(array $documents): \MongoDB\Driver\CursorInterface + { + return new class($documents) implements \MongoDB\Driver\CursorInterface + { + private array $data; + private int $position = 0; + + public function __construct(array $data) + { + $this->data = array_values($data); + } + + // Iterator + public function current(): array|null|object + { + return $this->data[$this->position]; + } + + public function key(): int + { + return $this->position; + } + + public function next(): void + { + $this->position++; + } + + public function rewind(): void + { + $this->position = 0; + } + + public function valid(): bool + { + return array_key_exists($this->position, $this->data); + } + + // CursorInterface + public function toArray(): array + { + return $this->data; + } + + public function isDead(): bool + { + return false; + } + + public function setTypeMap(array $typemap): void + { + // NOP – niepotrzebne w testach + } + + public function getId(): \MongoDB\BSON\Int64 + { + throw new \RuntimeException('Not implemented in test cursor.'); + } + + public function getServer(): \MongoDB\Driver\Server + { + throw new \RuntimeException('Not implemented in test cursor.'); + } + }; + } }