title: Trouble mocking an object in PHPUnit for my WordPress plugin
body: <p>In an effort to become a better developer I'm looking into automated tests for the WordPress plugin I'm creating. The problem is I've immediately hit a blocker that I've spend the better part of two days trying to fix without success.</p>
  <p>I've created the dumbest of classes inside a WordPress plugin</p>
  <pre><code>class Calc {
      public function add( int $a, int $b ): int {
          return $a + $b;
      }
  
      public function double( int $val ): int {
          return $this-&gt;add( $val, $val );
      }
  }
  </code></pre>
  <p>and an equally dumb test case</p>
  <pre><code>class CalcTest extends WP_UnitTestCase {
  
      public function test_add() {
          $instance = new \MyPlugin\Calc();
          $this-&gt;assertEquals( 6, $instance-&gt;add( 4, 2 ) );
      }
  
      public function test_double() {
          $mock = $this-&gt;createMock(
              \MyPlugin\Calc::class,
              [
                  'add',
              ]
          );
  
          $mock-&gt;expects( $mock-&gt;once() )
              -&gt;method('add')
              -&gt;willReturn( 10 );
  
          $this-&gt;assertEquals( 10, $mock-&gt;double( 5 ) );
      }
  }
  </code></pre>
  <p>So basically all I'm trying to do is mock the Calc class so that I can confirm the double method makes one call to the add method</p>
  <p>Trying to run the tests produces the following;</p>
  <pre><code>Time: 2.94 seconds, Memory: 38.50 MB
  
  There was 1 error:
  
  1) CalcTest::test_double
  Function ReflectionType::__toString() is deprecated
  </code></pre>
  <p>After a lot of Googling this seems to be an issue with running PHP &gt;7.1 and using PHPUnit 7 together. Trouble is WordPress requires v7.* of PHPUnit and my employer requires that I use PHP 7.4. The PHPUnit tickets I've seen suggest upgrading to v8 or 9 but WordPress doesn't currently allow that</p>
  <p>Am I understanding correctly that it's impossible to get these working together or have I just set up my test environment incorrectly? (for reference I set up the tests using <a href="https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/" rel="nofollow noreferrer">https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/</a> and then just added my composer autoloader)</p>
  <p>Any help is appeciated!</p>