Skip to content

Commit

Permalink
Handle 2.0 changes in schema test
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Oct 1, 2024
1 parent 89103d5 commit 20800c6
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -195,15 +200,15 @@ public function testEnsureIndexes(array $expectedWriteOptions, ?int $maxTimeMs,

$filesCollection
->method('listIndexes')
->willReturn([]);
->willReturn($this->createIndexIterator());
$filesCollection
->expects($this->atLeastOnce())
->method('createIndex')
->with(['filename' => 1, 'uploadDate' => 1], $this->writeOptions($expectedWriteOptions));

$chunksCollection
->method('listIndexes')
->willReturn([]);
->willReturn($this->createIndexIterator());
$chunksCollection
->expects($this->atLeastOnce())
->method('createIndex')
Expand Down Expand Up @@ -251,15 +256,15 @@ public function testEnsureDocumentIndexesForGridFSFile(array $expectedWriteOptio
if ($class === $fileBucket) {
$filesCollection
->method('listIndexes')
->willReturn([]);
->willReturn($this->createIndexIterator());
$filesCollection
->expects($this->once())
->method('createIndex')
->with(['filename' => 1, 'uploadDate' => 1], $this->writeOptions($expectedWriteOptions));

$chunksCollection
->method('listIndexes')
->willReturn([]);
->willReturn($this->createIndexIterator());
$chunksCollection
->expects($this->once())
->method('createIndex')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
));
}
}

0 comments on commit 20800c6

Please sign in to comment.