Files
archived-persistence/tests/RuntimeReflectionPropertyTest.php
Grégoire Paris 7e82b02509 Use Rector on tests
This is the configuration I used:

	<?php

	declare(strict_types=1);

	use Rector\Config\RectorConfig;

	return RectorConfig::configure()
	    ->withPaths([
		__DIR__ . '/tests',
	    ])
	    ->withPhpSets();
2026-01-24 11:52:03 +01:00

155 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Doctrine\Tests\Persistence;
use Closure;
use Doctrine\Persistence\Proxy;
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
class DummyMock
{
public function callGet(): void
{
}
public function callSet(): void
{
}
}
class RuntimeReflectionPropertyTest extends TestCase
{
#[TestWith(['test', 'testValue'])]
#[TestWith(['privateTest', 'privateTestValue'])]
public function testGetSetValue(string $name, string $value): void
{
$object = new RuntimeReflectionPropertyTestClass();
$reflProperty = new RuntimeReflectionProperty(RuntimeReflectionPropertyTestClass::class, $name);
self::assertSame($value, $reflProperty->getValue($object));
$reflProperty->setValue($object, 'changedValue');
self::assertSame('changedValue', $reflProperty->getValue($object));
}
/** @param class-string<RuntimeReflectionPropertyTestProxyMock> $proxyClass */
#[TestWith([RuntimeReflectionPropertyTestProxyMock::class])]
#[TestWith([RuntimeReflectionPropertyTestProxyMock::class])]
public function testGetValueOnProxyProperty(string $proxyClass): void
{
$getCheckMock = $this->createMock(DummyMock::class);
$getCheckMock->expects(self::never())->method('callGet');
$initializer = static function () use ($getCheckMock): void {
$getCheckMock->callGet();
};
$mockProxy = new $proxyClass($initializer);
$reflProperty = new RuntimeReflectionProperty($proxyClass, 'checkedProperty');
self::assertSame('testValue', $reflProperty->getValue($mockProxy));
unset($mockProxy->checkedProperty);
self::assertNull($reflProperty->getValue($mockProxy));
}
public function testSetValueOnProxyProperty(): void
{
$setCheckMock = $this->createMock(DummyMock::class);
$setCheckMock->expects(self::never())->method('callSet');
$initializer = static function () use ($setCheckMock): void {
$setCheckMock->callSet();
};
$mockProxy = new RuntimeReflectionPropertyTestProxyMock($initializer);
$reflProperty = new RuntimeReflectionProperty(RuntimeReflectionPropertyTestProxyMock::class, 'checkedProperty');
$reflProperty->setValue($mockProxy, 'newValue');
self::assertSame('newValue', $mockProxy->checkedProperty);
unset($mockProxy->checkedProperty);
$reflProperty->setValue($mockProxy, 'otherNewValue');
self::assertSame('otherNewValue', $mockProxy->checkedProperty);
}
}
/**
* Mock that simulates proxy property lazy loading
*
* @implements Proxy<object>
*/
class RuntimeReflectionPropertyTestProxyMock implements Proxy
{
private bool $initialized = false;
public string $checkedProperty = 'testValue';
/**
* {@inheritDoc}
*/
public function __construct(protected Closure|null $initializer = null)
{
}
public function __load(): void
{
}
public function __isInitialized(): bool
{
return $this->initialized;
}
/**
* {@inheritDoc}
*/
public function __setInitialized($initialized): void
{
$this->initialized = $initialized;
}
public function __get(string $name): mixed
{
if (! $this->initialized && $this->initializer !== null) {
($this->initializer)();
}
return $this->checkedProperty;
}
public function __set(string $name, mixed $value): void
{
if (! $this->initialized && $this->initializer !== null) {
($this->initializer)();
}
$this->checkedProperty = $value;
}
public function __isset(string $name): bool
{
if (! $this->initialized && $this->initializer !== null) {
($this->initializer)();
}
return isset($this->checkedProperty);
}
}
class RuntimeReflectionPropertyTestClass
{
public string $test = 'testValue';
private string $privateTest = 'privateTestValue';
public function getPrivateTest(): string|null
{
return $this->privateTest;
}
}