title: PHPUnit: Doing assertions on non-public variables
body: <p>Suppose I have a class with a private property and associated public getter and setter.  I want to test with PHPUnit that the property gets the correct value after the setter has been used or that the getter returns the correct property.</p>
  <p>Of course I can test the setter by using the getter to see that the object is storing the correct value, and vice versa for testing the getter.  However, this doesn't guarantee that the private property is the one being set.</p>
  <p>Say I had the following class. I created a property, getter and setter. But I made a typo in the property name, so the getter and the setter don't actually manipulate the property they're meant to manipulate</p>
  <pre><code>class SomeClass
  {
      private 
          $mane = NULL; // Was supposed to be $name but got fat-fingered!
  
      public function getName ()
      {
          return ($this -&gt; name);
      }
  
      public function setName ($newName)
      {
          $this -&gt; name = $newName;
          return ($this);
      }
  }
  </code></pre>
  <p>If I run the following test</p>
  <pre><code>public function testSetName ()
  {
      $this -&gt; object -&gt; setName ('Gerald');
      $this -&gt; assertTrue ($this -&gt; object -&gt; getName () == 'Gerald');
  }
  </code></pre>
  <p>I would get a pass.  However, something very bad has actually happened that I don't want.  When setName() is called, it actually creates a new property in the class with the name I thought my private property had, only the one that the setter creates is public!  I can demonstrate that with the following code:</p>
  <pre><code>$a  = new SomeClass;
  
  $a -&gt; setName('gerald');
  var_dump ($a -&gt; getName ());
  var_dump ($a -&gt; name);
  </code></pre>
  <p>It would output:</p>
  <blockquote>
  <p>string(6) &quot;gerald&quot;</p>
  <p>string(6) &quot;gerald&quot;</p>
  </blockquote>
  <p>Is there any way I can access the private properties from PHPUnit so I can write tests that make sure that the properties I think are being get and set actually really are being get and set?</p>
  <p>Or is there some other thing I should be doing in a test to catch problems like this without trying to get access to the private state of the object under test?</p>