Skip to content

Commit

Permalink
Remove usage of deprecated driver functionality (doctrine#2680)
Browse files Browse the repository at this point in the history
* Remove usages of deprecated BSON functions

* Remove usages of deprecated read preference functionality

* Update phpstan baseline
  • Loading branch information
alcaeus authored and GromNaN committed Oct 19, 2024
1 parent 73a4219 commit e6facbe
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 47 deletions.
7 changes: 3 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 3 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DOMDocument;
use InvalidArgumentException;
use LibXMLError;
use MongoDB\BSON\Document;
use MongoDB\Driver\Exception\UnexpectedValueException;
use SimpleXMLElement;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\<class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest\\.php\\:231\\>\\:\\: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\\<class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest\\.php\\:230\\>\\:\\: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\\<class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest\\.php\\:250\\>\\:\\: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\\<class@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest\\.php\\:249\\>\\:\\: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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -68,17 +68,17 @@ 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'], []]],
];
}

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());
}

Expand All @@ -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
Expand All @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 4 additions & 11 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@
use Documents\UserTyped;
use Generator;
use InvalidArgumentException;
use MongoDB\BSON\Document;
use PHPUnit\Framework\Attributes\DataProvider;
use ProxyManager\Proxy\GhostObjectInterface;
use ReflectionClass;
use ReflectionException;
use stdClass;

use function array_merge;
use function MongoDB\BSON\fromJSON;
use function MongoDB\BSON\toPHP;
use function serialize;
use function unserialize;

Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
9 changes: 3 additions & 6 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 MongoDB\BSON\Document;
use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Database;
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e6facbe

Please sign in to comment.