title: Zend Framework/PHPUnit: How to Stub/Mock an object method that connects to a database?
body: <p>In my Zend Framework project, I have a form that I am testing. In my form, a multi-select element gets its options from a model, which retrieves them from the database.</p>
  
  <pre><code>public function init()
  {
      $this-&gt;addElement('select', 'Region_ID', array('multiOptions' =&gt; $this-&gt;getRegions()));
  }
  
  protected function getRegions()
  {
      $mapper = new Model_RegionMapper();
      return $mapper-&gt;getFormSelectData(); //this method will try to connect to a database (or get another object that will connect to the database)
  }
  </code></pre>
  
  <p>I tried copying the example in the PHPUnit documentation, but it doesn't seem to be working.</p>
  
  <pre><code>public function setUp()
  {
      $stub = $this-&gt;getMock('Model_RegionMapper');
      $stub-&gt;expects($this-&gt;any())
          -&gt;method('getFormSelectData')
          -&gt;will($this-&gt;returnValue(array('testdata')));
  }
  
  public function testFoo()
  {
      //this will fail
      $form = new My_Form();
  }
  </code></pre>
  
  <p>The test fails because it is trying to find a table in the database that doesn't exist. But I don't want it to connect to the database at all. How do I correctly stub/mock this method so that it doesn't call the database?</p>