How (Not) to Write Testable Code
summary
Shows concrete PHP and JavaScript examples that break unit testing (missing return values, exit/redirect calls, creating dependencies inside methods, globals, singletons, deep object chains, and one 3–4 digit-condition monster method) and demonstrates fixes: replace with dependency injection and factories, wrap globals/exit/redirect in testable wrappers, add singleton reset, apply Law of Demeter, and split large methods. Uses real tooling: PHPUnit, Mockery, Tape, Sinon, Brain Monkey/WP Mock and publishes a GitHub repo with before/after pull requests.
AI-written summary · about
chapters (12)
- 0:00Start
- 8:37Example 2: Test
- 9:21Example 2: Mockery Version
- 9:36Example 2: JavaScript Version
- 10:35WP Mock Version
- 13:19Solution: Delegation
- 16:12Solution: Dependency Injection
- 17:28Solution: Use a Factory
- 20:01Issue: Using Unresettable Singletons
- 20:38Solution: Add a reset() Method
- 24:18Solution: Wrap Static Method
- 27:09Solution: Write Modular Code
