title: Mocking concrete method in abstract class using phpunit
body: <p>Are there any good ways to mock concrete methods in abstract classes using PHPUnit?</p>
  
  <p><strong>What I've found so far is:</strong></p>
  
  <ul>
  <li>expects()->will() works fine using abstract methods</li>
  <li>It does not work for concrete methods. The original method is run instead.</li>
  <li>Using mockbuilder and giving all the abstract methods and the concrete method to setMethods() works. However, it requires you to specify all the abstract methods, making the test fragile and too verbose.</li>
  <li>MockBuilder::getMockForAbstractClass() ignores setMethod().</li>
  </ul>
  
  <p><br/>
  Here are some unit tests examplifying the above points:</p>
  
  <pre><code>abstract class AbstractClass {
      public function concreteMethod() {
          return $this-&gt;abstractMethod();
      }
  
      public abstract function abstractMethod();
  }
  
  
  class AbstractClassTest extends PHPUnit_Framework_TestCase {
      /**
       * This works for abstract methods.
       */
      public function testAbstractMethod() {
          $stub = $this-&gt;getMockForAbstractClass('AbstractClass');
          $stub-&gt;expects($this-&gt;any())
                  -&gt;method('abstractMethod')
                  -&gt;will($this-&gt;returnValue(2));
  
          $this-&gt;assertSame(2, $stub-&gt;concreteMethod()); // Succeeds
      }
  
      /**
       * Ideally, I would like this to work for concrete methods too.
       */
      public function testConcreteMethod() {
          $stub = $this-&gt;getMockForAbstractClass('AbstractClass');
          $stub-&gt;expects($this-&gt;any())
                  -&gt;method('concreteMethod')
                  -&gt;will($this-&gt;returnValue(2));
  
          $this-&gt;assertSame(2, $stub-&gt;concreteMethod()); // Fails, concreteMethod returns NULL
      }
  
      /**
       * One way to mock the concrete method, is to use the mock builder,
       * and set the methods to mock.
       *
       * The downside of doing it this way, is that all abstract methods
       * must be specified in the setMethods() call. If you add a new abstract
       * method, all your existing unit tests will fail.
       */
      public function testConcreteMethod__mockBuilder_getMock() {
          $stub = $this-&gt;getMockBuilder('AbstractClass')
                  -&gt;setMethods(array('concreteMethod', 'abstractMethod'))
                  -&gt;getMock();
          $stub-&gt;expects($this-&gt;any())
                  -&gt;method('concreteMethod')
                  -&gt;will($this-&gt;returnValue(2));
  
          $this-&gt;assertSame(2, $stub-&gt;concreteMethod()); // Succeeds
      }
  
      /**
       * Similar to above, but using getMockForAbstractClass().
       * Apparently, setMethods() is ignored by getMockForAbstractClass()
       */
      public function testConcreteMethod__mockBuilder_getMockForAbstractClass() {
          $stub = $this-&gt;getMockBuilder('AbstractClass')
                  -&gt;setMethods(array('concreteMethod'))
                  -&gt;getMockForAbstractClass();
          $stub-&gt;expects($this-&gt;any())
                  -&gt;method('concreteMethod')
                  -&gt;will($this-&gt;returnValue(2));
  
          $this-&gt;assertSame(2, $stub-&gt;concreteMethod()); // Fails, concreteMethod returns NULL
      }
  }
  </code></pre>