title: PhpUnit Testing stub method multiple return
body: <p>I am doing some PHPUnit testing with symfony2. I am having a problem with one particular test. </p>
  
  <p>I am testing a response form one of my Class of course one response is true one false. I have a Mock of my Database and I have a stub for one of the methods from my databaseRepository. </p>
  
  <p>The issue is that in one test I do a stub to a method with an valid array, second test i just want to query to be invalid null.</p>
  
  <p>my db MOck:</p>
  
  <pre><code>    //Setting up mock of database repository class
      $this-&gt;db = $this-&gt;getMockBuilder('DatabaseRepository')
          -&gt;disableOriginalConstructor()
          -&gt;getMock();
  
      $this-&gt;db-&gt;expects($this-&gt;any())
          -&gt;method('getRecord')
          -&gt;will($this-&gt;returnValue(self::$registrationRecord));
  
      $this-&gt;db-&gt;expects($this-&gt;any())
          -&gt;method('getRecord')
          -&gt;willReturn(null);
  </code></pre>
  
  <p>So I am trying to have two different expects but this obviousley does not work.....is it possible to have a stub method to have two different returns..?</p>
  
  <p>test1: </p>
  
  <pre><code>    &lt;?php
  
  
  class UnsubscribeRegistrationTemplateTest extends \PHPUnit_Framework_TestCase
  {
  
      /**
       * @var UnsubscribeRegistrationTemplate
       */
      protected $object;
  
      /**
       * @var ValidationClass
       */
      public $validate;
  
      /**
       * @var DatabaseRepository
       */
      public $db;
  
      //Database Record Mock
      public static $registrationRecord = array
      (
          'rowid' =&gt; '96',
          'unsubscription' =&gt; 'N',
          'updated' =&gt; 'BB'
      );
  
      /**
       *
       */
      protected function setUp()
      {
          //Setting up mock of validation class
          $this-&gt;validate = $this-&gt;getMockBuilder('ValidationClass')
              -&gt;disableOriginalConstructor()
              -&gt;getMock();
  
          $this-&gt;validate-&gt;expects($this-&gt;any())
              -&gt;method('validateInput')
              -&gt;willReturn(true);
  
          //Setting up mock of database repository class
          $this-&gt;db = $this-&gt;getMockBuilder('DatabaseRepository')
              -&gt;disableOriginalConstructor()
              -&gt;getMock();
  
          $this-&gt;db-&gt;expects($this-&gt;any())
              -&gt;method('getRegistrationRecord')
              -&gt;will($this-&gt;returnValue(self::$registrationRecord));
  
          $this-&gt;db-&gt;expects($this-&gt;any())
              -&gt;method('getRegistrationRecord')
              -&gt;will($this-&gt;returnValue(null));
  
          $this-&gt;db-&gt;expects($this-&gt;any())
              -&gt;method('setPreRegistrationEnquiryUnsubscriptionEnabled')
              -&gt;willReturn(true);
  
          $this-&gt;object = $this-&gt;createUnsubscribeRegistrationTemplateInstance();
      }
  
      /**
       * @return UnsubscribeRegistrationTemplate
       *
       */
      public function createUnsubscribeRegistrationTemplateInstance()
      {
          //initialize Unsubscribe Registration Template
          return new UnsubscribeRegistrationTemplate
          (
              $this-&gt;validate,
              $this-&gt;db
          );
      }
  
      /**
       * @param array $mapping
       * @return Request
       */
      public function createRequest(array $mapping)
      {
          $request = new Request();
  
          foreach ( $mapping as $k =&gt;$v)
          {
              $request-&gt;query-&gt;set($k, $v);
          }
  
          return $request;
      }
  
      /**
       *
       */
      public function testUnsubscribeRegistrationTemplateValidResponse()
      {
          $request = $this-&gt;createRequest(array(
              'registration_id' =&gt; '96',
              'source_channel' =&gt; 'BB'
          ));
  
          $response = new Response(
              true,
              'Unsubscription successful'
          );
  
          $this-&gt;assertEquals($response, $this-&gt;object-&gt;create($request));
      }
  
      /**
       *
       */
      public function testUnsubscribeRegistrationTemplateEmptyResponse()
      {
          $request = $this-&gt;createRequest(array(
              'registration_id' =&gt; '96',
              'source_channel' =&gt; 'BB'
          ));
  
          $response = new Response(
              false,
              'Registration Record Not Found.'
          );
  
          $this-&gt;assertEquals($response, $this-&gt;object-&gt;create($request));
      }
  
      /**
       *
       */
      public function testIsAlreadyRegisteredValidResponse()
      {
          //Testing record is already unsubscribed
          $registrationRecord = array(
              'unsubscription_enabled' =&gt; 'Y'
          );
  
          $this-&gt;assertTrue($this-&gt;object-&gt;isAlreadyUnsubscribed($registrationRecord));
      }
  
      /**
       *
       */
      public function testIsAlreadyRegisteredInValidResponse()
      {
          //Testing record not unsubscribed
          $registrationRecord = array(
              'unsubscription_enabled' =&gt; 'N'
          );
          $this-&gt;assertFalse($this-&gt;object-&gt;isAlreadyUnsubscribed($registrationRecord));
      }
  
      /**
       *
       */
      protected function tearDown()
      {
          unset($this-&gt;object);
      }
  
  }
  </code></pre>