|
4 | 4 |
|
5 | 5 | namespace Doctrine\ODM\MongoDB\Tests\Functional; |
6 | 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; |
7 | 10 | use Doctrine\ODM\MongoDB\Mapping\MappingException; |
8 | 11 | use Doctrine\ODM\MongoDB\Tests\BaseTestCase; |
9 | | -use Documents\PropertyHooks\MappingVirtualProperty; |
10 | | -use Documents\PropertyHooks\User; |
11 | 12 |
|
12 | 13 | class PropertyHooksTest extends BaseTestCase |
13 | 14 | { |
@@ -91,8 +92,74 @@ public function testTriggerLazyLoadingWhenAccessingPropertyHooks(): void |
91 | 92 | public function testMappingVirtualPropertyIsNotSupported(): void |
92 | 93 | { |
93 | 94 | $this->expectException(MappingException::class); |
94 | | - $this->expectExceptionMessage('Mapping virtual property "fullName" on document "Documents84\PropertyHooks\MappingVirtualProperty" is not allowed.'); |
| 95 | + $this->expectExceptionMessage('Mapping virtual property "fullName" on document "' . __NAMESPACE__ . '\MappingVirtualProperty" is not allowed.'); |
95 | 96 |
|
96 | 97 | $this->dm->getClassMetadata(MappingVirtualProperty::class); |
97 | 98 | } |
98 | 99 | } |
| 100 | + |
| 101 | +#[Document(collection: 'property_hooks_user')] |
| 102 | +class User |
| 103 | +{ |
| 104 | + #[Id] |
| 105 | + public ?string $id; |
| 106 | + |
| 107 | + #[Field] |
| 108 | + public string $first { |
| 109 | + set { |
| 110 | + if (strlen($value) === 0) { |
| 111 | + throw new ValueError("Name must be non-empty"); |
| 112 | + } |
| 113 | + $this->first = $value; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + #[Field] |
| 118 | + public string $last { |
| 119 | + set { |
| 120 | + if (strlen($value) === 0) { |
| 121 | + throw new ValueError("Name must be non-empty"); |
| 122 | + } |
| 123 | + $this->last = $value; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public string $fullName { |
| 128 | + get => $this->first . " " . $this->last; |
| 129 | + set { |
| 130 | + [$this->first, $this->last] = explode(' ', $value, 2); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + #[Field] |
| 135 | + public string $language = 'de' { |
| 136 | + // Override the "read" action with arbitrary logic. |
| 137 | + get => strtoupper($this->language); |
| 138 | + |
| 139 | + // Override the "write" action with arbitrary logic. |
| 140 | + set { |
| 141 | + $this->language = strtolower($value); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[Document(collection: 'property_hooks_user')] |
| 147 | +class MappingVirtualProperty |
| 148 | +{ |
| 149 | + #[Id] |
| 150 | + public ?string $id; |
| 151 | + |
| 152 | + #[Field] |
| 153 | + public string $first; |
| 154 | + |
| 155 | + #[Field] |
| 156 | + public string $last; |
| 157 | + |
| 158 | + #[Field] |
| 159 | + public string $fullName { |
| 160 | + get => $this->first . " " . $this->last; |
| 161 | + set { |
| 162 | + [$this->first, $this->last] = explode(' ', $value, 2); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments