Writing Unit Testable Code in Drupal 8
Click here to watch Writing Unit Testable Code in Drupal 8.
As a developer, you want to know that your code does what it's supposed to do, in every possible scenario. You also want to know your code can be reliably reused in contexts that you have not specifically foreseen. Writing unit-testable code means solving both of these problems at the same time: the unit tests you write for your class represent the first re-use of that code. They show you that outside of the context in which they were written, your code behaves exactly as intended for any given set of inputs to it.
But writing unit tests - tests that test your class or method in total isolation from the "normal" context in which it usually runs - can be very difficult if that code has hard dependencies on aspects of that context. This session will help you understand the fundamentals of writing unit-testable code, including an overview of the Dependency Injection pattern which is central to achieving this goal. It will use examples of contributed modules in Drupal 7 that can be made unit-testable in Drupal 8 by following the newly adopted best practices for designing classes that are loosely coupled from each other.
The session will also cover some essentials for writing PHPUnit tests for your beautifully designed, loosely-coupled classes.