title: Mock returning mock call to member function on non object
body: <p>I want to test this code</p>
  
  <pre><code>public function saveRecord( $recordID )
  {
      if (empty($recordID)) {
          return $this;
      }
      $this-&gt;model('Company')-&gt;save($this-&gt;getData());
      return $this;
  }
  </code></pre>
  
  <p>I have set up a test for the case where $recordID is not empty:</p>
  
  <pre><code>public function testSaveRecord2()
  {
      $dataToSave = array(
          'name' =&gt; 'value1',
          'project_stages' =&gt; 'value2'
      );
  
      $model = $this-&gt;getMockBuilder('Company')
          -&gt;disableOriginalConstructor()
          -&gt;setMethods(array('save') )
          -&gt;getMock();
      $model-&gt;expects($this-&gt;once())
          -&gt;method('save')
          -&gt;with($dataToSave);
  
      $subject = $this-&gt;getMockBuilder('Form_Company_Settings')
          -&gt;disableOriginalConstructor()
          -&gt;setMethods(array('getData', 'model') )
          -&gt;getMock();
      $subject-&gt;expects($this-&gt;once())-&gt;method('getData')-&gt;will($this-&gt;returnValue($dataToSave));
      $subject-&gt;expects($this-&gt;once())-&gt;method('model')-&gt;will($this-&gt;returnValue($model));
  
      $subject-&gt;saveRecord(1);
  }
  </code></pre>
  
  <p>The error I get is "Fatal error: Call to a member function save() on a non-object", ie. when $model is returned from the "model" function, it doesn't seem to have the save function in it.</p>
  
  <p>I also tried:</p>
  
  <pre><code>public function testSaveRecord2()
  {
      $dataToSave = array(
          'name' =&gt; 'value1',
          'project_stages' =&gt; 'value2'
      );
      $model = $this-&gt;getMockBuilder('Company')
          -&gt;disableOriginalConstructor()
          -&gt;setMethods(array('save') )
          -&gt;getMock();
      $model-&gt;expects($this-&gt;once())
          -&gt;method('save')
          -&gt;with($dataToSave);
  
      $subject = $this-&gt;getMockBuilder('Form_Company_Settings')
          -&gt;disableOriginalConstructor()
          -&gt;setMethods(array('getData') )
          -&gt;getMock();
      $subject-&gt;expects($this-&gt;once())-&gt;method('getData')-&gt;will($this-&gt;returnValue($dataToSave));
  
      $subject-&gt;saveRecord(1);
  }
  </code></pre>
  
  <p>hoping I don't need to mock model, but simply have it return a Company object to me. In this case I get 
  "Expectation failed for method name is equal to  when invoked 1 time(s).
  Method was expected to be called 1 times, actually called 0 times"</p>
  
  <p>I can't see why save would not be called here?</p>