DEV Community

Cover image for Stop using Mocking libraries
Marc Guinea
Marc Guinea

Posted on

2

Stop using Mocking libraries

This weekend I read a post titled "Don't use Mocking libraries" by Crell and I think it's a gold nugget.

I will just make an overview of it to show you how powerful and clever solution he is proposing us.

Mock libraries are great but we can achieve the same feature with just plain PHP giving an:

  • Easier reading and understanding of our code.
  • Better performance.
  • Less dependencies (oh yes please, less composer packages to keep up to date).

Example

We have a UserRepository we need to mock to make our Unit Tests without coupling with infrastructure layer (MySQL, SQLite...)

Let's show our scenario:


class User
{
    private string $id;

    public function __construct(string $id)
    {
        $this->id = $id;
    }

    public function id()
    {
        return $this->id;
    }
}

interface UserRepository
{
    public function findById(string $id): User;
}

// An example implementation of our repository.
// not required for our example.
class InMemoryUserRepository implements UserRepository
{
    private array $users;

    public function findById(string $id): User
    {
        return $this->users[$id];
    }
}
Enter fullscreen mode Exit fullscreen mode

Well, it's time to mock it to run our tests. We just need to create a class that mocks our UserRepository

$userRepositoryMock = new class implements UserRepository
{
    public function __construct() {}

    public function findById(string $id): User
    {
        switch($id) {
            case '1':
                return new User('1');
            case '2':
                return new User('2');
        }

        throw new \Exception('Not found');
    }
};
Enter fullscreen mode Exit fullscreen mode

Boom! just using an anonymous class and implementing our interface we have the UserRepository mocked and ready to test

public function itShouldTestSomething(): void
{
    $user = $userRepositoryMock->findById('1');

    $this->assertEquals('1', $user->id());
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

It's usual to look for libraries / packages that help us to implement some feature to avoid reinventing the wheel and save some time but this is (sometimes) not necessary and easily achieve with just pure PHP.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay