Skip to content

Commit 23fed9f

Browse files
committed
Move PropertyHooks document classes to the PropertyHooksTest to prevent invalid document from polluting the other tests
1 parent 1420e8d commit 23fed9f

File tree

4 files changed

+73
-84
lines changed

4 files changed

+73
-84
lines changed

phpcs.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<exclude-pattern>tests/Hydrators*</exclude-pattern>
2121
<exclude-pattern>tests/PersistentCollections*</exclude-pattern>
2222

23+
<!-- Property Hooks aren't supported yet -->
24+
<exclude-pattern>tests/Tests/Functional/PropertyHooksTest.php</exclude-pattern>
25+
2326
<rule ref="Doctrine">
2427
<!-- Traversable type hints often end up as mixed[], so we skip them for now -->
2528
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification" />

tests/Documents/PropertyHooks/MappingVirtualProperty.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/Documents/PropertyHooks/User.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/Tests/Functional/PropertyHooksTest.php

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace Doctrine\ODM\MongoDB\Tests\Functional;
66

7+
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
8+
use Doctrine\ODM\MongoDB\Mapping\Annotations\Field;
9+
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
710
use Doctrine\ODM\MongoDB\Mapping\MappingException;
811
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
9-
use Documents\PropertyHooks\MappingVirtualProperty;
10-
use Documents\PropertyHooks\User;
1112

1213
class PropertyHooksTest extends BaseTestCase
1314
{
@@ -91,8 +92,74 @@ public function testTriggerLazyLoadingWhenAccessingPropertyHooks(): void
9192
public function testMappingVirtualPropertyIsNotSupported(): void
9293
{
9394
$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.');
9596

9697
$this->dm->getClassMetadata(MappingVirtualProperty::class);
9798
}
9899
}
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

Comments
 (0)