Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One thing I would recommend no matter what testing framework you are using is type-hinting against the expected class, or interface.</p> <pre><code>&lt;?php class ExampleClass { private $OtherClass; // OtherClass instance public function __construct(OtherClass $OtherClass=null) { // ... } } </code></pre> <p>I'm no di expert, but I don't see the problem in letting each class call <em>new</em> if an instance isn't provided for a particular dependency. You could also of course take the approach where you use setter methods to configure dependencies.</p> <pre><code>&lt;?php class class ExampleClass { private $OtherClass; // OtherClass instance public function setOtherClass(OtherClass $OtherClass) { $this-&gt;OtherClass = $OtherClass; } } </code></pre> <p>It is lame that the <strong>ExampleClass</strong> in the sample code doesn't even define the <strong>doSomething</strong> method from the <strong>ExampleDependencyClassTests</strong>, but if I understand correctly it looks like <strong>Enhance PHP</strong> is not forcing you to take a particular style of dependency injection. You can write the test class however you want, so for example if you took the setter method approach I mentioned above, you could change the example mock code to</p> <pre><code>&lt;?php class ExampleDependencyClassTests extends \Enhance\TestFixture { public function verifyWithAMock() { $mock = \Enhance\MockFactory::createMock('ExampleDependencyClass'); $mock-&gt;addExpectation( \Enhance\Expect::method('getSomething') -&gt;with(1, 'Arg2') -&gt;returns('Something') -&gt;times(1) ); $target = new ExampleClass(); $target-&gt;setExampleDependencyClass($mock); $result = $target-&gt;doSomething(); $mock-&gt;verifyExpectations(); } } </code></pre> <p>Of course it would probly make sense to make the appropriate revisions to the <strong>ExampleClass</strong>!</p> <pre><code>&lt;?php class ExampleClass { private $ExampleDependencyClass; public function addTwoNumbers($a, $b) { return $a + $b; } public function setExampleDependencyClass( ExampleDependencyClass $ExampleDependecyClass ) { $this-&gt;ExampleDependecyClass = $ExampleDependecyClass; } public function doSomething($someArg) { return 'Something'; } } </code></pre> <p>I've worked with <strong>PHPUnit</strong> quite a bit, and honestly you'll have to face the same challenges with <em>Mocks</em> there. My 2 cents, try to model your tests without <em>Mocks</em> if possible ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload