One thing that I would recommend no matter what test structure you use is a hint type against the expected class or interface.
<?php class ExampleClass { private $OtherClass;
I am not an expert, but I see no problem with each class invoking a new one if the instance is not provided for a specific dependency. Of course, you can also use an approach in which you use configuration methods to configure dependencies.
<?php class class ExampleClass { private $OtherClass;
It’s not enough that the ExampleClass in the sample code doesn’t even define the doSomething method from ExampleDependencyClassTests , but if I understand correctly, it looks like Improving PHP does not force you to adopt a certain dependency injection style. You can write the test class as you wish, for example, if you applied the setter method approach mentioned above, you could change the example code layout to
<?php class ExampleDependencyClassTests extends \Enhance\TestFixture { public function verifyWithAMock() { $mock = \Enhance\MockFactory::createMock('ExampleDependencyClass'); $mock->addExpectation( \Enhance\Expect::method('getSomething') ->with(1, 'Arg2') ->returns('Something') ->times(1) ); $target = new ExampleClass(); $target->setExampleDependencyClass($mock); $result = $target->doSomething(); $mock->verifyExpectations(); } }
Of course, it would be wise to make appropriate changes to the ExampleClass !
<?php class ExampleClass { private $ExampleDependencyClass; public function addTwoNumbers($a, $b) { return $a + $b; } public function setExampleDependencyClass( ExampleDependencyClass $ExampleDependecyClass ) { $this->ExampleDependecyClass = $ExampleDependecyClass; } public function doSomething($someArg) { return 'Something'; } }
I have been working with PHPUnit quite a bit, and frankly, you will have to face the same problems with Mocks. My 2 cents, try to simulate your tests without Mocks, if possible;)
source share