Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support version 2.0 of the MongoDB driver #2683

Draft
wants to merge 5 commits into
base: 2.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
runs-on: "ubuntu-20.04"

strategy:
fail-fast: false
matrix:
php-version:
- "8.1"
Expand All @@ -38,7 +39,7 @@ jobs:
- dependencies: "lowest"
php-version: "8.1"
mongodb-version: "5.0"
driver-version: "1.17.0"
driver-version: "1.20.0"
topology: "server"
symfony-version: "stable"
# Test with highest dependencies
Expand All @@ -55,6 +56,13 @@ jobs:
driver-version: "stable"
dependencies: "highest"
symfony-version: "stable"
# Test with ext-2.0
- topology: "server"
php-version: "8.2"
mongodb-version: "7.0"
driver-version: "mongodb/[email protected]"
dependencies: "highest"
symfony-version: "7"
# Test with a 5.0 sharded cluster
# Currently disabled due to a bug where MongoDB reports "sharding status unknown"
# - topology: "sharded_cluster"
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
],
"require": {
"php": "^8.1",
"ext-mongodb": "^1.17",
"ext-mongodb": "^1.20 || ^2.0",
"doctrine/cache": "^1.11 || ^2.0",
"doctrine/collections": "^1.5 || ^2.0",
"doctrine/event-manager": "^1.0 || ^2.0",
"doctrine/instantiator": "^1.1 || ^2",
"doctrine/persistence": "^3.2",
"friendsofphp/proxy-manager-lts": "^1.0",
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
"mongodb/mongodb": "^1.17.0",
"mongodb/mongodb": "^1.20 || ^2.0@dev",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"symfony/console": "^5.4 || ^6.0 || ^7.0",
"symfony/deprecation-contracts": "^2.2 || ^3.0",
Expand Down
56 changes: 53 additions & 3 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@
use Documents\Project;
use Documents\SubProject;
use Documents\User;
use Exception;
use InvalidArgumentException;
use LogicException;
use MongoDB\BSON\Int64;
use MongoDB\BSON\ObjectId;
use MongoDB\Collection;
use MongoDB\Driver\CursorId;
use MongoDB\Driver\CursorInterface;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Traversable;

use function array_keys;
use function class_exists;
use function iterator_to_array;

use const DOCTRINE_MONGODB_DATABASE;
Expand Down Expand Up @@ -497,7 +503,7 @@

public function testFindWithHint(): void
{
$cursor = $this->createMock(Traversable::class);
$cursor = $this->createCursorMock();

$collection = $this->getMockCollection();
$collection->expects($this->once())
Expand All @@ -524,7 +530,7 @@
$nearest = new ReadPreference('nearest');
$secondaryPreferred = new ReadPreference('secondaryPreferred');

$cursor = $this->createMock(Traversable::class);
$cursor = $this->createCursorMock();

$collection = $this->getMockCollection();
$collection->expects($this->once())
Expand Down Expand Up @@ -553,7 +559,41 @@

public function testNonRewindable(): void
{
$cursor = new ArrayIterator(['foo']);
$cursor = new class ([['foo']]) extends ArrayIterator implements CursorInterface {

Check failure on line 562 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

MissingTemplateParam

tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:562:23: MissingTemplateParam: Doctrine\ODM\MongoDB\Tests\_home_runner_work_mongodb_odm_mongodb_odm_tests_Doctrine_ODM_MongoDB_Tests_QueryTest_php_562_18313 has missing template params when extending ArrayIterator, expecting 2 (see https://psalm.dev/182)

Check failure on line 562 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

MissingTemplateParam

tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:562:74: MissingTemplateParam: Doctrine\ODM\MongoDB\Tests\_home_runner_work_mongodb_odm_mongodb_odm_tests_Doctrine_ODM_MongoDB_Tests_QueryTest_php_562_18313 has missing template params when extending MongoDB\Driver\CursorInterface, expecting 2 (see https://psalm.dev/182)
public function getId(): Int64

Check failure on line 563 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

ImplementedReturnTypeMismatch

tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:563:38: ImplementedReturnTypeMismatch: The inherited return type 'MongoDB\Driver\CursorId' for MongoDB\Driver\CursorInterface::getId is different to the implemented return type for Doctrine\ODM\MongoDB\Tests\_home_runner_work_mongodb_odm_mongodb_odm_tests_Doctrine_ODM_MongoDB_Tests_QueryTest_php_562_18313::getid 'MongoDB\BSON\Int64' (see https://psalm.dev/123)

Check failure on line 563 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Return type (MongoDB\BSON\Int64) of method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:562::getId() should be compatible with return type (MongoDB\Driver\CursorId) of method MongoDB\Driver\CursorInterface::getId()
{
return new Int64(0);
}

public function getServer(): Server
{
throw new Exception('Not implemented');
}

public function isDead(): bool
{
return false;
}

public function setTypeMap(array $typemap): void

Check failure on line 578 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:562::setTypeMap() has parameter $typemap with no value type specified in iterable type array.
{
}

public function toArray(): array

Check failure on line 582 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InvalidReturnType

tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:582:40: InvalidReturnType: The declared return type 'array<array-key, mixed>' for Doctrine\ODM\MongoDB\Tests\_home_runner_work_mongodb_odm_mongodb_odm_tests_Doctrine_ODM_MongoDB_Tests_QueryTest_php_562_18313::toArray is incorrect, got 'array<int|null, array<array-key, mixed>|null>' (see https://psalm.dev/011)

Check failure on line 582 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:562::toArray() return type has no value type specified in iterable type array.
{
return iterator_to_array($this);

Check failure on line 584 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InvalidReturnStatement

tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:584:24: InvalidReturnStatement: The inferred type 'array<int|null, array<array-key, mixed>|null>' does not match the declared return type 'array<array-key, mixed>' for Doctrine\ODM\MongoDB\Tests\_home_runner_work_mongodb_odm_mongodb_odm_tests_Doctrine_ODM_MongoDB_Tests_QueryTest_php_562_18313::toArray (see https://psalm.dev/128)
}

public function current(): array|null

Check failure on line 587 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php:562::current() return type has no value type specified in iterable type array.
{
return parent::current();
}

public function key(): int|null
{
return parent::key();
}
};

$collection = $this->getMockCollection();
$collection->expects($this->once())
Expand Down Expand Up @@ -583,6 +623,16 @@
{
return $this->createMock(Collection::class);
}

private function createCursorMock(): CursorInterface|Traversable

Check failure on line 627 in tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\ODM\MongoDB\Tests\QueryTest::createCursorMock() return type has no value type specified in iterable type Traversable.
{
return $this->createMock(
// Use the cursorID class to differentiate between 1.x and 2.x
class_exists(CursorId::class)
? Traversable::class
: CursorInterface::class,
);
}
}

#[ODM\Document(collection: 'people')]
Expand Down
45 changes: 37 additions & 8 deletions tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

UnusedBaselineEntry

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php:0:0: UnusedBaselineEntry: Baseline for issue "InternalClass" has 1 extra entry. (see https://psalm.dev/316)

Check failure on line 1 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

UnusedBaselineEntry

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php:0:0: UnusedBaselineEntry: Baseline for issue "InternalMethod" has 1 extra entry. (see https://psalm.dev/316)

declare(strict_types=1);

Expand All @@ -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 @@

$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 @@
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 @@
$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 @@
$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 @@
$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 @@
{
return new CommandException('Unrecognized pipeline stage name: \'$listSearchIndexes\'', 40234);
}

private function createIndexIterator(array $indexes = []): Iterator

Check failure on line 1352 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\ODM\MongoDB\Tests\SchemaManagerTest::createIndexIterator() has parameter $indexes with no value type specified in iterable type array.
{
if (interface_exists(IndexInfoIterator::class)) {
return new IndexInfoIteratorIterator(new ArrayIterator($indexes));
}

return new ArrayIterator(array_map(
static fn (array $indexInfo) => new IndexInfo($indexInfo),
$indexes,
));
}

private function createCollectionIterator(array $collections = []): Iterator

Check failure on line 1364 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\ODM\MongoDB\Tests\SchemaManagerTest::createCollectionIterator() has parameter $collections with no value type specified in iterable type array.
{
if (interface_exists(CollectionInfoIterator::class)) {
return new CollectionInfoCommandIterator(new ArrayIterator($collections));

Check failure on line 1367 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InternalClass

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php:1367:20: InternalClass: MongoDB\Model\CollectionInfoCommandIterator is internal to MongoDB but called from Doctrine\ODM\MongoDB\Tests\SchemaManagerTest (see https://psalm.dev/174)

Check failure on line 1367 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InternalMethod

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php:1367:20: InternalMethod: Constructor MongoDB\Model\CollectionInfoCommandIterator::__construct is internal to MongoDB but called from Doctrine\ODM\MongoDB\Tests\SchemaManagerTest::createCollectionIterator (see https://psalm.dev/175)
}

return new ArrayIterator(array_map(
static fn (array $collectionInfo) => new CollectionInfo($collectionInfo),
$collections,
));
}
}
Loading