title: Php, is it good to mock database resultset?
body: <p>In my model:</p>
  
  <pre><code>class UsersModel
  {
      public function getUsers()
      {
          return SELECT id,username,rights FROM database
      }
  }
  </code></pre>
  
  <p>and the result is:</p>
  
  <pre><code>array{
      array{'id' =&gt; 1, 'username' =&gt; 'a', 'rights' =&gt; 4},
      array{'id' =&gt; 2, 'username' =&gt; 'b', 'rights' =&gt; 8},
      array{'id' =&gt; 4, 'username' =&gt; 'c', 'rights' =&gt; 7},
  }
  </code></pre>
  
  <p>this then gets formatted:</p>
  
  <pre><code>class Helper
  {
      function formatUsers (array $source)
      {
          foreach ($source as $item)
          {
               $item['usernameF'] = $item['username'].'.'.$item['id'];
          }
          return $source;
      }
  }
  </code></pre>
  
  <p>and the result is:</p>
  
  <pre><code>array{
      array{'id' =&gt; 1, 'username' =&gt; 'a', 'rights' =&gt; 4, 'usernameF' =&gt; 'a.1'},
      array{'id' =&gt; 2, 'username' =&gt; 'b', 'rights' =&gt; 8, 'usernameF' =&gt; 'b.2'},
      array{'id' =&gt; 4, 'username' =&gt; 'c', 'rights' =&gt; 7, 'usernameF' =&gt; 'c.4'}
  }
  </code></pre>
  
  <p>and outputting in the view:</p>
  
  <pre><code>foreach ($users as $item)
  {
      echo $item['usernameF'].' = '.$item['rights'].'&lt;br&gt;';
  }
  </code></pre>
  
  <p>so far so good, now lets test it.</p>
  
  <p>testModel:</p>
  
  <pre><code>assertEquals ($result, array{
      array{'id' =&gt; 1, 'username' =&gt; 'a', 'rights' =&gt; 4, 'usernameF' =&gt; 'a.1'},
      array{'id' =&gt; 2, 'username' =&gt; 'b', 'rights' =&gt; 8, 'usernameF' =&gt; 'b.2'},
      array{'id' =&gt; 4, 'username' =&gt; 'c', 'rights' =&gt; 7, 'usernameF' =&gt; 'c.4'}
  });
  </code></pre>
  
  <p>testHelper:</p>
  
  <p>here comes the difficulties. I can mock the <code>getUsers()</code> method to enforce a result.</p>
  
  <pre><code>$mock-&gt;setMock ($this-&gt;once()-&gt;method('getUsers')-&gt;willReturn(array{
      array{'id' =&gt; 1, 'username' =&gt; 'a', 'rights' =&gt; 4},
      array{'id' =&gt; 2, 'username' =&gt; 'b', 'rights' =&gt; 8},
      array{'id' =&gt; 4, 'username' =&gt; 'c', 'rights' =&gt; 7}
  });
  
  assertEquals ($result, array{
      array{'id' =&gt; 1, 'username' =&gt; 'a', 'rights' =&gt; 4, 'usernameF' =&gt; 'a.1'},
      array{'id' =&gt; 2, 'username' =&gt; 'b', 'rights' =&gt; 8, 'usernameF' =&gt; 'b.2'},
      array{'id' =&gt; 4, 'username' =&gt; 'c', 'rights' =&gt; 7, 'usernameF' =&gt; 'c.4'}
  }
  </code></pre>
  
  <p>BUT what if the DB scheme changes? What if I accidentally mistype a field, lets say "<code>usernmame</code>'. Result will be:</p>
  
  <pre><code>array{
      array{'id' =&gt; 1, 'usernmame' =&gt; 'a', 'rights' =&gt; 4},
      array{'id' =&gt; 2, 'usernmame' =&gt; 'b', 'rights' =&gt; 8},
      array{'id' =&gt; 4, 'usernmame' =&gt; 'c', 'rights' =&gt; 7},
  }
  </code></pre>
  
  <p>of course the test of model fails, so I change it too:</p>
  
  <p>testModel:</p>
  
  <pre><code>assertEquals ($result, array{
      array{'id' =&gt; 1, 'usernmame' =&gt; 'a', 'rights' =&gt; 4, 'usernameF' =&gt; 'a.1'},
      array{'id' =&gt; 2, 'usernmame' =&gt; 'b', 'rights' =&gt; 8, 'usernameF' =&gt; 'b.2'},
      array{'id' =&gt; 4, 'usernmame' =&gt; 'c', 'rights' =&gt; 7, 'usernameF' =&gt; 'c.4'}
  });
  </code></pre>
  
  <p>so all test passes - but in production, the view will fail with "wrong index; username"! So what would be the best approach?</p>