diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php index f0099c28e..6a48f5fc1 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php @@ -15,6 +15,7 @@ use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata; use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriver; +use MongoDB\BSON\Document; use MongoDB\Driver\Exception\UnexpectedValueException; use ReflectionClass; use ReflectionMethod; @@ -27,8 +28,6 @@ use function constant; use function count; use function is_array; -use function MongoDB\BSON\fromJSON; -use function MongoDB\BSON\toPHP; use function trigger_deprecation; /** @@ -133,12 +132,12 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad } elseif ($attribute instanceof ODM\Validation) { if (isset($attribute->validator)) { try { - $validatorBson = fromJSON($attribute->validator); + $validatorBson = Document::fromJSON($attribute->validator); } catch (UnexpectedValueException $e) { throw MappingException::schemaValidationError($e->getCode(), $e->getMessage(), $className, 'validator'); } - $validator = toPHP($validatorBson, []); + $validator = $validatorBson->toPHP(); $metadata->setValidator($validator); } diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php index 6c288a2d9..64f32867c 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php @@ -11,6 +11,7 @@ use DOMDocument; use InvalidArgumentException; use LibXMLError; +use MongoDB\BSON\Document; use MongoDB\Driver\Exception\UnexpectedValueException; use SimpleXMLElement; @@ -31,8 +32,6 @@ use function libxml_clear_errors; use function libxml_get_errors; use function libxml_use_internal_errors; -use function MongoDB\BSON\fromJSON; -use function MongoDB\BSON\toPHP; use function next; use function preg_match; use function simplexml_load_file; @@ -215,12 +214,12 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C $validatorJson = (string) $xmlSchemaValidation; try { - $validatorBson = fromJSON($validatorJson); + $validatorBson = Document::fromJSON($validatorJson); } catch (UnexpectedValueException $e) { throw MappingException::schemaValidationError($e->getCode(), $e->getMessage(), $className, 'schema-validation'); } - $validator = toPHP($validatorBson, []); + $validator = $validatorBson->toPHP(); $metadata->setValidator($validator); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ae58dfd95..6e6aca3be 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -956,12 +956,12 @@ parameters: path: tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataLoadEventTest.php - - message: "#^Parameter \\#1 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\\\:\\:mapField\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\{fieldName\\: 'enum', enumType\\: 'Documents\\\\\\\\Card'\\} given\\.$#" + message: "#^Parameter \\#1 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\\\:\\:mapField\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\{fieldName\\: 'enum', enumType\\: 'Documents\\\\\\\\Card'\\} given\\.$#" count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php - - message: "#^Parameter \\#1 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\\\:\\:mapField\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\{fieldName\\: 'enum', enumType\\: 'Documents\\\\\\\\SuitNonBacked'\\} given\\.$#" + message: "#^Parameter \\#1 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\\\:\\:mapField\\(\\) expects array\\{type\\?\\: string, fieldName\\?\\: string, name\\?\\: string, strategy\\?\\: string, association\\?\\: int, id\\?\\: bool, isOwningSide\\?\\: bool, collectionClass\\?\\: class\\-string, \\.\\.\\.\\}, array\\{fieldName\\: 'enum', enumType\\: 'Documents\\\\\\\\SuitNonBacked'\\} given\\.$#" count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php index 8135cce28..1ad64db3a 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReadPreferenceTest.php @@ -46,7 +46,7 @@ public function testHintIsNotSetByDefault(): void /** @psalm-param ReadPreferenceTagShape[] $tags */ #[DataProvider('provideReadPreferenceHints')] - public function testHintIsSetOnQuery(int $readPreference, array $tags = []): void + public function testHintIsSetOnQuery(string $readPreference, array $tags = []): void { $this->skipTestIfSharded(User::class); @@ -68,9 +68,9 @@ public function testHintIsSetOnQuery(int $readPreference, array $tags = []): voi public static function provideReadPreferenceHints(): array { return [ - [ReadPreference::RP_PRIMARY, []], - [ReadPreference::RP_SECONDARY_PREFERRED, []], - [ReadPreference::RP_SECONDARY, [['dc' => 'east'], []]], + [ReadPreference::PRIMARY, []], + [ReadPreference::SECONDARY_PREFERRED, []], + [ReadPreference::SECONDARY, [['dc' => 'east'], []]], ]; } @@ -78,7 +78,7 @@ public function testDocumentLevelReadPreferenceIsSetInCollection(): void { $coll = $this->dm->getDocumentCollection(DocumentWithReadPreference::class); - self::assertSame(ReadPreference::RP_NEAREST, $coll->getReadPreference()->getMode()); + self::assertSame(ReadPreference::NEAREST, $coll->getReadPreference()->getModeString()); self::assertSame([['dc' => 'east']], $coll->getReadPreference()->getTagSets()); } @@ -88,7 +88,7 @@ public function testDocumentLevelReadPreferenceIsAppliedInQueryBuilder(): void ->createQueryBuilder() ->getQuery(); - $this->assertReadPreferenceHint(ReadPreference::RP_NEAREST, $query->getQuery()['readPreference'], [['dc' => 'east']]); + $this->assertReadPreferenceHint(ReadPreference::NEAREST, $query->getQuery()['readPreference'], [['dc' => 'east']]); } public function testDocumentLevelReadPreferenceCanBeOverriddenInQueryBuilder(): void @@ -98,14 +98,14 @@ public function testDocumentLevelReadPreferenceCanBeOverriddenInQueryBuilder(): ->setReadPreference(new ReadPreference('secondary', [])) ->getQuery(); - $this->assertReadPreferenceHint(ReadPreference::RP_SECONDARY, $query->getQuery()['readPreference']); + $this->assertReadPreferenceHint(ReadPreference::SECONDARY, $query->getQuery()['readPreference']); } /** @psalm-param ReadPreferenceTagShape[] $tags */ - private function assertReadPreferenceHint(int $mode, ReadPreference $readPreference, array $tags = []): void + private function assertReadPreferenceHint(string $mode, ReadPreference $readPreference, array $tags = []): void { self::assertInstanceOf(ReadPreference::class, $readPreference); - self::assertEquals($mode, $readPreference->getMode()); + self::assertEquals($mode, $readPreference->getModeString()); self::assertEquals($tags, $readPreference->getTagSets()); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php index 44c2c5c40..00468ea54 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php @@ -391,7 +391,7 @@ public function testPrimeReferencesInvokesPrimer(): void // Note: using a secondary read preference here can cause issues when using transactions // Using a primaryPreferred works just as well to check if the hint is passed on to the primer - $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY_PREFERRED); + $readPreference = new ReadPreference(ReadPreference::PRIMARY_PREFERRED); $this->dm->createQueryBuilder(User::class) ->field('account')->prime($primer) ->field('groups')->prime($primer) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ValidationTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ValidationTest.php index 042afd476..60f71fd1c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ValidationTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ValidationTest.php @@ -8,11 +8,7 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Tests\BaseTestCase; use Documents\SchemaValidated; - -use function MongoDB\BSON\fromJSON; -use function MongoDB\BSON\fromPHP; -use function MongoDB\BSON\toCanonicalExtendedJSON; -use function MongoDB\BSON\toPHP; +use MongoDB\BSON\Document; class ValidationTest extends BaseTestCase { @@ -41,15 +37,13 @@ public function testCreateUpdateValidatedDocument(): void ] } EOT; - $expectedValidatorBson = fromJSON($expectedValidatorJson); - $expectedValidator = toPHP($expectedValidatorBson, []); + $expectedValidator = Document::fromJSON($expectedValidatorJson)->toPHP(); $expectedOptions = [ 'validator' => $expectedValidator, 'validationLevel' => ClassMetadata::SCHEMA_VALIDATION_LEVEL_MODERATE, 'validationAction' => ClassMetadata::SCHEMA_VALIDATION_ACTION_WARN, ]; - $expectedOptionsBson = fromPHP($expectedOptions); - $expectedOptionsJson = toCanonicalExtendedJSON($expectedOptionsBson); + $expectedOptionsJson = Document::fromPHP($expectedOptions)->toCanonicalExtendedJSON(); $collections = $this->dm->getDocumentDatabase($cm->name)->listCollections(); $assertNb = 0; foreach ($collections as $collection) { @@ -58,8 +52,7 @@ public function testCreateUpdateValidatedDocument(): void } $assertNb++; - $collectionOptionsBson = fromPHP($collection->getOptions()); - $collectionOptionsJson = toCanonicalExtendedJSON($collectionOptionsBson); + $collectionOptionsJson = Document::fromPHP($collection->getOptions())->toCanonicalExtendedJSON(); self::assertJsonStringEqualsJsonString($expectedOptionsJson, $collectionOptionsJson); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php index adf80fb6a..976b83a1c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php @@ -35,6 +35,7 @@ use Documents\UserTyped; use Generator; use InvalidArgumentException; +use MongoDB\BSON\Document; use PHPUnit\Framework\Attributes\DataProvider; use ProxyManager\Proxy\GhostObjectInterface; use ReflectionClass; @@ -42,8 +43,6 @@ use stdClass; use function array_merge; -use function MongoDB\BSON\fromJSON; -use function MongoDB\BSON\toPHP; use function serialize; use function unserialize; @@ -78,7 +77,7 @@ public function testClassMetadataInstanceSerialization(): void $cm->setVersioned(true); $cm->setVersionField('version'); $validatorJson = '{ "$and": [ { "email": { "$regularExpression" : { "pattern": "@mongodb\\\\.com$", "options": "" } } } ] }'; - $cm->setValidator(toPHP(fromJSON($validatorJson))); + $cm->setValidator(Document::fromJSON($validatorJson)->toPHP()); $cm->setValidationAction(ClassMetadata::SCHEMA_VALIDATION_ACTION_WARN); $cm->setValidationLevel(ClassMetadata::SCHEMA_VALIDATION_LEVEL_OFF); self::assertIsArray($cm->getFieldMapping('phonenumbers')); @@ -110,7 +109,7 @@ public function testClassMetadataInstanceSerialization(): void self::assertEquals('lock', $cm->lockField); self::assertEquals(true, $cm->isVersioned); self::assertEquals('version', $cm->versionField); - self::assertEquals(toPHP(fromJSON($validatorJson)), $cm->getValidator()); + self::assertEquals(Document::fromJSON($validatorJson)->toPHP(), $cm->getValidator()); self::assertEquals(ClassMetadata::SCHEMA_VALIDATION_ACTION_WARN, $cm->getValidationAction()); self::assertEquals(ClassMetadata::SCHEMA_VALIDATION_LEVEL_OFF, $cm->getValidationLevel()); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/XmlDriverTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/XmlDriverTest.php index 988414384..8280a1df1 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/XmlDriverTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/XmlDriverTest.php @@ -7,6 +7,7 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver; use Doctrine\ODM\MongoDB\Mapping\MappingException; +use MongoDB\BSON\Document; use TestDocuments\AlsoLoadDocument; use TestDocuments\CustomIdGenerator; use TestDocuments\InvalidPartialFilterDocument; @@ -16,9 +17,6 @@ use TestDocuments\UserNonStringOptions; use TestDocuments\WildcardIndexDocument; -use function MongoDB\BSON\fromJSON; -use function MongoDB\BSON\toPHP; - class XmlDriverTest extends AbstractDriverTestCase { public function setUp(): void @@ -137,8 +135,7 @@ public function testValidationMapping(): void ] } EOT; - $expectedValidatorBson = fromJSON($expectedValidatorJson); - $expectedValidator = toPHP($expectedValidatorBson, []); + $expectedValidator = Document::fromJSON($expectedValidatorJson)->toPHP(); self::assertEquals($expectedValidator, $classMetadata->getValidator()); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php index 77252d76d..0f7d67308 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php @@ -675,7 +675,7 @@ public function testSetReadPreference(): void $readPreference = $qb->debug('readPreference'); self::assertInstanceOf(ReadPreference::class, $readPreference); - self::assertEquals(ReadPreference::RP_SECONDARY, $readPreference->getMode()); + self::assertEquals(ReadPreference::SECONDARY, $readPreference->getModeString()); self::assertEquals([['dc' => 'east']], $readPreference->getTagSets()); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php index 83f4dd559..a8dc4e656 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 MongoDB\BSON\Document; use MongoDB\Client; use MongoDB\Collection; use MongoDB\Database; @@ -43,8 +44,6 @@ use function array_map; use function assert; use function in_array; -use function MongoDB\BSON\fromJSON; -use function MongoDB\BSON\toPHP; /** * @psalm-import-type IndexMapping from ClassMetadata @@ -601,8 +600,7 @@ public function testUpdateDocumentValidator(array $expectedWriteOptions, ?int $m ] } EOT; - $expectedValidatorBson = fromJSON($expectedValidatorJson); - $expectedValidator = toPHP($expectedValidatorBson, []); + $expectedValidator = Document::fromJSON($expectedValidatorJson)->toPHP(); $database ->expects($this->once()) ->method('command') @@ -707,8 +705,7 @@ public function testCreateDocumentCollectionWithValidator(array $expectedWriteOp ] } EOT; - $expectedValidatorBson = fromJSON($expectedValidatorJson); - $expectedValidator = toPHP($expectedValidatorBson, []); + $expectedValidator = Document::fromJSON($expectedValidatorJson)->toPHP(); $options = [ 'capped' => false, 'size' => null,