Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP function reference to another function
    text
    copied!<p>Is it at all possible to do this in PHP: (Solved: Yes it is possible to do this.)</p> <pre><code>/// Unit test for class some below class some_test{ function test_foo1 () { $var-&gt;fileName = "name"; $var-&gt;fileLocation = "Location"; $var-&gt;fileType = "png"; $var-&gt;someFunction() = foo2(); // Do $var-&gt;someFunction = foo2(); } function foo2() { ....do something .... } } // Class Under test class some { function foo1(){ $var = getObjWith_someFunction(); if(isset($var)) $var-&gt;someFunction(); } } </code></pre> <p>I need to do this because when I am writing this unit test for a class where someFunction() is being called, it says that the someFunction() does not exist. So I need to create a dummy function in the test case.</p> <pre><code> Code example: class pictureManager { public function getPicture() { try { $picObj = getPicObj(1); } catch (Exception) { if (isset($picObj)) $picObj-&gt;showMessage(); } } public function getPicObj($id){ // Need to mock this function to return picClass object return new picClass($id); } } class picClass { public $id; public function __construct($id){ $this-&gt;id = id; } public function showMessage(){ echo "in this function"; } } Unit Test for class pictureManager: class pictureManager_test extends PHPUnit_Framework_TestCase { public function test_getPicture() { $fixture = $this-&gt;getMock('pictureManager', array('getPicObj')); $arg = 1; // Here I am creating the return obj on the fly. Can this be done? (Yes) $returnValue-&gt;id = 1; &gt;&gt;&gt; $returnValue-&gt;showMessage() = someThing(); // This is where I am stuck //Solution: $returnValue-&gt;showMessage = someThing(); //Remove the brackets (). Since I cant answer my question I am writing it here. $fixture::staticExpects($this-&gt;once()) -&gt;method('getPicObj') -&gt;with($arg) -&gt;will($this-&gt;returnValue($returnValue)); $fixture-&gt;getPicture(); } } </code></pre>
 

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