Skip to content
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^10.5.58|^11.5.43",
"phpunit/phpunit": "^12.5.1",
"squizlabs/php_codesniffer": "^4",
"symfony/cache": "^5.4 || ^6.0 || ^7.0 || ^8.0",
"symfony/uid": "^5.4 || ^6.0 || ^7.0 || ^8.0",
Expand Down
6 changes: 2 additions & 4 deletions tests/Tests/Aggregation/AggregationTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ protected function getTestAggregationBuilder(string $documentName = User::class)
return new Builder($this->dm, $documentName);
}

/** @return MockObject|AggregationExpr */
protected function getMockAggregationExpr()
protected function getMockAggregationExpr(): AggregationExpr&MockObject
{
return $this->createMock(AggregationExpr::class);
}

/** @return MockObject|QueryExpr */
protected function getMockQueryExpr()
protected function getMockQueryExpr(): QueryExpr&MockObject
{
return $this->createMock(QueryExpr::class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OnClassMetadataNotFoundEventArgsTest extends TestCase
{
public function testEventArgsMutability(): void
{
$documentManager = $this->createMock(DocumentManager::class);
$documentManager = $this->createStub(DocumentManager::class);

$args = new OnClassMetadataNotFoundEventArgs(stdClass::class, $documentManager);

Expand Down
18 changes: 9 additions & 9 deletions tests/Tests/Functional/DocumentPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public function testExecuteInsertsRespectsWriteConcern(string $class, string|int
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));
->with($this->isArray(), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -656,7 +656,7 @@ public function testExecuteInsertsOmitsWriteConcernInTransaction(string $class,
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->logicalNot($this->arrayHasKey('writeConcern')));
->with($this->isArray(), $this->logicalNot($this->arrayHasKey('writeConcern')));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -677,7 +677,7 @@ public function testExecuteUpsertsRespectsWriteConcern(string $class, string|int
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('updateOne')
->with($this->isType('array'), $this->isType('array'), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));
->with($this->isArray(), $this->isArray(), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -699,7 +699,7 @@ public function testExecuteUpsertsDoesNotUseWriteConcernInTransaction(string $cl
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('updateOne')
->with($this->isType('array'), $this->logicalNot($this->arrayHasKey('writeConcern')));
->with($this->isArray(), $this->logicalNot($this->arrayHasKey('writeConcern')));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -721,7 +721,7 @@ public function testRemoveRespectsWriteConcern(string $class, string|int $writeC
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('deleteOne')
->with($this->isType('array'), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));
->with($this->isArray(), $this->logicalAnd($this->arrayHasKey('writeConcern'), $this->containsEqual(new WriteConcern($writeConcern))));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -745,7 +745,7 @@ public function testRemoveDoesNotUseWriteConcernInTransaction(string $class, str
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('deleteOne')
->with($this->isType('array'), $this->logicalNot($this->arrayHasKey('writeConcern')));
->with($this->isArray(), $this->logicalNot($this->arrayHasKey('writeConcern')));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -768,7 +768,7 @@ public function testDefaultWriteConcernIsRespected(): void
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->equalTo(['writeConcern' => new WriteConcern(0)]));
->with($this->isArray(), $this->equalTo(['writeConcern' => new WriteConcern(0)]));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -790,7 +790,7 @@ public function testDefaultWriteConcernIsIgnoredInTransaction(): void
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->logicalNot($this->arrayHasKey('writeConcern')));
->with($this->isArray(), $this->logicalNot($this->arrayHasKey('writeConcern')));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand All @@ -812,7 +812,7 @@ public function testDefaultWriteConcernIsRespectedBackwardCompatibility(): void
$collection = $this->createMock(Collection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->equalTo(['writeConcern' => new WriteConcern(0)]));
->with($this->isArray(), $this->equalTo(['writeConcern' => new WriteConcern(0)]));

$reflectionProperty = new ReflectionProperty($documentPersister, 'collection');
$reflectionProperty->setValue($documentPersister, $collection);
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/Functional/SplObjectHashCollisionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SplObjectHashCollisionsTest extends BaseTestCase
{
/** @param callable(DocumentManager, object=): void $f */
#[DataProvider('provideParentAssociationsIsCleared')]
public function testParentAssociationsIsCleared(callable $f): void
public function testParentAssociationsIsCleared(callable $f, int $leftover): void
{
$d = new SplColDoc();
$d->one = new SplColEmbed('d.one.v1');
Expand Down
3 changes: 1 addition & 2 deletions tests/Tests/Proxy/Factory/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

class ProxyFactoryTest extends BaseTestCase
{
/** @var Client|MockObject */
private Client $client;
private Client&MockObject $client;

public function testProxyInitializeWithException(): void
{
Expand Down
7 changes: 3 additions & 4 deletions tests/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ public static function provideProxiedExprMethods(): array
'type()' => ['type', [7]],
'all()' => ['all', [['value1', 'value2']]],
'mod()' => ['mod', [2, 0]],
'near()' => ['near', [1, 2], null, 5, 10],
'nearSphere()' => ['nearSphere', [1, 2], null, 5, 10],
'near()' => ['near', [[1, 2], null, 5, 10]],
'nearSphere()' => ['nearSphere', [[1, 2], null, 5, 10]],
Comment on lines +500 to +501
Copy link
Member Author

@IonBazan IonBazan Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused extra arguments to be provided to the test. It only accepts 2 arguments and the second one must be array. I believe it was causing unexpected results.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you backport this fix?

'geoIntersects()' => ['geoIntersects', [self::createGeometry()]],
'geoWithin()' => ['geoWithin', [self::createGeometry()]],
'geoWithinBox()' => ['geoWithinBox', [1, 2, 3, 4]],
Expand Down Expand Up @@ -864,8 +864,7 @@ private function getTestQueryBuilder(): Builder
return new Builder($this->dm, User::class);
}

/** @return MockObject&Expr */
private function getMockExpr()
private function getMockExpr(): Expr&MockObject
{
return $this->createMock(Expr::class);
}
Expand Down
21 changes: 7 additions & 14 deletions tests/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
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 PHPUnit\Framework\MockObject\Stub;
use Traversable;

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

use const DOCTRINE_MONGODB_DATABASE;
Expand Down Expand Up @@ -426,7 +425,7 @@ public function testConstructorShouldThrowExceptionForInvalidType(): void
{
$this->expectException(InvalidArgumentException::class);

new Query($this->dm, new ClassMetadata(User::class), $this->getMockCollection(), ['type' => -1], []);
new Query($this->dm, new ClassMetadata(User::class), $this->createStub(Collection::class), ['type' => -1], []);
}

/** @param Query::TYPE_* $type */
Expand Down Expand Up @@ -502,7 +501,7 @@ public function testCountOptionInheritance(): void

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

$collection = $this->getMockCollection();
$collection->expects($this->once())
Expand All @@ -529,7 +528,7 @@ public function testFindOptionInheritance(): void
$nearest = new ReadPreference('nearest');
$secondaryPreferred = new ReadPreference('secondaryPreferred');

$cursor = $this->createCursorMock();
$cursor = $this->createCursorStub();

$collection = $this->getMockCollection();
$collection->expects($this->once())
Expand Down Expand Up @@ -620,20 +619,14 @@ public function key(): int
iterator_to_array($iterator);
}

/** @return MockObject&Collection */
private function getMockCollection()
private function getMockCollection(): Collection&MockObject
{
return $this->createMock(Collection::class);
}

private function createCursorMock(): CursorInterface|Traversable
private function createCursorStub(): CursorInterface&Stub
{
return $this->createMock(
// Use the cursorID class to differentiate between 1.x and 2.x
class_exists(CursorId::class)
? Traversable::class
: CursorInterface::class,
);
return $this->createStub(CursorInterface::class);
}
}

Expand Down
24 changes: 11 additions & 13 deletions tests/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use ReflectionProperty;

use function array_count_values;
Expand Down Expand Up @@ -88,7 +89,7 @@ class SchemaManagerTest extends BaseTestCase
/** @var array<Collection&MockObject> */
private array $documentCollections = [];

/** @var array<Bucket&MockObject> */
/** @var array<Bucket&Stub> */
private array $documentBuckets = [];

/** @var array<Database&MockObject> */
Expand All @@ -100,16 +101,16 @@ public function setUp(): void
{
parent::setUp();

$client = $this->createMock(Client::class);
$this->dm = DocumentManager::create($client, $this->dm->getConfiguration(), $this->createMock(EventManager::class));
$client = $this->createStub(Client::class);
$this->dm = DocumentManager::create($client, $this->dm->getConfiguration(), $this->createStub(EventManager::class));

foreach ($this->dm->getMetadataFactory()->getAllMetadata() as $cm) {
if ($cm->isMappedSuperclass || $cm->isEmbeddedDocument || $cm->isQueryResultDocument) {
continue;
}

if ($cm->isFile) {
$this->documentBuckets[$cm->getBucketName()] = $this->getMockBucket();
$this->documentBuckets[$cm->getBucketName()] = $this->getBucketStub();
} else {
$this->documentCollections[$cm->getCollection()] = $this->getMockCollection($cm->getCollection());
}
Expand Down Expand Up @@ -1407,30 +1408,27 @@ private function getDatabaseName(ClassMetadata $cm): string
return ($cm->getDatabase() ?: $this->dm->getConfiguration()->getDefaultDB()) ?: 'doctrine';
}

/** @return Bucket&MockObject */
private function getMockBucket()
private function getBucketStub(): Bucket&Stub
{
$mock = $this->createMock(Bucket::class);
$mock = $this->createStub(Bucket::class);
$mock->method('getFilesCollection')->willReturn($this->getMockCollection());
$mock->method('getChunksCollection')->willReturn($this->getMockCollection());

return $mock;
}

/** @return Collection&MockObject */
private function getMockCollection(?string $name = null)
private function getMockCollection(?string $name = null): Collection&MockObject
{
$collection = $this->createMock(Collection::class);
$collection->method('getCollectionName')->willReturnCallback(static fn () => $name);
$collection->expects($this->atLeast(0))->method('getCollectionName')->willReturnCallback(static fn () => $name);

return $collection;
}

/** @return Database&MockObject */
private function getMockDatabase()
private function getMockDatabase(): Database&MockObject
{
$db = $this->createMock(Database::class);
$db->method('getCollection')->willReturnCallback(fn (string $collection) => $this->documentCollections[$collection]);
$db->expects($this->atLeast(0))->method('getCollection')->willReturnCallback(fn (string $collection) => $this->documentCollections[$collection]);
$db->method('selectGridFSBucket')->willReturnCallback(fn (array $options) => $this->documentBuckets[$options['bucketName']]);
$db->method('listCollections')->willReturnCallback(function () {
$collections = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public function testTransactionalCommitOmitsWriteConcernInOperation(): void
$collection = $this->createMock(MongoDBCollection::class);
$collection->expects($this->once())
->method('insertMany')
->with($this->isType('array'), $this->logicalNot($this->arrayHasKey('writeConcern')));
->with($this->isArray(), $this->logicalNot($this->arrayHasKey('writeConcern')));

$documentPersister = $this->uow->getDocumentPersister(ForumUser::class);

Expand Down