How to unit test the methods of a class whose constructor take some arguments?
The basic idea behind unit test it to test a class / method itself, not dependencies of this class. In order to unit test you class A
you should not use real instances of your constructor arguments but use mocks instead. PHPUnit
provides nice way to create ones, so:
use A; class ATest extends PHPUnit_Framework_TestCase{ public function testGetSum{ $arg1Mock = $this->getMock('classB'); //use fully qualified class name $arg2Mock = $this->getMockBuilder('classC') ->disableOriginalConstructor() ->getMock(); //use mock builder in case classC constructor requires additional arguments $a = new A($arg1Mock, $arg2Mock); $this->assertEquals(3, $a->getSum(1,2)); } }
Note: If you won't be using mock's here but a real classB and classC instances it won't be unit test anymore - it will be a functional test
You could tell to PHPUnit which method you want to mock of a specified class, with the methods setMethods
. From the doc:
setMethods(array $methods)
can be called on the Mock Builder object to specify the methods that are to be replaced with a configurable test double. The behavior of the other methods is not changed. If you callsetMethods(null)
, then no methods will be replaced.
So you can construct your class without replacing any methods but bypass the construct as following (working) code:
public function testGetSum(){ $a = $this->getMockBuilder(A::class) ->setMethods(null) ->disableOriginalConstructor() ->getMock(); $this->assertEquals(3, $a->getSum(1,2));}
Hope this help