模拟在方法中间创建的对象

I know that creating an instance of a Class in the middle of a method it's a bad practice since it makes code hard to test. But I can't refactor the code, so I need to find a way to mock an Object created with new in the middle of a method under test.

Used Frameworks: PHPUnit, Mockery, WP_Mock

Example: Here I need to mock the get_second_string() method from the instance of the class ExternalClass

Class MyClass {

    function methodUnderTest($string) {
        $objToMock = new ExternalClass();
        $second_string = $objToMock->get_second_string();
        $final_string = $string . $second_string;
        return $final_string;
    }
}
Class TestMyClass extends PHPUnit_Framework_TestCase {
    public function setUp() {
    }
    public function tearDown() {
    }

    public function test_methodUnderTest() {
        $externalObject = $this->getMockBuilder('ExternalClass')
                               ->setMethods(['get_second_string'])
                               ->getMock;
        $externalObject->expects($this->once())
                       ->method('get_second_string')
                       ->willReturn(' two');
        $testObj = new MyClass();
        $this->assertEquals('one two', $testObj->methodUnderTest('one');
    }
}

If you really have no opportunity to refactor the code or do some appropriate integration testing, you might want to take a look at https://github.com/php-test-helpers/php-test-helpers#intercepting-object-creation and https://github.com/krakjoe/uopz/tree/PHP5

Still I think the code you make would profit a lot more from refactoring than monkey patching.

Besides, the refactoring does not need to be very heavy. You might do at least this:

class MyClass
{
    private $externalsFactory;

    public function __construct($externalsFactory){
        $this->externalsFactory = $externalsFactory;
    }

    public function methodUnderTest($str){
        $external = $this->externalsFactory->make();
        $second_string = $external->get_second_string();
        $finalString = $str.$second_string;
        return $finalString;
    }
}

class ExternalsFactory
{
    public function make(){
        return new ExternalClass();
    }
}

class ExternalClass
{
    public function get_second_string(){
        return 'some real stuff may be even from database or whatever else it could be';
    }
}

class MyClassTest extends PHPUnit_Framework_TestCase
{
    private $factoryMock;
    private $myClass;

    public function setUp(){
        $this->factoryMock = $this->getMockBuilder('ExternalsFactory')
                                  ->getMock();
        $this->myClass = new MyClass($this->factoryMock);
    }

    public function testMethodUnderTest(){
        $extenalMock = $this->createMock('ExternalClass');
        $extenalMock->method('get_second_string')
                    ->willReturn('second');
        $this->factoryMock->method('make')
                          ->willReturn($extenalMock);
        $this->assertSame('first-and-second', $this->myClass->methodUnderTest('first-and-'));
    }
}

IMHO there is no way to do such a thing. You should pass the object as parameter to the method.

You can not mock entire object. But with phpunit you can do something like this:

 $f = $this->getMockBuilder(<your_class>)->disableOriginalConstructor()
      ->setMethods(array(
          <mocked_method_1>, <mocked_method_2>
       ))->getMock();

This way, newly created object ommits constructor and you specify which method are going to behave normally and which you mock.

in testing you can specify what the method/s will return, like this:

$f->method(<mocked_method_1>)->willReturn(<dummy_data>);

using this, you will not test the mocked object in any way, but can test method which is creating the object..