|
16 | 16 | use Symfony\Component\Validator\Mapping\PropertyMetadata; |
17 | 17 | use Symfony\Component\Validator\Tests\Fixtures\Entity_74; |
18 | 18 | use Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy; |
| 19 | +use Symfony\Component\Validator\Tests\Fixtures\EntityWithHook; |
19 | 20 | use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity; |
20 | 21 | use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\EntityParent; |
21 | 22 |
|
@@ -77,4 +78,39 @@ public function testGetPropertyValueFromUninitializedPropertyShouldNotReturnNull |
77 | 78 | $this->assertNull($notUnsetMetadata->getPropertyValue($entity)); |
78 | 79 | $this->assertEquals(42, $metadata->getPropertyValue($entity)); |
79 | 80 | } |
| 81 | + |
| 82 | + /** |
| 83 | + * @requires PHP 8.4 |
| 84 | + */ |
| 85 | + public function testGetPropertyValueFromUninitializedPropertyShouldUseHookIfPresent() |
| 86 | + { |
| 87 | + $entity = new EntityWithHook(); |
| 88 | + $entity->name = 'FOOBAR'; |
| 89 | + $metadata = new PropertyMetadata(EntityWithHook::class, 'withHook'); |
| 90 | + |
| 91 | + $this->assertEquals('foobar', $metadata->getPropertyValue($entity)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @requires PHP 8.4 |
| 96 | + */ |
| 97 | + public function testGetPropertyValueFromUninitializedPropertyShouldReturnNullIfHookFails() |
| 98 | + { |
| 99 | + $entity = new EntityWithHook(); |
| 100 | + // $withHook uses $entity->name but it's not initialized |
| 101 | + $metadata = new PropertyMetadata(EntityWithHook::class, 'withHook'); |
| 102 | + |
| 103 | + $this->assertNull($metadata->getPropertyValue($entity)); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @requires PHP 8.4 |
| 108 | + */ |
| 109 | + public function testGetPropertyValueFromUninitializedPropertyWithHookReferencingItself() |
| 110 | + { |
| 111 | + $entity = new EntityWithHook(); |
| 112 | + $metadata = new PropertyMetadata(EntityWithHook::class, 'withHookOnSelf'); |
| 113 | + |
| 114 | + $this->assertNull($metadata->getPropertyValue($entity)); |
| 115 | + } |
80 | 116 | } |
0 commit comments