Mock a method within a model method in CakePHP
<p>I am running <strong>CakePHP 2.8.X</strong>, and am trying to write a unit test for a Model function.</p>

<p>Let's call the model <code>Item</code>, and I'm trying to test its <code>getStatus</code> method.</p>

<p>However, that model makes a call to its <code>find</code> within the <code>getStatus</code> method.</p>

<p>So something like this:</p>

<pre class="lang-php prettyprint-override"><code>class Item extends Model
{
    public function getStatus($id) {
      // Calls our `$this-&gt;Item-find` method
      $item = $this-&gt;find('first', [
        'fields' =&gt; ['status'],
        'conditions' =&gt; ['Item.id' =&gt; $id]
      ]);

      $status = $item['status'];

      $new_status = null;

      // Some logic below sets `$new_status` based on `$status`
      // ...

      return $new_status;
    }
}
</code></pre>

<p>The logic to set "<code>$new_status</code>" is a bit complex, which is why I want to write some tests for it.</p>

<p>However, I'm not entirely sure how to override the <code>find</code> call within <code>Item::getStatus</code>.</p>

<p>Normally when I want to mock a Model's function, I use <a href="https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects.examples.SubjectTest.php" rel="nofollow"><code>$this-&gt;getMock</code></a> coupled with <code>method('find')-&gt;will($this-&gt;returnValue($val_here))</code>, but I don't want to completely mock my <code>Item</code> since I want to test its actual <code>getStatus</code> function.</p>

<p>That is, in my test function, I'm going to be calling:</p>

<pre class="lang-php prettyprint-override"><code>// This doesn't work since `$this-&gt;Item-&gt;getStatus` calls out to
// `$this-&gt;Item-&gt;find`, which my test suite doesn't know how to compute.
$returned_status = $this-&gt;Item-&gt;getStatus($id);
$this-&gt;assertEquals($expected_status, $returned_status);
</code></pre>

<p>So how do I communicate to my real <code>Item</code> model within my test that it should override its internal call to its <code>find</code> method?</p>