Backend Development 6 min read

Understanding Mock, Dummy, Spy, Stub, and Fake Test Doubles in PHP Unit Testing

This article explains the differences and appropriate use cases of Mock, Dummy, Spy, Stub, and Fake test doubles in PHP unit testing, providing clear code examples to help developers write effective and maintainable tests.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding Mock, Dummy, Spy, Stub, and Fake Test Doubles in PHP Unit Testing

In the specialized field of unit testing, especially with PHP, developers use various test doubles—Mock, Dummy, Spy, Stub, and Fake—to simulate complex object behavior and ensure effective, accurate tests.

This article explains the subtle differences and appropriate use cases for each test double, providing concrete PHP code examples for practical implementation.

Mock

A Mock is a pre-programmed object with expectations that define a set of interaction rules, used to verify that specific calls occur as intended, ensuring component communication and reliability.

PHP example:

// Create a mock object for the Cache class
$cacheMock = $this->getMockBuilder(Cache::class)->getMock();

// Set expectation for the 'get' method to be called once and return a specific result
$cacheMock->expects($this->once())
    ->method('get')
    ->with('key')
    ->willReturn(['result' => 'fake_result']);

Dummy

A Dummy is a placeholder object passed around but never actually used, filling parameter lists to maintain structural completeness without providing functional value.

PHP example:

// Create a dummy object
$dummyObject = new DummyClass();

// Call a method with the dummy object as a parameter
$result = $testedObject->someMethod($dummyObject);

Spy

A Spy records interactions during a test, allowing verification that expected behaviors occurred, thus providing a mechanism for back‑trace validation.

PHP example:

// Create a spy for the Logger class
$loggerSpy = $this->getMockBuilder(Logger::class)->getMock();

// Expect the 'log' method to be called once
$loggerSpy->expects($this->once())
          ->method('log');

Stub

A Stub provides predefined responses for method calls, isolating the unit under test from its dependencies and focusing the test on specific logic.

PHP example:

// Create a stub for the Api class
$apiStub = $this->getMockBuilder(Api::class)->getMock();

// Stub the 'fetchData' method to return a specific value
$apiStub->method('fetchData')->willReturn(['data' => 'fake_data']);

Fake

A Fake implements a functional but simplified version of a real object, offering lightweight behavior suitable for testing scenarios that require realistic features without full complexity.

PHP example:

// Create a fake object for the EmailSender class
$emailSenderFake = new FakeEmailSender();

// Set a specific behavior for the 'send' method
$emailSenderFake->setSuccessfulSend(true);

Conclusion

Understanding the differences among Mock, Dummy, Spy, Stub, and Fake is crucial for writing efficient, maintainable unit tests; each test double has distinct scenarios and advantages that help developers isolate code units and achieve reliable test results.

By skillfully applying these tools, PHP developers can build robust test suites, improve code quality, reduce defects, and ensure stable, reliable applications.

unit testingMockPHPTest DoublesStub
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.