From 20800c6a0e4a322b5c3292cf27f6dd42b65c58fc Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 30 Sep 2024 15:45:46 +0200 Subject: [PATCH] Handle 2.0 changes in schema test --- .../ODM/MongoDB/Tests/SchemaManagerTest.php | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php index a8dc4e656..fc9d33dec 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php @@ -23,6 +23,7 @@ use Documents\Tournament\Tournament; use Documents\UserName; use InvalidArgumentException; +use Iterator; use MongoDB\BSON\Document; use MongoDB\Client; use MongoDB\Collection; @@ -31,7 +32,10 @@ use MongoDB\Driver\WriteConcern; use MongoDB\GridFS\Bucket; use MongoDB\Model\CollectionInfo; +use MongoDB\Model\CollectionInfoCommandIterator; +use MongoDB\Model\CollectionInfoIterator; use MongoDB\Model\IndexInfo; +use MongoDB\Model\IndexInfoIterator; use MongoDB\Model\IndexInfoIteratorIterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Constraint\ArrayHasKey; @@ -44,6 +48,7 @@ use function array_map; use function assert; use function in_array; +use function interface_exists; /** * @psalm-import-type IndexMapping from ClassMetadata @@ -195,7 +200,7 @@ public function testEnsureIndexes(array $expectedWriteOptions, ?int $maxTimeMs, $filesCollection ->method('listIndexes') - ->willReturn([]); + ->willReturn($this->createIndexIterator()); $filesCollection ->expects($this->atLeastOnce()) ->method('createIndex') @@ -203,7 +208,7 @@ public function testEnsureIndexes(array $expectedWriteOptions, ?int $maxTimeMs, $chunksCollection ->method('listIndexes') - ->willReturn([]); + ->willReturn($this->createIndexIterator()); $chunksCollection ->expects($this->atLeastOnce()) ->method('createIndex') @@ -251,7 +256,7 @@ public function testEnsureDocumentIndexesForGridFSFile(array $expectedWriteOptio if ($class === $fileBucket) { $filesCollection ->method('listIndexes') - ->willReturn([]); + ->willReturn($this->createIndexIterator()); $filesCollection ->expects($this->once()) ->method('createIndex') @@ -259,7 +264,7 @@ public function testEnsureDocumentIndexesForGridFSFile(array $expectedWriteOptio $chunksCollection ->method('listIndexes') - ->willReturn([]); + ->willReturn($this->createIndexIterator()); $chunksCollection ->expects($this->once()) ->method('createIndex') @@ -296,7 +301,7 @@ public function testUpdateDocumentIndexesShouldCreateMappedIndexes(array $expect $collection ->expects($this->once()) ->method('listIndexes') - ->willReturn(new IndexInfoIteratorIterator(new ArrayIterator([]))); + ->willReturn($this->createIndexIterator()); $collection ->expects($this->once()) ->method('createIndex') @@ -326,7 +331,7 @@ public function testUpdateDocumentIndexesShouldDeleteUnmappedIndexesBeforeCreati $collection ->expects($this->once()) ->method('listIndexes') - ->willReturn(new IndexInfoIteratorIterator(new ArrayIterator($indexes))); + ->willReturn($this->createIndexIterator($indexes)); $collection ->expects($this->once()) ->method('createIndex') @@ -1307,10 +1312,10 @@ private function getMockDatabase() $db->method('listCollections')->willReturnCallback(function () { $collections = []; foreach ($this->documentCollections as $collectionName => $collection) { - $collections[] = new CollectionInfo(['name' => $collectionName]); + $collections[] = ['name' => $collectionName]; } - return $collections; + return $this->createCollectionIterator($collections); }); return $db; @@ -1343,4 +1348,28 @@ private function createSearchIndexCommandExceptionForOlderServers(): CommandExce { return new CommandException('Unrecognized pipeline stage name: \'$listSearchIndexes\'', 40234); } + + private function createIndexIterator(array $indexes = []): Iterator + { + if (interface_exists(IndexInfoIterator::class)) { + return new IndexInfoIteratorIterator(new ArrayIterator($indexes)); + } + + return new ArrayIterator(array_map( + fn (array $indexInfo) => new IndexInfo($indexInfo), + $indexes, + )); + } + + private function createCollectionIterator(array $collections = []): Iterator + { + if (interface_exists(CollectionInfoIterator::class)) { + return new CollectionInfoCommandIterator(new ArrayIterator($collections)); + } + + return new ArrayIterator(array_map( + fn (array $collectionInfo) => new CollectionInfo($collectionInfo), + $collections, + )); + } }