title: Code coverage when not testing protected/private methods with PHPUnit
body: <p>I know it's possible to test private/protected methods with PHPUnit using reflection or other workarounds.</p>
  
  <p>But most sources tell me that it's <strong>not</strong> best practice to write tests for private methods inside of a class. </p>
  
  <p>You are supposed to test the class as if it were a "black box" — you just test for expected behavior by comparing inputs with outputs disregarding the internal mechanics. Writing tests for classes should also notify you to unused private methods, by showing lack of code coverage.</p>
  
  <p>When I test my class and generate an HTML report, it shows the private methods as not covered by tests, even though the lines from which they are called are absolutely executed/covered. I know that the private methods are executed, because if they weren't the assertions on my class would not pass.</p>
  
  <p>Is this expected behavior in PHPUnit? Can I strive for 100% coverage, while still testing private methods only indirectly?</p>
  
  <p>Some simplified example code (using RestBundle in Symfony2):</p>
  
  <pre><code>class ApiController extends FOSRestController {
  
  /*
   * @REST\View()
   * @REST\Get("/api/{codes}")
   */
  public function getCodesAction($codes) {
      $view = new View();
      $view-&gt;setHeader('Access-Control-Allow-Origin', '*');
      $view-&gt;setData(array('type' =&gt; 'codes','data' =&gt; $this-&gt;_stringToArray($codes)));
      $view-&gt;setFormat('json')-&gt;setHeader('Content-Type', 'application/json');
      return $this-&gt;handleView($view);
  }
  
  private function _stringToArray($string){
      return explode('+',$string);
  }
  </code></pre>
  
  <p>The public function shows as "covered", the private function is indirectly covered but shows colored red in PHPUnit reports.</p>
  
  <p>Test:</p>
  
  <pre><code>class ApiControllerTest extends WebTestCase {
  
      public function test_getCodesAction(){
          $client = static::createClient();
          $client-&gt;request('GET', '/api/1+2+3');
          $this-&gt;assertContains('{"type": "codes", "data": [1,2,3]}', $client-&gt;getResponse()-&gt;getContent());
      }
  
  }
  </code></pre>
  
  <p>This is just a silly example of course, I could just as well include the explode() right there in the public function; But the controllers I'm writing tests for contain much more intricate and re-usable private functions which transform data in more complex ways (but are still side-effect free).</p>