Last active
April 4, 2017 13:32
-
-
Save gskema/4ea0cdf92c31418f59f6f811d8e96ff2 to your computer and use it in GitHub Desktop.
PHP entity object factory method for tests. PHPUnit/Symfony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BaseTestCase extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* Creates an arbitrary object with specified property values. | |
* | |
* @param string $className | |
* @param array $propertyValues | |
* | |
* @return object | |
*/ | |
protected function create(string $className, array $propertyValues) | |
{ | |
$object = new $className(); | |
foreach ($propertyValues as $propertyName => $propertyValue) { | |
$property = new \ReflectionProperty($className, $propertyName); | |
$property->setAccessible(true); | |
$property->setValue($object, $propertyValue); | |
} | |
return $object; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment