Last active
December 31, 2015 18:23
-
-
Save walkeralencar/d686b1a82501fc80e70c to your computer and use it in GitHub Desktop.
Trait for PHPUnit private methods tests
This file contains 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 | |
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); | |
} | |
} |
This file contains 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 | |
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