Real-World PHP Testing
Real-World PHP Testing
Summary:
Introduction
The book "Real-World PHP Testing" (2018) by Chris Hartjes is not another dry reference on PHPUnit, but a practical guide written by a developer with many years of experience. Hartjes is known in the PHP community as an active advocate for testing and the author of the popular podcast PHP Town Hall. In this book, he shares real-world techniques and approaches that will help you integrate testing into your daily development, even when working with legacy code.
What the book is about
The book consists of 12 chapters covering all stages of testing PHP applications — from basic concepts to advanced techniques. The author not only explains how to write tests but also shows how to integrate them into the development process so that they bring real value rather than becoming a mere formality. The main focus is on unit testing, but integration tests, database testing, HTTP request testing, and working with external APIs are also covered.
Who this book is for
The book is aimed at PHP developers who already have a basic knowledge of the language and want to improve the quality of their code. It will be useful both for beginners just getting started with testing and for experienced programmers looking to organize their knowledge and learn new techniques. I especially recommend it to those working with legacy code — Hartjes devotes significant attention to strategies for testing legacy systems.
What the reader will learn
- Testing fundamentals: the difference between unit, integration, and functional tests, the FIRST principles (Fast, Isolated, Repeatable, Self-validating, Timely).
- PHPUnit: installation, configuration, writing test classes, using assertions, data providers, dependency injection in tests.
- Mock objects and stubs: creating stubs using PHPUnit and the Mockery library, testing complex dependencies.
- Database testing: strategies for working with test databases, fixtures, transactions, testing queries and models.
- HTTP and API testing: emulating HTTP requests, testing REST controllers, working with Guzzle and other HTTP clients.
- Testing legacy code: refactoring for testability, extracting dependencies, using seam patterns.
- CI/CD and automation: integrating tests into Jenkins, Travis CI, GitLab CI, setting up automatic execution on pushes.
- TDD (Test-Driven Development): the "red-green-refactor" cycle, practical examples.
- Code coverage: analyzing coverage reports, setting quality thresholds.
- Best practices: organizing tests, naming conventions, maintaining readability, avoiding brittle tests.
Strengths
Practical focus. Each chapter contains real code examples that can be immediately applied in a project. The author does not get carried away with theory but shows how to solve specific problems.
Focus on real-world scenarios. Hartjes examines situations every developer faces: testing legacy code, working with external services, error handling. This makes the book especially valuable for those not working on "ideal" projects.
Clear structure. The material is presented logically: from simple to complex. Each chapter ends with a summary and a list of key takeaways, helping to consolidate knowledge.
Modern tools. The book covers current versions of PHPUnit (7.x), Mockery, PHPStan, and other tools, making it useful even several years after publication.
Humor and light style. The author writes vividly, with a touch of self-irony, making the reading engaging. Testing is not the most fun topic, but Hartjes manages to keep the reader interested.