mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Utilities > Reflection > setPropertyValue() method > sets value of given property
This commit is contained in:
@@ -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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user