title: How to test a Factory / Strategy implementation with PHPUnit
body: <p>I have a Factory class that returns a writer strategy based on the extension of a given file:</p>
  
  <pre><code>public static function getWriterForFile($file)
  {
      // create file info object
      $fileInfo = new \SplFileInfo($file);
      // check that an extension is present
      if ('' === $extension = $fileInfo-&gt;getExtension()) {
          throw new \RuntimeException(
              'No extension found in target file: ' . $file
          );
      }
      // build a class name using the file extension
      $className = 'MyNamespace\Writer\Strategy\\'
          . ucfirst(strtolower($extension))
          . 'Writer';
      // attempt to get an instance of the class
      if (!in_array($className, get_declared_classes())) {
          throw new \RuntimeException(
              'No writer could be found for the extension: ' . $extension
          );
      }
      $instance = new $className();
      $instance-&gt;setTargetFile($file);
      return $instance;
  }
  </code></pre>
  
  <p>All of my strategies implement the same interface, for example:</p>
  
  <pre><code>class CsvWriter implements WriterStrategyInterface
  {
      public function setTargetFile($file)
      {
          ...
      }
  
      public function writeLine(array $data)
      {
          ...
      }
  
      public function flush()
      {
          ...
      }
  }
  </code></pre>
  
  <p>I want to be able to test this method using a dummy extension so that my tests are not dependant on any particular strategy existing. I tried creating a mock for the interface with a set classname, but this does not seem to have declared the mock class name:</p>
  
  <pre><code>public function testExpectedWriterStrategyReturned()
  {
      $mockWriter = $this-&gt;getMock(
          'MyNamespace\Writer\Strategy\WriterStrategyInterface',
          array(),
          array(),
          'SssWriter'
      );
  
      $file = 'extension.sss';
  
      $writer = MyNamespace\Writer\WriterFactory::getWriterForFile($file);
  
      $this-&gt;assertInstanceOf('MyNamespace\Writer\Strategy\WriterStrategyInterface', $writer);;
  }
  </code></pre>
  
  <p>Is there any way for me to stage a mock writer strategy for the factory to load, or should I refactor the factory method to make it more testable?</p>