/
githubmirror
/
symfony
Обзор
Документация
Войти
/
githubmirror
/
symfony
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
8.2
src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php
109 строк
4 KB
Nicolas Grekas
[Validator] Drop PHP < 8.4 compat in PropertyMetadata
29 апр 2026, 18:08
29 апр 2026, 18:08
6b9a42c
Код
Авторство
О чём код?
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Validator\Tests\Mapping; use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Component\Validator\Mapping\PropertyMetadata; use Symfony\Component\Validator\Tests\Fixtures\Entity_74; use Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy; use Symfony\Component\Validator\Tests\Fixtures\EntityWithHook; use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity; use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\EntityParent; class PropertyMetadataTest extends TestCase { private const CLASSNAME = Entity::class; private const CLASSNAME_74 = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74'; private const CLASSNAME_74_PROXY = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy'; private const PARENTCLASS = EntityParent::class; public function testInvalidPropertyName() { $this->expectException(ValidatorException::class); new PropertyMetadata(self::CLASSNAME, 'foobar'); } public function testGetPropertyValueFromPrivateProperty() { $entity = new Entity('foobar'); $metadata = new PropertyMetadata(self::CLASSNAME, 'internal'); $this->assertEquals('foobar', $metadata->getPropertyValue($entity)); } public function testGetPropertyValueFromOverriddenPrivateProperty() { $entity = new Entity('foobar'); $metadata = new PropertyMetadata(self::PARENTCLASS, 'data'); $this->assertTrue($metadata->isPublic($entity)); $this->assertEquals('Overridden data', $metadata->getPropertyValue($entity)); } public function testGetPropertyValueFromRemovedProperty() { $entity = new Entity('foobar'); // simulate out-of-sync metadata $metadata = 'O:52:"Symfony\Component\Validator\Mapping\PropertyMetadata":3:{s:5:"class";s:65:"Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity";s:4:"name";s:7:"missing";s:8:"property";s:7:"missing";}'; $metadata = unserialize($metadata); $this->expectException(ValidatorException::class); $metadata->getPropertyValue($entity); } public function testGetPropertyValueFromUninitializedProperty() { $entity = new Entity_74(); $metadata = new PropertyMetadata(self::CLASSNAME_74, 'uninitialized'); $this->assertNull($metadata->getPropertyValue($entity)); } public function testGetPropertyValueFromUninitializedPropertyShouldNotReturnNullIfMagicGetIsPresent() { $entity = new Entity_74_Proxy(); $metadata = new PropertyMetadata(self::CLASSNAME_74_PROXY, 'uninitialized'); $notUnsetMetadata = new PropertyMetadata(self::CLASSNAME_74_PROXY, 'notUnset'); $this->assertNull($notUnsetMetadata->getPropertyValue($entity)); $this->assertEquals(42, $metadata->getPropertyValue($entity)); } public function testGetPropertyValueFromUninitializedPropertyShouldUseHookIfPresent() { $entity = new EntityWithHook(); $entity->name = 'FOOBAR'; $metadata = new PropertyMetadata(EntityWithHook::class, 'withHook'); $this->assertEquals('foobar', $metadata->getPropertyValue($entity)); } public function testGetPropertyValueFromUninitializedPropertyShouldReturnNullIfHookFails() { $entity = new EntityWithHook(); // $withHook uses $entity->name but it's not initialized $metadata = new PropertyMetadata(EntityWithHook::class, 'withHook'); $this->assertNull($metadata->getPropertyValue($entity)); } public function testGetPropertyValueFromUninitializedPropertyWithHookReferencingItself() { $entity = new EntityWithHook(); $metadata = new PropertyMetadata(EntityWithHook::class, 'withHookOnSelf'); $this->assertNull($metadata->getPropertyValue($entity)); } }