title: Testing setting a private property: can we use the mock to set the value and reflect the property to check if value was set?
body: <p>I have both a "way of doing this" and "how is it done" question(s).</p>
  
  <pre><code>Class realClass {
  
       private $privateProperty;
  
       public function __construct($someArray, $someString) {
            // stuff
       }
  
       public function setProperty($someArray) {
            $this-&gt;privateProperty = $someArray;
       }
  }
  
  
  class testRealClass extends TestCase {
  
      // creating a mock because don't want to have tests depending on arguments passed to the constructor
      public function setUp(): void
      {
           $this-&gt;classToTest = $this-&gt;getMockBuilder(realClass::class)
               -&gt;disableOriginalConstructor()
               -&gt;setMethods(null)
               -&gt;getMock();
      }
  
      // testing the set of the variable. In order to know if it was set, without using a getter,
      // we probably need to use reflection
      public function testSetPrivateProperty() {
           $this-&gt;classToTest-&gt;setProperty([1,2,3]);
  
           // This will not work as the property is private
           // echo $this-&gt;classToTest-&gt;privateProperty;
  
           // Reflecting the mock, makes sense? How does this work?
           $reflect = new \ReflectionClass($this-&gt;classToTest);
           $property = $reflect-&gt;getProperty('privateProperty');
           $property-&gt;setAccessible(true);
           var_dump($property-&gt;getValue($this-&gt;classToTest));
      }
  }
  </code></pre>
  
  <p>When executing this I get a ReflectionException: Property privateProperty does not exist.</p>
  
  <p>If I do a print_r of the reflections->getProperties I can see the property there. </p>
  
  <p>I understand this would be easily achievable with just a getter in the realClass but take this as an exercise. </p>
  
  <p>Doest this make sense at all or this will not work because it is just wrong? </p>
  
  <p>Thank you everyone</p>
  
  <p>(Wrote the code here so if I'm missing ";", "(", etc, do ignore it :)</p>