Skip to content

Instantly share code, notes, and snippets.

@walkeralencar
Last active December 31, 2015 18:23
Show Gist options
  • Save walkeralencar/d686b1a82501fc80e70c to your computer and use it in GitHub Desktop.
Save walkeralencar/d686b1a82501fc80e70c to your computer and use it in GitHub Desktop.
Trait for PHPUnit private methods tests
<?php
namespace Test;
trait PrivateTrait
{
/**
* @param string|Object $obj Object
* @param string $name Method Name
* @param array $args
* @return mixed its based on method result;
*/
public function callPrivateMethod($obj, $name, array $args = array())
{
$class = new \ReflectionClass($obj);
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method->invokeArgs($obj, $args);
}
}
<?php
namespace Test;
class SampleTest extends \PHPUnit_Framework_TestCase
{
use PrivateTrait;
public function testFooGettingBar()
{
$result = $this->callPrivateMethod(new Foo(), 'getBar', ['1']);
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment