Okay In your testCase class you need to use the same namespace of your MyClass
class. The trick is to override built-in functions in your current namespace. So assuming your class looks like the following:
namespace My\Namespace;
class MyClass
{
public function methodExists() {
if (method_exists($this, 'someMethod')) {
return true;
} else {
return false;
}
}
}
Here is how the testCase class should look like:
namespace My\Namespace;//same namespace of the original class being tested
use \Mockery;
// Override method_exists() in current namespace for testing
function method_exists()
{
return ExampleTest::$functions->method_exists();
}
class ExampleTest extends \PHPUnit_Framework_TestCase
{
public static $functions;
public function setUp()
{
self::$functions = Mockery::mock();
}
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
self::$functions->shouldReceive('method_exists')->once()->andReturn(false);
$myClass = new MyClass;
$this->assertEquals($myClass->methodExists(), false);
}
}
It works perfect for me. Hope this helps.
trueOrFalse
you can checkmethod_exists
in code, because if method existisTrue
and always returntrue
you can simple replace this to singlemethod_exists()
but if you methodisTrue
returned evrything data you can simpleif(!method_exists) return false
or several data, or you can create abstract class and set this method as require for parrenting. p.s. sorry for my English. – BittisTrue
returns boolean (false or true) as its result. so question is, is there a chance to test it like it is, or refactoring is needed? in that case, yes i would probably need to get rid of thatmethod_exists
out of the method – Ker