~/talk

Mastering Mocks and Stubs with PHPUnit

TalkinJapaneseat PHP Conference Japan 2024 · on Sunday, 2024-12-22· [php, testing]
slides:none on file

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

  1. 0:00Introduction
  2. 7:36Mocks vs stubs
  3. 11:58Practical use case
  4. 18:20Test doubles in classes
  5. 19:06Common misuse
  6. 22:46Anti-patterns in tests
  7. 23:52Better test doubles
  8. 24:14Summary
~/search
type to search 8,222 conferences, 162,397 speakers and 219,688 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
← →
on a talk: 5 seconds back / forward
0–9
on a talk: jump to 0%–90% of the recording
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.