mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Reflection > setPropertiesValues() method > sets values of properties in given object
This commit is contained in:
@@ -692,4 +692,25 @@ class Reflection
|
|||||||
$reflectionProperty->setAccessible(false);
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -525,6 +525,43 @@ class ReflectionTest extends BaseTestCase
|
|||||||
static::assertSame($newValue, $value);
|
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
|
* Provides invalid class and trait
|
||||||
*
|
*
|
||||||
@@ -607,4 +644,104 @@ class ReflectionTest extends BaseTestCase
|
|||||||
'Smith',
|
'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,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user