Better instance mocking in Function Mocker 02

An update on the work in progress.

One instance per replacement

The issue I’ve mentioned in my earlier post will be eliminated in the next release of function-mocker: replacing the same class two or more times in the same test will yield different instances of the replacement class on each call:

use tad\FunctionMocker\FunctionMocker as Test;

class SampleTest extends \PHPUnit_Framework_TestCase {

    public function instanceTest(){

        // before
        $mock1 = Test::replace('someClass::someMethod');
        $mock2 = Test::replace('someClass::anotherMethod');

        // would have passed before
        // will fail in new version
        $this->assertTrue($mock1 === $mock2);

    }

}

Meaningful symbol

While using the :: symbol to indicate both static and instance methods “works” it lacks clarity. In the new version function-mocker will allow instance methods to be replaced using the -> symbol:

use tad\FunctionMocker\FunctionMocker as Test;

class SampleTest extends \PHPUnit_Framework_TestCase {

    public function instanceTest(){

        // using the `->` symbol
        $mock = Test::replace('someClass->someMethod');

    }

    public function badReplacementTest(){

        // using the `->` symbol for static methods will
        // generate an exception
        $mock = Test::replace('someClass->staticMethod');

    }

}

All in favour of later test readability.

Chains

The weird idea I had for returning the same instance of the mock on each replacement was to allow for multiple methods to be mocked on the same object.
In the next release the possibility will be implemented differently

use tad\FunctionMocker\FunctionMocker as Test;

class SampleTest extends \PHPUnit_Framework_TestCase {

    public function instanceChainTest(){

        $mock = Test::replace('someClass')
            ->method('someMethod', 23)
            ->method('anotherMethod', function($arg){return $arg + 1;})
            ->method('thirdMethod')
            ->get();

        $this->assertEquals(23, $mock->someMethod());
        $this->assertEquals(24, $mock->anotherMethod(23));
        $this->assertNull($mock->thirdMethod());
    }

}

All the while maintaining the possibility to test for calls on the functions, the static methods and the mock objects. Phew.

I appreciate your input