I write a lot of tests for all of my Drupal code. I try to live by Test Driven Development. It delivers cleaner code. It helps define your architecture without full implementation details. I also despise clicking and manually testing things.
However, testing in Drupal is still somewhat painful. Time. Since so many things require the database tests can get slow. Even the
Kernel test suite which uses a most minimally bootstrapped environment.
Because of this, I'm a huge fan of using mocked objects. If you're not familiar with mocking in tests, check out the Drupal.org documentation for using Prophecy and the PHPUnit documentation. The one "problem" was always having PhpStorm give warnings about type errors for mocked objects.
But, earlier this week, I finally installed the PHPUnit Enhancement plugin, and my mind is blown 🤩
Once you have called
$this->prophesize on a class or interface you will see autocomplete suggestions for the mocked item.
It gets even better. After you pick a method from that mocked item, it will autocomplete the Prophecy mocking methods. 😱
I don't know why I waited so long to install this.
My methods now understand the object returned by
reveal() matches the expected interfaces. I have also learned about some new things in Prophecy that I haven't used.