The unit test database is about implementation detail

I have a simple PHP class that wraps a database access to fetch a user and wants to test it. I currently have the following code:

Class to check:

class UserTable {

    protected $tableGateway;

    public function __construct(\Zend\Db\TableGateway\TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function getUserWithId($id) {
        return $this->tableGateway->select(['id' => $id])->current();
    }
}

      

unit test:

class UserTableTest extends \PHPUnit_Framework_TestCase {
    public function testGetUserWithIdReturnsCorrectUser() {
        $user = new User();

        $resultSet = new ResultSet();
        $resultSet->initialize([$user]);

        $mockTableGateway = $this->getMock('\Zend\Db\TableGateway\TableGateway', ['select'], [], '', false);
        $mockTableGateway->expects($this->once())->method('select')->with(['id' => 1])->willReturn($resultSet);

        $userTable = new UserTable($mockTableGateway);

        $this->assertEquals($user, $userTable->getUserWithId(1));
    }
}

      

However, now the unit test will fail if I later decide to change the way I use the table gateway (for example, use select(['id = ?' => $id]

). This ties the unit test to an implementation detail getUserWithId($id)

to avoid.

What would be the best practice to prevent using a unit test depending on the implementation detail? Is it worth trying to set up an actual test database that the unit test can handle (which will also slow down test execution a lot), or is there a better way to mock the tabular gateway?

+3


source to share


1 answer


Don't scoff at code you don't have! * For classes using a database, you need to write integration tests. The good news is that this will force you to separate the DB access from other logic.



* This is actual advice from The Growing Test-Driven Object Oriented Software book, backed up by my own experience writing tests for code using Doctrine Entity Manager

+2


source







All Articles