Skip to content

Instantly share code, notes, and snippets.

@gskema
Last active April 4, 2017 13:32
Show Gist options
  • Save gskema/4ea0cdf92c31418f59f6f811d8e96ff2 to your computer and use it in GitHub Desktop.
Save gskema/4ea0cdf92c31418f59f6f811d8e96ff2 to your computer and use it in GitHub Desktop.
PHP entity object factory method for tests. PHPUnit/Symfony
<?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