Utilities > Reflection > setPropertyValue() method > sets value of given property

This commit is contained in:
Meritoo
2018-07-29 16:00:13 +02:00
parent 60d7b03cd7
commit 535ae65e5e
4 changed files with 154 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ use Generator;
use Meritoo\Common\Collection\Collection;
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
use Meritoo\Common\Exception\Reflection\NotExistingPropertyException;
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Test\Utilities\Reflection\A;
@@ -474,6 +475,37 @@ class ReflectionTest extends BaseTestCase
static::assertEquals('name', $property->getName());
}
/**
* @param mixed $object Object that should contains given property
* @param string $property Name of the property
*
* @dataProvider provideObjectAndNotExistingProperty
*/
public function testSetPropertyValueUsingNotExistingProperty($object, $property)
{
$this->setExpectedException(NotExistingPropertyException::class);
$object = new \stdClass();
Reflection::setPropertyValue($object, 'test', 'test test test');
}
/**
* @param mixed $object Object that should contains given property
* @param string $property Name of the property
* @param mixed $value Value of the property
*
* @dataProvider provideObjectPropertyAndValue
*/
public function testSetPropertyValue($object, $property, $value)
{
$oldValue = Reflection::getPropertyValue($object, $property);
Reflection::setPropertyValue($object, $property, $value);
$newValue = Reflection::getPropertyValue($object, $property);
static::assertNotSame($oldValue, $value);
static::assertSame($newValue, $value);
}
/**
* Provides invalid class and trait
*
@@ -501,4 +533,59 @@ class ReflectionTest extends BaseTestCase
[],
];
}
/**
* Provides object and name of not existing property
*
* @return Generator
*/
public function provideObjectAndNotExistingProperty()
{
yield[
new \stdClass(),
'test',
];
yield[
new A(),
'test',
];
yield[
new B(),
'firstName',
];
}
/**
* Provides object, name of property and value of the property
*
* @return Generator
*/
public function provideObjectPropertyAndValue()
{
yield[
new A(),
'count',
123,
];
yield[
new B(),
'name',
'test test',
];
yield[
new G(),
'firstName',
'Jane',
];
yield[
new G(),
'lastName',
'Smith',
];
}
}