Good. In your testCase class, you need to use the same namespace of your class MyClass. The trick is to override the built-in functions in your current namespace. So, suppose your class looks like this:
namespace My\Namespace;
class MyClass
{
public function methodExists() {
if (method_exists($this, 'someMethod')) {
return true;
} else {
return false;
}
}
}
This is what the testCase class looks like:
namespace My\Namespace;
use \Mockery;
function method_exists()
{
return ExampleTest::$functions->method_exists();
}
class ExampleTest extends \PHPUnit_Framework_TestCase
{
public static $functions;
public function setUp()
{
self::$functions = Mockery::mock();
}
public function testBasicExample()
{
self::$functions->shouldReceive('method_exists')->once()->andReturn(false);
$myClass = new MyClass;
$this->assertEquals($myClass->methodExists(), false);
}
}
It works great for me. Hope this helps.
source
share