title: PhpSpec. Call to a member function on a non-object when mock must return an object
body: <p>I'm new i phpspec and trying to create dummy converter for learning purposes, but I stuck with <strong>Call to a member function on a non-objec</strong>t error</p>
  
  <p>Here is my code: </p>
  
  <p><strong>Conver class</strong></p>
  
  <pre><code>private $supportedFormats = ['xml', 'json'];
  private $handler;
  
  public function __construct(ConvertHandler $handler)
  {
      $this-&gt;handler = $handler;
  }
  
  public function convert(array $data, $format)
  {
      if (!$this-&gt;shlouldBeSupportedFormat($format)) {
          throw new \Exception();
      }
      $converter = $this-&gt;handler-&gt;getConverter($format);
  
      return $converter-&gt;convert($data);
  }
  
  public function shlouldBeSupportedFormat($format)
  {
      if (!in_array($format, $this-&gt;supportedFormats)) {
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p><strong>ConvertHandler class</strong></p>
  
  <pre><code>public function getConverter($format)
  {
      $class = "AppBundle\Service\Converter\\".ucfirst($format).'Converter';
      if (class_exists($class)) {
          return new $class();
      } else {
          throw new \Exception();
      }
  }
  </code></pre>
  
  <p><strong>XmlConverter class</strong></p>
  
  <pre><code>public function convert(array $data)
  {
      return str_replace(PHP_EOL, '', TypeConverter::toXml($data));
  }
  </code></pre>
  
  <p>This code is working. It returns string in XML format.</p>
  
  <p>Now tests:</p>
  
  <p><strong>ConvertSpec class</strong></p>
  
  <pre><code>function let(ConvertHandler $handler) {
      $this-&gt;beConstructedWith($handler);
  }
  
  function its_convert_function_should_be_valid()
  {
      $this-&gt;shouldThrow('Exception')-&gt;during('convert', ['sadsa','xml']);
      $this-&gt;shouldThrow('Exception')-&gt;during('convert', [[],'bad_format']);
  }
  
  function it_should_check_if_support()
  {
      $this-&gt;shlouldBeSupportedFormat('xml')-&gt;shouldReturn(true);
      $this-&gt;shlouldBeSupportedFormat('bad_format')-&gt;shouldReturn(false);
  }
  
  function it_should_covert_to_xml(ConvertHandler $handler)
  {
      $this-&gt;convert(['test' =&gt; 'test'], 'xml')-&gt;shouldBeString();
      $handler-&gt;getConverter('xml')-&gt;shouldBeCalled();
  }
  </code></pre>
  
  <p><strong>ConvertHandlerSpec class</strong></p>
  
  <pre><code>function its_get_converter_shouldBe_valid()
  {
      $this-&gt;shouldThrow('Exception')-&gt;during('getConverter', ['wrong_format']);
  }
  
  function it_should_return_converter()
  {
      $this-&gt;getConverter('xml')-&gt;shouldReturnAnInstanceOf('AppBundle\Service\Converter\XmlConverter');
  }
  </code></pre>
  
  <p><strong>XmlConverterSpec class</strong></p>
  
  <pre><code>function it_should_implement()
  {
      $this-&gt;shouldImplement('AppBundle\Service\Converter\ConverterInterface');
  }
  function it_should_convert()
  {
      $this-&gt;convert(['test'=&gt;'test'])-&gt;shouldBeString();
      $this-&gt;convert(['test'=&gt;'test'])-&gt;shouldReturn(
          '&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;root&gt;&lt;test&gt;test&lt;/test&gt;&lt;/root&gt;'
          );
  }
  </code></pre>
  
  <p>Error when I run a tests: 
  <strong>PHP Fatal error:  Call to a member function convert() on a non-object in /var/www/phpSpecTest/src/AppBundle/Service/Convert.php on line 25</strong></p>
  
  <p>I'm already trying to fix it for second day, and looking for solution in the web. But still can't find a solution.Any suggest will be appreciated.</p>