title: PHPUnit - Mock return value of private method
body: <p>I have created a class with a function that makes a request to an external site using <code>file_get_contents()</code></p>
  
  <pre><code>class MyClass {
  
      public function emailExternal($email, $url){
  
          $url = $url;
          $data = array('email' =&gt; $email);
  
          $options = array(
              'http' =&gt; array(
                      'header'  =&gt; "Content-type: application/x-www-form-urlencoded\r\n",
                      'method'  =&gt; 'POST',
                      'content' =&gt; http_build_query($data),
              ),
          );
          $context  = stream_context_create($options);
  
          $result = $this-&gt;makeExternalRequest($url, $context);
  
          return $result;
  
      }
  
      private function makeExternalRequest($url, $context){
  
          return json_decode(file_get_contents($url, false, $context), true);
      }
  
  }
  </code></pre>
  
  <p>I have removed some of the function so i am only exposing what is required to understand my issue (This is why the function doesn't really do much)</p>
  
  <p>I need to test the emailExternal function but need to mock the return of the makeExternalRequest function. Just so i can simulate a successful/not-successful response.</p>
  
  <p>This is my attemot so far:</p>
  
  <pre><code>public function testEmailFromExternalSiteInvalidEmail(){
  
      $results = [
              'message' =&gt; 'test'
      ];
  
      $stub = $this-&gt;getMockBuilder('MyClass')
              -&gt;setMethods(['makeExternalRequest'])
              -&gt;getMock();
      $stub-&gt;expects($this-&gt;once())
              -&gt;method('makeExternalRequest')
              -&gt;withAnyParameters()
              -&gt;will($this-&gt;returnValue($results));
  
      $stub-&gt;emailExternal(['email' =&gt; 'test@test.co.uk'], 'http://test.com');
  
  }
  </code></pre>
  
  <p>The above works fine if the makeExternalRequest function is public or protected but no longer works whilst it is private. Its required i keep this function private but is also required that i test. Does anyone know of anything i could do?</p>