|
37 | 37 | use Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularAndPluralProps; |
38 | 38 | use Symfony\Component\PropertyAccess\Tests\Fixtures\Ticket5775Object; |
39 | 39 | use Symfony\Component\PropertyAccess\Tests\Fixtures\TypeHinted; |
| 40 | +use Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty; |
40 | 41 | use Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty; |
41 | 42 | use Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedProperty; |
| 43 | +use Symfony\Component\VarExporter\ProxyHelper; |
42 | 44 |
|
43 | 45 | class PropertyAccessorTest extends TestCase |
44 | 46 | { |
@@ -225,7 +227,8 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAn |
225 | 227 | $this->expectException(UninitializedPropertyException::class); |
226 | 228 | $this->expectExceptionMessage('The method "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?'); |
227 | 229 |
|
228 | | - $object = new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty {}; |
| 230 | + $object = new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty { |
| 231 | + }; |
229 | 232 |
|
230 | 233 | $this->propertyAccessor->getValue($object, 'uninitialized'); |
231 | 234 | } |
@@ -958,4 +961,54 @@ public function testCastDateTimeImmutable() |
958 | 961 |
|
959 | 962 | $this->assertInstanceOf(\DateTime::class, $object->getDate()); |
960 | 963 | } |
| 964 | + |
| 965 | + public function testGetValuePropertyThrowsExceptionIfUninitializedWithoutLazyGhost() |
| 966 | + { |
| 967 | + $this->expectException(UninitializedPropertyException::class); |
| 968 | + $this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$uninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.'); |
| 969 | + |
| 970 | + $this->propertyAccessor->getValue(new UninitializedObjectProperty(), 'uninitialized'); |
| 971 | + } |
| 972 | + |
| 973 | + public function testGetValueGetterThrowsExceptionIfUninitializedWithoutLazyGhost() |
| 974 | + { |
| 975 | + $this->expectException(UninitializedPropertyException::class); |
| 976 | + $this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$privateUninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.'); |
| 977 | + |
| 978 | + $this->propertyAccessor->getValue(new UninitializedObjectProperty(), 'privateUninitialized'); |
| 979 | + } |
| 980 | + |
| 981 | + private function createUninitializedObjectPropertyGhost(): UninitializedObjectProperty |
| 982 | + { |
| 983 | + $class = 'UninitializedObjectPropertyGhost'; |
| 984 | + |
| 985 | + if (!class_exists($class)) { |
| 986 | + eval('class '.$class.ProxyHelper::generateLazyGhost(new \ReflectionClass(UninitializedObjectProperty::class))); |
| 987 | + } |
| 988 | + |
| 989 | + $this->assertTrue(class_exists($class)); |
| 990 | + |
| 991 | + return $class::createLazyGhost(initializer: function ($instance) { |
| 992 | + }); |
| 993 | + } |
| 994 | + |
| 995 | + public function testGetValuePropertyThrowsExceptionIfUninitializedWithLazyGhost() |
| 996 | + { |
| 997 | + $this->expectException(UninitializedPropertyException::class); |
| 998 | + $this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$uninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.'); |
| 999 | + |
| 1000 | + $lazyGhost = $this->createUninitializedObjectPropertyGhost(); |
| 1001 | + |
| 1002 | + $this->propertyAccessor->getValue($lazyGhost, 'uninitialized'); |
| 1003 | + } |
| 1004 | + |
| 1005 | + public function testGetValueGetterThrowsExceptionIfUninitializedWithLazyGhost() |
| 1006 | + { |
| 1007 | + $this->expectException(UninitializedPropertyException::class); |
| 1008 | + $this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$privateUninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.'); |
| 1009 | + |
| 1010 | + $lazyGhost = $this->createUninitializedObjectPropertyGhost(); |
| 1011 | + |
| 1012 | + $this->propertyAccessor->getValue($lazyGhost, 'privateUninitialized'); |
| 1013 | + } |
961 | 1014 | } |
0 commit comments