Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting a private method in an abstract class extends the other one
    text
    copied!<p>I'm trying to test a private method in an abstract class. </p> <p>I've got three abstract classes:</p> <pre><code>abstract class AbstractClass1 extends AbstractClass2 { private function _privateFunction() { //method's body } } abstract class AbstractClass2 extends AbstractClass3 { public function __construct($param) { parent::__construct($param) } } abstract class AbstractClass3 { public function __construct($param = array()) { //something } } </code></pre> <p>The test class:</p> <pre><code>class AbstractClass1Test extends PHPUnit_Framework_TestCase { public function test_privateFunction() { $stub = $this-&gt;getMockForAbstractClass("AbstractClass1"); $class = new ReflectionClass($stub); $method = $class-&gt;getMethod("_privateFunction"); $method-&gt;setAccessible(true); //some assertings with $method-&gt;invoke($stub) } } </code></pre> <p>The test failed, because of the error:</p> <blockquote> <p>Missing argument 1 for AbstractClass2::__construct(), called in /usr/share/php/PHPUnit/Framework/MockObject/Generator.php on line 190 and defined</p> </blockquote> <p>AbstractClass2.php</p> <pre><code>public function __construct($param) </code></pre> <p>AbstractClass1.php</p> <pre><code>$classMock = $this-&gt;getMockForAbstractClass("AbstractClass1"); </code></pre> <p>Generator.php:190</p> <pre><code>if ($callOriginalConstructor &amp;&amp; !interface_exists($originalClassName, $callAutoload)) { if (count($arguments) == 0) { &lt;strong&gt;$mockObject = new $mock['mockClassName'];&lt;/strong&gt; } else { $mockClass = new ReflectionClass($mock['mockClassName']); $mockObject = $mockClass-&gt;newInstanceArgs($arguments); } } else ... </code></pre> <p>What do I wrong? Or how can I test my private function in this situation?</p>
 

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