Files
archived-persistence/tests/Reflection/TypedNoDefaultReflectionPropertyTest.php
2025-11-02 00:45:31 +01:00

87 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Doctrine\Tests\Persistence\Reflection;
use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
use PHPUnit\Framework\TestCase;
class TypedNoDefaultReflectionPropertyTest extends TestCase
{
public function testGetValue(): void
{
$object = new TypedNoDefaultReflectionPropertyTestClass();
$reflProperty = new TypedNoDefaultReflectionProperty(TypedNoDefaultReflectionPropertyTestClass::class, 'test');
self::assertNull($reflProperty->getValue($object));
$object->test = 'testValue';
self::assertSame('testValue', $reflProperty->getValue($object));
unset($object->test);
self::assertNull($reflProperty->getValue($object));
}
public function testSetValueNull(): void
{
$reflection = new TypedNoDefaultReflectionProperty(TypedFoo::class, 'id');
$object = new TypedFoo();
$object->setId(1);
self::assertTrue($reflection->isInitialized($object));
$reflection->setValue($object, null);
self::assertNull($reflection->getValue($object));
self::assertFalse($reflection->isInitialized($object));
}
public function testSetValueNullOnNullableProperty(): void
{
$reflection = new TypedNoDefaultReflectionProperty(TypedNullableFoo::class, 'value');
$object = new TypedNullableFoo();
$reflection->setValue($object, null);
self::assertNull($reflection->getValue($object));
self::assertTrue($reflection->isInitialized($object));
self::assertNull($object->getValue());
}
}
class TypedNoDefaultReflectionPropertyTestClass
{
public string $test;
}
class TypedFoo
{
private int $id;
public function setId(mixed $id): void
{
$this->id = $id;
}
}
class TypedNullableFoo
{
private string|null $value;
public function setValue(mixed $value): void
{
$this->value = $value;
}
public function getValue(): mixed
{
return $this->value;
}
}