Current Location: Home> Latest Articles> PHP Object-Oriented Programming in Practice: Complete Guide to Unit Testing and Mocking

PHP Object-Oriented Programming in Practice: Complete Guide to Unit Testing and Mocking

M66 2025-10-31

Introduction

Object-Oriented Programming (OOP) is a powerful paradigm in PHP that allows developers to write code that is readable, maintainable, and reusable. In OOP practice, testing and mocking are essential steps to ensure the stability and reliability of the code.

The Importance of Testing

Testing verifies whether the code behaves as expected. In object-oriented development, testing usually includes the following types:

  • Unit Testing: Tests individual classes or methods.
  • Integration Testing: Checks interactions between classes.
  • End-to-End Testing: Tests the complete workflow and functionality of the application.

Understanding Mocking

Mocking is the technique of creating simulated objects, allowing methods to be tested without actually invoking underlying dependencies. This is especially useful for external services or hard-to-access dependencies, ensuring tests remain independent and stable.

Using PHPUnit for Testing

PHPUnit is a widely used testing framework in PHP. It provides various assertion methods to validate expected results and supports mock objects. Here is a simple example showing how to use PHPUnit for unit testing:

use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{
    public function testCreateUser()
    {
        $user = new User('John', 'Doe');
        $this->assertEquals('John', $user->getFirstName());
        $this->assertEquals('Doe', $user->getLastName());
    }
}

Using Prophecy for Mocking

Prophecy is a powerful PHP mocking library that allows you to create mock objects and configure their expected behavior. The following example demonstrates how to use Prophecy for mocking dependencies:

use Prophecy\PhpUnit\ProphecyTrait;

class DatabaseTest extends TestCase
{
    use ProphecyTrait;

    public function testDatabaseConnection()
    {
        $database = $this->prophesize(Database::class);
        $database->connect()->shouldBeCalledOnce();

        $model = new Model($database->reveal());
        $model->connect();

        $database->connect()->shouldHaveBeenCalledOnce();
    }
}

Practical Example

Suppose we have a UserService class that depends on UserRepository to fetch user data:

  • We use PHPUnit to unit test UserService to ensure the returned results are correct.
  • We use Prophecy to mock UserRepository, controlling method calls and return values.

This approach allows us to validate UserService behavior without accessing the real database, improving test stability and maintainability.

Conclusion

Testing and mocking are indispensable practices in PHP object-oriented programming. They ensure code correctness while enhancing maintainability and reusability. By leveraging tools like PHPUnit and Prophecy, developers can write robust, well-tested OOP code.