Mockery (test doubles/mocking dependencies)

[This is a relatively old post I think I forgot to publish….]

Previously, I’d only used PHPUnit’s mock implementation; however lately I’ve been exposed to Mockery.

While they both achieve broadly the same result (at least from my point of view), here’s an example of how to mock dependencies with Mockery.

Class to test:

class TestMe {
    private $db;
    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function doesSomethingImportant(array $data) : bool {
        $stmt = $this->db->prepare("UPDATE my_table SET field2 = :field2 WHERE field1 = :field1");
        return $stmt->execute(['field1' => $data['field1'], 'field2' => $data['field2']);

    }
}

And to test ….

....
use Mockery as m;
class MyTest extends \PHPUnit\Framework\TestCase {
    public function tearDown() {
        m::close();
        parent::tearDown();
   }
    public function testDoesSomethingImpotant() {
        $fakePdo = m::mock(\PDO::class);
        $stmt = m::mock(\PDOStatement::class);
        $stmt
           ->shouldReceive('execute')
           ->withArgs(['field1' => 'hello', 'field2' => 'world'])
           ->once()
           ->andReturn(true);
        $fakePdo
           ->shouldReceive('prepare')
           ->withArgs(['UPDATE my table SET field2 = :field2 WHERE field1 = :field1'])
           ->once()
           ->andReturn($stmt);

        $testClass = new TestMe($fakePdo);
        $this->assertTrue(
            $testClass->updateSomething(['field1' => 'hello', 'field2' => 'world']);
    }
}

So that’s all well and good, and with a little imagination you can see how a method that does some calculation could be tested to ensure it does the calculation correctly and performs the appropriate database query. It obviously requires you inject all dependencies in to the class (else you can’t pass in the appropriate mocks!)

While this test is isolated from the underlying database, it doesn’t ensure you code will work – what if someone’s changed the database schema – your test will still (incorrectly) pass …

You can also create ‘fake’ errors throughout your code which might help give you a higher code coverage score ­čÖé

$pdo = m::mock(PDO::class);

$pdo->shouldReceive('prepare')
    ->withArgs(['bad sql'])
    ->andThrow(PDOException::class);

$pdo->shouldReceive('prepare')
    ->withArgs(['whatever good sql'])
    ->once()
    ->andReturn($stmt);

$pdo->shouldReceive('prepare')
    ->withArgs(['more good sql'])
    ->once()
    ->andReturn($stmt);
....

Leave a Reply

Your email address will not be published. Required fields are marked *