2014-12-05 12:56:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Mapping;
|
|
|
|
|
|
|
|
use Doctrine\Instantiator\Instantiator;
|
|
|
|
use Doctrine\ORM\Mapping\ReflectionEmbeddedProperty;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\Tests\Models\Generic\BooleanModel;
|
2014-12-06 15:10:00 -05:00
|
|
|
use Doctrine\Tests\Models\Mapping\Entity;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\Tests\Models\Reflection\AbstractEmbeddable;
|
|
|
|
use Doctrine\Tests\Models\Reflection\ArrayObjectExtendingClass;
|
|
|
|
use Doctrine\Tests\Models\Reflection\ConcreteEmbeddable;
|
2017-03-31 17:20:10 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2014-12-05 12:56:34 +01:00
|
|
|
use ReflectionProperty;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for {@see \Doctrine\ORM\Mapping\ReflectionEmbeddedProperty}
|
|
|
|
*
|
|
|
|
* @covers \Doctrine\ORM\Mapping\ReflectionEmbeddedProperty
|
|
|
|
*/
|
2017-03-31 17:20:10 +01:00
|
|
|
class ReflectionEmbeddedPropertyTest extends TestCase
|
2014-12-05 12:56:34 +01:00
|
|
|
{
|
|
|
|
/**
|
2014-12-08 01:17:25 +01:00
|
|
|
* @param ReflectionProperty $parentProperty property of the embeddable/entity where to write the embeddable to
|
|
|
|
* @param ReflectionProperty $childProperty property of the embeddable class where to write values to
|
|
|
|
* @param string $embeddableClass name of the embeddable class to be instantiated
|
2014-12-05 12:56:34 +01:00
|
|
|
*
|
|
|
|
* @dataProvider getTestedReflectionProperties
|
|
|
|
*/
|
|
|
|
public function testCanSetAndGetEmbeddedProperty(
|
|
|
|
ReflectionProperty $parentProperty,
|
2014-12-08 01:17:25 +01:00
|
|
|
ReflectionProperty $childProperty,
|
|
|
|
$embeddableClass
|
2014-12-05 12:56:34 +01:00
|
|
|
) {
|
2014-12-08 01:17:25 +01:00
|
|
|
$embeddedPropertyReflection = new ReflectionEmbeddedProperty($parentProperty, $childProperty, $embeddableClass);
|
2014-12-05 12:56:34 +01:00
|
|
|
|
|
|
|
$instantiator = new Instantiator();
|
|
|
|
|
|
|
|
$object = $instantiator->instantiate($parentProperty->getDeclaringClass()->getName());
|
|
|
|
|
|
|
|
$embeddedPropertyReflection->setValue($object, 'newValue');
|
|
|
|
|
|
|
|
$this->assertSame('newValue', $embeddedPropertyReflection->getValue($object));
|
|
|
|
|
|
|
|
$embeddedPropertyReflection->setValue($object, 'changedValue');
|
|
|
|
|
|
|
|
$this->assertSame('changedValue', $embeddedPropertyReflection->getValue($object));
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:15:15 +01:00
|
|
|
/**
|
2014-12-08 01:17:25 +01:00
|
|
|
* @param ReflectionProperty $parentProperty property of the embeddable/entity where to write the embeddable to
|
|
|
|
* @param ReflectionProperty $childProperty property of the embeddable class where to write values to
|
|
|
|
* @param string $embeddableClass name of the embeddable class to be instantiated
|
2014-12-05 13:15:15 +01:00
|
|
|
*
|
|
|
|
* @dataProvider getTestedReflectionProperties
|
|
|
|
*/
|
|
|
|
public function testWillSkipReadingPropertiesFromNullEmbeddable(
|
|
|
|
ReflectionProperty $parentProperty,
|
2014-12-08 01:17:25 +01:00
|
|
|
ReflectionProperty $childProperty,
|
|
|
|
$embeddableClass
|
|
|
|
) {
|
|
|
|
$embeddedPropertyReflection = new ReflectionEmbeddedProperty($parentProperty, $childProperty, $embeddableClass);
|
2014-12-05 13:15:15 +01:00
|
|
|
|
|
|
|
$instantiator = new Instantiator();
|
|
|
|
|
|
|
|
$this->assertNull($embeddedPropertyReflection->getValue(
|
|
|
|
$instantiator->instantiate($parentProperty->getDeclaringClass()->getName())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-12-05 12:56:34 +01:00
|
|
|
/**
|
|
|
|
* Data provider
|
|
|
|
*
|
2014-12-08 01:17:25 +01:00
|
|
|
* @return ReflectionProperty[][]|string[][]
|
2014-12-05 12:56:34 +01:00
|
|
|
*/
|
|
|
|
public function getTestedReflectionProperties()
|
|
|
|
{
|
2016-12-07 23:33:41 +01:00
|
|
|
return [
|
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->getReflectionProperty(BooleanModel::class, 'id'),
|
|
|
|
$this->getReflectionProperty(BooleanModel::class, 'id'),
|
|
|
|
BooleanModel::class
|
2016-12-07 23:33:41 +01:00
|
|
|
],
|
2014-12-08 01:18:36 +01:00
|
|
|
// reflection on embeddables that have properties defined in abstract ancestors:
|
2016-12-07 23:33:41 +01:00
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->getReflectionProperty(BooleanModel::class, 'id'),
|
|
|
|
$this->getReflectionProperty(AbstractEmbeddable::class, 'propertyInAbstractClass'),
|
|
|
|
ConcreteEmbeddable::class
|
2016-12-07 23:33:41 +01:00
|
|
|
],
|
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->getReflectionProperty(BooleanModel::class, 'id'),
|
|
|
|
$this->getReflectionProperty(ConcreteEmbeddable::class, 'propertyInConcreteClass'),
|
|
|
|
ConcreteEmbeddable::class
|
2016-12-07 23:33:41 +01:00
|
|
|
],
|
2014-12-05 12:57:48 +01:00
|
|
|
// reflection on classes extending internal PHP classes:
|
2016-12-07 23:33:41 +01:00
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
|
|
|
|
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'privateProperty'),
|
|
|
|
ArrayObjectExtendingClass::class
|
2016-12-07 23:33:41 +01:00
|
|
|
],
|
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
|
|
|
|
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'protectedProperty'),
|
|
|
|
ArrayObjectExtendingClass::class
|
2016-12-07 23:33:41 +01:00
|
|
|
],
|
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
|
|
|
|
$this->getReflectionProperty(ArrayObjectExtendingClass::class, 'publicProperty'),
|
|
|
|
ArrayObjectExtendingClass::class
|
2016-12-07 23:33:41 +01:00
|
|
|
],
|
|
|
|
];
|
2014-12-05 12:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $className
|
|
|
|
* @param string $propertyName
|
|
|
|
*
|
|
|
|
* @return ReflectionProperty
|
|
|
|
*/
|
|
|
|
private function getReflectionProperty($className, $propertyName)
|
|
|
|
{
|
|
|
|
$reflectionProperty = new ReflectionProperty($className, $propertyName);
|
|
|
|
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
|
|
|
|
return $reflectionProperty;
|
|
|
|
}
|
|
|
|
}
|