title: PHPUnit testing with closures
body: <p>This came up trying to write a test for a method of a class that calls a mock method with a closure.  How would you verify the closure being called?</p>
  
  <p>I know that you would be able to assert that the parameter is an instance of <code>Closure</code>. But how would you check anything about the closure?</p>
  
  <p>For example how would you verify the function that is passed:</p>
  
  <pre><code> class SUT {
       public function foo($bar) {
           $someFunction = function() { echo "I am an anonymous function"; };
           $bar-&gt;baz($someFunction);
       }
   }
  
   class SUTTest extends PHPUnit_Framework_TestCase {
       public function testFoo() {
           $mockBar = $this-&gt;getMockBuilder('Bar')
                -&gt;setMethods(array('baz'))
                -&gt;getMock();
           $mockBar-&gt;expects($this-&gt;once())
                -&gt;method('baz')
                -&gt;with( /** WHAT WOULD I ASSERT HERE? **/);
  
           $sut = new SUT();
  
           $sut-&gt;foo($mockBar);
       }
   }
  </code></pre>
  
  <p>You can't compare two closures in PHP.  Is there a way in PHPUnit to execute the parameter passed in or in some way verify it?</p>