Lesson 1 of 9 · 22m
Unit vs Integration: Draw the Boundary Once
The testing pyramid in PHP terms, why 'mock everything' suites lie, and how to decide whether a test needs a unit or an integration seam.
The worst PHP test suites do not fail because the team chose the wrong assertion syntax. They fail because nobody decided where the boundary was. Everything becomes a unit test with six mocks, or everything boots the framework and hits MySQL, and either way the suite becomes too fragile or too slow to trust.
A unit test proves a decision inside your process
A unit test exercises your domain or application logic without the framework container, network, filesystem, clock, or database. The subject under test is usually one class or a small cluster of objects.
final class PriceCalculatorTest extends TestCase
{
public function test_empty_order_totals_zero(): void
{
$total = (new PriceCalculator())->total([]);
self::assertSame('0', $total->amount());
self::assertSame('USD', $total->currency());
}
}That test answers a narrow question: did this object implement its rule correctly? It should run in microseconds.
An integration test proves a seam actually works
The filesystem adapter writes bytes. The SQL repository hydrates records. The HTTP client sends the headers your vendor requires. Framework bootstrapping wires routes to controllers. None of that is knowable from a pure unit test, because the interesting behavior is in the interaction.
final class UserRepositoryTest extends TestCase
{
public function test_find_returns_persisted_user(): void
{
$pdo = $this->sqliteConnection();
$repo = new PdoUserRepository($pdo);
$repo->save(new User('ada@example.invalid', 'Ada'));
$user = $repo->findByEmail('ada@example.invalid');
self::assertSame('Ada', $user->name());
}
}Notice the boundary: the repository test can still use SQLite if it only proves the repository's hydration logic. If the SQL uses MySQL-specific syntax, that convenience becomes a lie and the test needs MySQL.
Choose the smallest honest test
Ask these questions in order:
- Can the behavior be expressed without I/O? Unit test.
- Is the behavior SQL, filesystem, serialization, or protocol behavior? Integration test.
- Does the user journey matter more than any one seam? A small number of end-to-end tests.
Do not use a large test because it is easier to bootstrap. Large tests blur failure causes. When CheckoutTest fails, you want to know whether pricing changed, the gateway timed out, or the database schema drifted.
Field notes
- A test that boots Laravel or Symfony is not automatically an end-to-end test. Name the seam you are proving.
- If deleting a private method breaks tests, those tests are coupled to implementation rather than behavior.
- Unit tests should dominate because they are cheap and diagnostic, not because integration tests are impure.
- One meaningful end-to-end checkout test is worth more than twenty controller tests that all mock the same service differently.
Was this lesson helpful?