|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket; |
| 6 | + |
| 7 | +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
| 8 | +use Doctrine\ODM\MongoDB\Tests\BaseTestCase; |
| 9 | +use Doctrine\ODM\MongoDB\Types\Type; |
| 10 | +use Doctrine\ODM\MongoDB\Types\Versionable; |
| 11 | +use MongoDB\BSON\Binary; |
| 12 | +use PHPUnit\Framework\Attributes\BackupGlobals; |
| 13 | + |
| 14 | +use function assert; |
| 15 | +use function is_int; |
| 16 | + |
| 17 | +#[BackupGlobals(true)] |
| 18 | +class GH2789Test extends BaseTestCase |
| 19 | +{ |
| 20 | + public function testVersionWithCustomType(): void |
| 21 | + { |
| 22 | + Type::addType(GH2789CustomType::class, GH2789CustomType::class); |
| 23 | + $doc = new GH2789VersionedUuid('original message'); |
| 24 | + |
| 25 | + $this->dm->persist($doc); |
| 26 | + $this->dm->flush(); |
| 27 | + |
| 28 | + $documents = $this->dm->getDocumentCollection(GH2789VersionedUuid::class)->find()->toArray(); |
| 29 | + self::assertCount(1, $documents); |
| 30 | + self::assertEquals(new Binary('1', 142), $documents[0]['version'], 'The version field should be stored using the custom type'); |
| 31 | + |
| 32 | + $doc->message = 'new message'; |
| 33 | + $this->dm->persist($doc); |
| 34 | + $this->dm->flush(); |
| 35 | + |
| 36 | + $documents = $this->dm->getDocumentCollection(GH2789VersionedUuid::class)->find()->toArray(); |
| 37 | + self::assertCount(1, $documents); |
| 38 | + self::assertEquals(new Binary('2', 142), $documents[0]['version'], 'The version field should be incremented and stored using the custom type'); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[ODM\Document(collection: 'gh2789_versioned_uuid')] |
| 43 | +class GH2789VersionedUuid |
| 44 | +{ |
| 45 | + #[ODM\Id] |
| 46 | + public string $id; |
| 47 | + |
| 48 | + #[ODM\Version] |
| 49 | + #[ODM\Field(type: GH2789CustomType::class)] |
| 50 | + public int $version; |
| 51 | + |
| 52 | + public function __construct( |
| 53 | + #[ODM\Field(type: 'string')] |
| 54 | + public string $message, |
| 55 | + ) { |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Custom type that stores an integer as a MongoDB Binary subtype 142. |
| 61 | + */ |
| 62 | +class GH2789CustomType extends Type implements Versionable |
| 63 | +{ |
| 64 | + public function convertToPHPValue($value): int |
| 65 | + { |
| 66 | + assert($value instanceof Binary); |
| 67 | + |
| 68 | + return (int) $value->getData(); |
| 69 | + } |
| 70 | + |
| 71 | + public function convertToDatabaseValue($value): Binary |
| 72 | + { |
| 73 | + assert(is_int($value)); |
| 74 | + |
| 75 | + return new Binary((string) $value, 142); |
| 76 | + } |
| 77 | + |
| 78 | + public function getNextVersion($current): int |
| 79 | + { |
| 80 | + if ($current === null) { |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + assert(is_int($current)); |
| 85 | + |
| 86 | + return $current + 1; |
| 87 | + } |
| 88 | +} |
0 commit comments