Mastering Mocks and Stubs with PHPUnit
summary
Explains how to choose between PHPUnit createMock and createStub: use mocks (createMock with expects/with/willReturn/willThrow) when tests must assert interactions like call count and arguments, and use stubs (createStub) when only supplying return values. Gives a concrete EC purchase-flow example: mock the external payment API to verify a single call with the amount, keep real DB queries and domain-rule checks in unit tests, use stubs for unimportant helper objects, and treat controllers as integration tests. Warns against over-stubbing, misusing mock expectations, and notes PHPUnit history and API changes (createStub introduced after PHPUnit 8 and newer versions tighten APIs).
AI-written summary · about
