Tell me more ×  I have a class I want to test. Here is the code:
class MyClass     function functionToTest() {          $class =
new Example();   In PHPUnit, can I use mocks/stubs to substitute
for the Example class?  In my test method:  class MyClassTest
extends PHPUnit_Framework_TestCase {      function
testFunctionTest() {         $testClass = new MyClass();
$result = $testClass->functionTest();    So instead of using the
actual "Example" class, can PHPUnit intervene here and use the
mock to represent "new Example()" ?  share|improve this question
add comment  1 Answer  up vote 2 down vote accepted  The best
solution would be to inject an Example instance into
functionToTest() method:  function functionToTest( Example
$class )  Then you'll be able to mock it in your unit tests:
function testFunctionTest() {     $testClass = new MyClass();
$class = $this->getMock( 'Example' );     $result =
$testClass->functionTest( $class );   But if this approach is
for some reason not an option for you, try