diff --git a/src/Utilities/Reflection.php b/src/Utilities/Reflection.php index e45ad09..c3fbafe 100644 --- a/src/Utilities/Reflection.php +++ b/src/Utilities/Reflection.php @@ -692,4 +692,25 @@ class Reflection $reflectionProperty->setAccessible(false); } } + + /** + * Sets values of properties in given object + * + * @param mixed $object Object that should contains given property + * @param array $propertiesValues Key-value pairs, where key - name of the property, value - value of the property + */ + public static function setPropertiesValues($object, array $propertiesValues) + { + /* + * No properties? + * Nothing to do + */ + if (empty($propertiesValues)) { + return; + } + + foreach ($propertiesValues as $property => $value) { + static::setPropertyValue($object, $property, $value); + } + } } diff --git a/tests/Utilities/ReflectionTest.php b/tests/Utilities/ReflectionTest.php index 660da65..8763716 100644 --- a/tests/Utilities/ReflectionTest.php +++ b/tests/Utilities/ReflectionTest.php @@ -525,6 +525,43 @@ class ReflectionTest extends BaseTestCase static::assertSame($newValue, $value); } + public function testSetPropertiesValuesWithoutProperties() + { + $object = new G(); + Reflection::setPropertiesValues($object, []); + + static::assertSame($object->getFirstName(), 'John'); + static::assertSame($object->getLastName(), 'Scott'); + } + + /** + * @param mixed $object Object that should contains given property + * @param array $propertiesValues Key-value pairs, where key - name of the property, value - value of the property + * + * @dataProvider provideObjectAndNotExistingProperties + */ + public function testSetPropertiesValuesUsingNotExistingProperties($object, array $propertiesValues) + { + $this->setExpectedException(NotExistingPropertyException::class); + Reflection::setPropertiesValues($object, $propertiesValues); + } + + /** + * @param mixed $object Object that should contains given property + * @param array $propertiesValues Key-value pairs, where key - name of the property, value - value of the property + * + * @dataProvider provideObjectAndPropertiesValues + */ + public function testSetPropertiesValues($object, array $propertiesValues) + { + Reflection::setPropertiesValues($object, $propertiesValues); + + foreach ($propertiesValues as $property => $value) { + $realValue = Reflection::getPropertyValue($object, $property); + static::assertSame($value, $realValue); + } + } + /** * Provides invalid class and trait * @@ -607,4 +644,104 @@ class ReflectionTest extends BaseTestCase 'Smith', ]; } + + /** + * Provides object and not existing properties + * + * @return Generator + */ + public function provideObjectAndNotExistingProperties() + { + yield[ + new \stdClass(), + [ + 'test' => 1, + ], + ]; + + yield[ + new A(), + [ + 'test' => 2, + ], + ]; + + yield[ + new B(), + [ + 'firstName' => '', + ], + ]; + } + + /** + * Provides object and its new values of properties + * + * @return Generator + */ + public function provideObjectAndPropertiesValues() + { + yield[ + new A(), + [ + 'count' => 123, + ], + ]; + + yield[ + new B(), + [ + 'name' => 'test test', + ], + ]; + + yield[ + new G(), + [ + 'firstName' => 'Jane', + ], + ]; + + yield[ + new G(), + [ + 'lastName' => 'Smith', + ], + ]; + + yield[ + new G(), + [ + 'firstName' => 'Jane', + 'lastName' => 'Brown', + ], + ]; + + yield[ + new F( + 123, + 'New York', + 'USA', + 'UnKnown' + ), + [ + 'gInstance' => new G(), + ], + ]; + + yield[ + new F( + 123, + 'New York', + 'USA', + 'UnKnown', + 'Mary', + 'Brown' + ), + [ + 'country' => 'Canada', + 'accountBalance' => 456, + ], + ]; + } }