2018-10-31 19:15:07 +01:00
|
|
|
<?php
|
2018-10-31 20:39:41 +01:00
|
|
|
|
2018-10-31 19:15:07 +01:00
|
|
|
namespace App\Tests;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
abstract class AbstractTestCase extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Call protected/private method of a class.
|
2018-10-31 20:39:41 +01:00
|
|
|
*
|
2018-10-31 19:15:07 +01:00
|
|
|
* @see https://jtreminio.com/blog/unit-testing-tutorial-part-iii-testing-protected-private-methods-coverage-reports-and-crap/
|
|
|
|
*
|
2018-10-31 20:39:41 +01:00
|
|
|
* @param object &$object Instantiated object that we will run method on
|
2018-10-31 19:15:07 +01:00
|
|
|
* @param string $methodName Method name to call
|
2018-10-31 20:39:41 +01:00
|
|
|
* @param array $parameters array of parameters to pass into method
|
2018-10-31 19:15:07 +01:00
|
|
|
*
|
2018-10-31 20:39:41 +01:00
|
|
|
* @return mixed method return
|
2018-10-31 19:15:07 +01:00
|
|
|
*/
|
2018-11-01 22:34:26 +01:00
|
|
|
public function invokeMethod(object &$object, string $methodName, array $parameters = [])
|
2018-10-31 19:15:07 +01:00
|
|
|
{
|
2018-11-01 22:34:26 +01:00
|
|
|
$reflection = $this->getReflectionClassByObject($object);
|
2018-10-31 19:15:07 +01:00
|
|
|
$method = $reflection->getMethod($methodName);
|
|
|
|
$method->setAccessible(true);
|
2018-10-31 20:39:41 +01:00
|
|
|
|
2018-10-31 19:15:07 +01:00
|
|
|
return $method->invokeArgs($object, $parameters);
|
|
|
|
}
|
2018-11-01 22:34:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $object
|
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setProperty(object &$object, string $property, $value): void
|
|
|
|
{
|
|
|
|
$reflectionClass = $this->getReflectionClassByObject($object);
|
|
|
|
$reflectionProperty = $reflectionClass->getProperty($property);
|
|
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$reflectionProperty->setValue($object, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getReflectionClassByObject(object &$object): \ReflectionClass
|
|
|
|
{
|
|
|
|
return new \ReflectionClass(get_class($object));
|
|
|
|
}
|
2018-10-31 19:15:07 +01:00
|
|
|
}
|