|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Functional; |
| 6 | + |
| 7 | +use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; |
| 8 | +use Doctrine\ODM\MongoDB\Mapping\Annotations\Field; |
| 9 | +use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; |
| 10 | +use Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceOne; |
| 11 | +use Doctrine\ODM\MongoDB\Tests\BaseTestCase; |
| 12 | + |
| 13 | +class ReadOnlyPropertiesTest extends BaseTestCase |
| 14 | +{ |
| 15 | + public function testReadOnlyDocument(): void |
| 16 | + { |
| 17 | + $configuration = $this->dm->getConfiguration(); |
| 18 | + if (! $configuration->isNativeLazyObjectEnabled() && ! $configuration->isLazyGhostObjectEnabled()) { |
| 19 | + $this->markTestSkipped('Read-only properties are not supported by the legacy Proxy Manager. https://github.com/FriendsOfPHP/proxy-manager-lts/issues/26'); |
| 20 | + } |
| 21 | + |
| 22 | + $document = new ReadOnlyProperties('Test Name'); |
| 23 | + $document->onlyRead = new ReadOnlyProperties('Nested Name'); |
| 24 | + $this->dm->persist($document); |
| 25 | + $this->dm->persist($document->onlyRead); |
| 26 | + $this->dm->flush(); |
| 27 | + $this->dm->clear(); |
| 28 | + |
| 29 | + $document = $this->dm->getRepository(ReadOnlyProperties::class)->find($document->id); |
| 30 | + $this->assertEquals('Test Name', $document->name); |
| 31 | + $this->assertEquals('Nested Name', $document->onlyRead->name); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[Document] |
| 36 | +class ReadOnlyProperties |
| 37 | +{ |
| 38 | + #[Id] |
| 39 | + public readonly string $id; // @phpstan-ignore property.uninitializedReadonly (initialized by reflection) |
| 40 | + |
| 41 | + #[Field] |
| 42 | + public readonly string $name; |
| 43 | + |
| 44 | + #[ReferenceOne(targetDocument: self::class)] |
| 45 | + public ?self $onlyRead; |
| 46 | + |
| 47 | + public function __construct(string $name) |
| 48 | + { |
| 49 | + $this->name = $name; |
| 50 | + } |
| 51 | +} |
0 commit comments