title: PHPUnit Stubbing Class methods declared as "final"
body: <p>I'm writing a unit test for a class method that calls another class's method using a mock, only the method that needs to be called is declared as final, so PHPUnit is unable to mock it. Is there a different approach I can take?</p>
  
  <p>example:</p>
  
  <p>class to be mocked</p>
  
  <pre><code>class Class_To_Mock
  {
      final public function needsToBeCalled($options)
      {
          ...
      }
  }
  </code></pre>
  
  <p>my test case</p>
  
  <pre><code>class MyTest extends PHPUnit_Framework_TestCase
  {
      public function testDoSomething()
      {
          $mock = $this-&gt;getMock('Class_To_Mock', array('needsToBeCalled'));
          $mock-&gt;expects($this-&gt;once())
               -&gt;method('needsToBeCalled')
               -&gt;with($this-&gt;equalTo(array('option'));
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Edit: If using the solution provided by Mike B and you have a setter/getter for the object you're mocking that does type checking (to ensure the correct object was passed into the setter), you'll need to mock the getter on the class you're testing and have it return the other mock.</p>
  
  <p>example:</p>
  
  <p>class to be mocked</p>
  
  <pre><code>class Class_To_Mock
  {
      final public function needsToBeCalled($options)
      {
          ...
      }
  }
  </code></pre>
  
  <p>mock</p>
  
  <pre><code>class Class_To_MockMock
  {
      public function needsToBeCalled($options)
      {
          ...
      }
  }
  </code></pre>
  
  <p>class to be tested</p>
  
  <pre><code>class Class_To_Be_Tested
  {
      public function setClassToMock(Class_To_Mock $classToMock)
      {
          ...
      }
  
      public function getClassToMock()
      {
          ...
      }
  
      public function doSomething()
      {
          $this-&gt;getClassToMock()
               -&gt;needsToBeCalled(array('option'));
      }
  }
  </code></pre>
  
  <p>my test case</p>
  
  <pre><code>class MyTest extends PHPUnit_Framework_TestCase
  {
      public function testDoSomething()
      {
          $classToTest = $this-&gt;getMock('Class_To_Be_Tested', array('getClassToMock'));
  
          $mock = $this-&gt;getMock('Class_To_MockMock', array('needsToBeCalled'));
  
          $classToTest-&gt;expects($this-&gt;any())
                      -&gt;method('getClassToMock')
                      -&gt;will($this-&gt;returnValue($mock));
  
          $mock-&gt;expects($this-&gt;once())
               -&gt;method('needsToBeCalled')
               -&gt;with($this-&gt;equalTo(array('option'));
  
          $classToTest-&gt;doSomething();
      }
  }
  </code></pre>