-
-
Notifications
You must be signed in to change notification settings - Fork 514
Convert the value of the "version" field using its known type #2813
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2789Test.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket; | ||
|
|
||
| use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
| use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
| use Doctrine\ODM\MongoDB\Types\Type; | ||
| use Doctrine\ODM\MongoDB\Types\Versionable; | ||
| use MongoDB\BSON\Binary; | ||
| use PHPUnit\Framework\Attributes\BackupGlobals; | ||
|
|
||
| use function assert; | ||
| use function is_int; | ||
|
|
||
| #[BackupGlobals(true)] | ||
| class GH2789Test extends BaseTestCase | ||
| { | ||
| public function testVersionWithCustomType(): void | ||
| { | ||
| Type::addType(GH2789CustomType::class, GH2789CustomType::class); | ||
| $doc = new GH2789VersionedUuid('original message'); | ||
|
|
||
| $this->dm->persist($doc); | ||
| $this->dm->flush(); | ||
|
|
||
| $documents = $this->dm->getDocumentCollection(GH2789VersionedUuid::class)->find()->toArray(); | ||
| self::assertCount(1, $documents); | ||
| self::assertEquals(new Binary('1', 142), $documents[0]['version'], 'The version field should be stored using the custom type'); | ||
|
|
||
| $doc->message = 'new message'; | ||
| $this->dm->persist($doc); | ||
| $this->dm->flush(); | ||
|
|
||
| $documents = $this->dm->getDocumentCollection(GH2789VersionedUuid::class)->find()->toArray(); | ||
| self::assertCount(1, $documents); | ||
| self::assertEquals(new Binary('2', 142), $documents[0]['version'], 'The version field should be incremented and stored using the custom type'); | ||
| } | ||
| } | ||
|
|
||
| #[ODM\Document(collection: 'gh2789_versioned_uuid')] | ||
| class GH2789VersionedUuid | ||
| { | ||
| #[ODM\Id] | ||
| public string $id; | ||
|
|
||
| #[ODM\Version] | ||
| #[ODM\Field(type: GH2789CustomType::class)] | ||
| public int $version; | ||
|
|
||
| public function __construct( | ||
| #[ODM\Field(type: 'string')] | ||
| public string $message, | ||
| ) { | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Custom type that stores an integer as a MongoDB Binary subtype 142. | ||
| */ | ||
| class GH2789CustomType extends Type implements Versionable | ||
| { | ||
| public function convertToPHPValue($value): int | ||
| { | ||
| assert($value instanceof Binary); | ||
|
|
||
| return (int) $value->getData(); | ||
| } | ||
|
|
||
| public function convertToDatabaseValue($value): Binary | ||
| { | ||
| assert(is_int($value)); | ||
|
|
||
| return new Binary((string) $value, 142); | ||
| } | ||
|
|
||
| public function getNextVersion($current): int | ||
| { | ||
| if ($current === null) { | ||
| return 1; | ||
| } | ||
|
|
||
| assert(is_int($current)); | ||
|
|
||
| return $current + 1; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a static method called on the instance. So it was possible to change the behavior for a custom type by overriding the method, but that's not how it's supposed to be used.
In fact, this method should be
final.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you suggesting that the
convertPHPToDatabaseValue()should befinal? If so, I agree and we should probably track that with a separate ticket for the next major version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's the idea. But a better approach would be to extract the static methods into a
TypeRegistry, similar to DBAL. #2818