Connor Smyth

Psuedo

Pseudo is a system for mocking PHP's PDO database connections. When writing unit tests for PHP applications, one frequently has the need to test code that interacts with a database. However, in the true spirit of a unit test, the database should be abstracted, as we can assume with some degree of certainty that things like network links to the database server, the database connection drivers, and the database server and software itself are "going to work", and they are outside the scope of our unit tests.


See The Project Here

Pseudo is a system for mocking PHP’s PDO database connections. When writing unit tests for PHP applications, one frequently has the need to test code that interacts with a database. However, in the true spirit of a unit test, the database should be abstracted, as we can assume with some degree of certainty that things like network links to the database server, the database connection drivers, and the database server and software itself are “going to work”, and they are outside the scope of our unit tests.

Enter Pseudo. Pseudo allows you to have “fake” interactions with a database that produce predefined results every time. This has 2 main advantages over actually interacting with a database. First, it saves having to write data fixtures in another format, ensuring the data schema availability, loading the fixtures in, and then later cleaing and resetting them between tests. Second, and somewhat as a result of the first, tests can run significantly faster because they are essentially talking to an in-memory object structure rather than incurring all the overhead of connecting and interacting with an actual database.

The general idea is that Pseudo implements all the classes in the PDO system by inheriting from them and then overriding their methods. During your test, at the point where you would inject a PDO object into your data layer, you can now inject a Pseudo\Pdo object transparently, giving yourself 100% flexibility to control what your application now thinks is the database. In your unit test, you can express the mocks for your test in terms of SQL statements and arrays of result data.

Find the package on packagist.org

Documentation

Installation

composer require --dev pseudo/pseudo

Usage

Something you may want to test

<?php
class ObjectsModel {
    public function __construct(private readonly PDO $pdo)
    {
    }
    
    public function getObjectsByFoo(string $foo): array
    {
        $statement = $this->pdo->prepare('SELECT id FROM objects WHERE foo = :foo');

        $statement->execute(['foo' => 'bar']);
        
        $objects = $statement->fetchAll();
        
        if (!$objects) {
            throw new RuntimeException('Entity not found');
        }
        
        return $objects;
    }
}

Tests with Pseudo

<?php

class ObjectsModelTest extends \PHPUnit\Framework\TestCase {
    public function testGetObjectsByFoo(): void {
        $pdo = new Pseudo\Pdo();

        $objectsModel = new \ObjectsModel($pdo);

        $pdo->mock(
          "SELECT id FROM objects WHERE foo = :foo'", 
          ['foo' => 'bar'],
          [['id' => 1, 'foo' => 'bar']]
        );

        $objects = $objectsModel->getObjectsByFoo('bar');
        
        $this->assertEquals([['id' => 1, 'foo' => 'bar']], $objects);
    }
}