title: Testing code that uses get_class with PHPUnit mock objects
body: <p>Using PHPUnit and a mock object, I am trying to test some code that uses <code>get_class</code> to determine if an object is included by a filter or not.</p>
  
  <p>Here is the class to be tested:</p>
  
  <pre><code>class BlockFilter implements FilterInterface
  {
      private $classes;
  
      public function __construct(array $classes = array())
      {
          $this-&gt;classes = $classes;
      }
  
      public function isIncluded(NodeTraversableInterface $node)
      {
          if (Type::BLOCK != $node-&gt;getDocumentType()) {
              return false;
          }
  
          if (! empty($this-&gt;classes)) {
              /*** HERE IS THE PROBLEM: ***/
              return in_array(get_class($node), $this-&gt;classes);
          }
  
          return true;
      }
  }
  </code></pre>
  
  <p>Here is the method from my unit test:</p>
  
  <pre><code>public function testIfContainerBlockIsIncluded()
  {
      $containerBlock = $this-&gt;getMock('Pwn\ContentBundle\Document\ContainerBlock');
      $containerBlock-&gt;expects($this-&gt;any())-&gt;method('getDocumentType')-&gt;will($this-&gt;returnValue(Type::BLOCK));
  
      $filter = new BlockFilter(array('Pwn\ContentBundle\Document\ContainerBlock'));
      $this-&gt;assertTrue($filter-&gt;isIncluded($containerBlock));
  }
  </code></pre>
  
  <p>The mock object <code>$containerBlock</code> behaves like the real object <code>Pwn\ContentBundle\Document\ContainerBlock</code>; even code using <code>instanceof</code> works (because PHPUnit makes it a subclass of the real class, I believe).</p>
  
  <p>The code being tested uses <code>get_class</code> to get a string value of the class and compare it with an array of expected class names. Unfortunately, for the mock object, get_class returns something like this:</p>
  
  <pre><code>Mock_ContainerBlock_ac231064
  </code></pre>
  
  <p>(the _ac231064 suffix changes on each invocation).</p>
  
  <p>This causes my test to fail, so what are my options?</p>
  
  <ul>
  <li>Rework the code to avoid using get_class? This implies get_class should not be used when trying to write testable code.</li>
  <li>Use a real instance of the ContainerBlock class instead of a mock? This means we are effectively testing both classes at once.</li>
  <li>Some other awesomely clever trick that you're all going to suggest??? ;)</li>
  </ul>
  
  <p>Thanks for any help...</p>