Highly Opinionated Thoughts on Programming

by Elnur Abdurrakhimov


Testing Repositories

Jun 11, 2014


Another common confusion I see in the PHP community is unit testing repositories. Some developers using an ORM like Doctrine mock a query builder and make their mocks expect particular methods calls with particular arguments. Others even try to parse resulting DQL and check it for particular structure. And there are some who go as far as doing the same with the resulting SQL. o_O

In case you missed it, let me repeat it and do it slower. Some developers — okay — using an ORM — okay — and believing that using one will allow them to switch to another database vendor later and without problems — okay — test for the resulting — okay — S-Q-L. What’s more surprising, is that most of those developers think that using a lighter RDBMS like SQLite just for testing is a great idea.

No wonder people whine that TDD is hard, it sucks, it doesn’t work, and, well, it’s dead.

Okay. Enough of facepalming. Now let me try and cure some minds.

First, you should unit test only the code that’s under your control. And you should mock only your own types, even thought sometimes it’s perfectly okay to mock third-party interfaces.

Since repositories are usually chock full of third-party API calls, you can’t really control what code to write — you have to play by the rules of the APIs. Therefore unit testing them doesn’t make much sense.

Second, repositories are at the boundaries of your beautiful application and the ugly outside world. A database is not on the side of your application; it’s outside of it. Repositories are the ones that connect the both worlds. Therefore mocking the APIs of the outside world or the ones connecting with it doesn’t make sense. On the other hand, since repositories’ interfaces belong to you, mocking repositories themselves makes sense.

Third, each database system has its own specific way of communicating with it. If you tie your unit tests to inner workings of a third-party component by basically recreating the functionality of that component in your mocks and feel really great about yourself and how smart you are, I’m happy for you. Just don’t whine that your tests are insanely hard to write, are brittle, and maintaining them is a nightmare.

Fourth, just parsing and testing generated SQL should not give you any confidence that your code works because not everything can be derived from just SQL. You may be missing a required column in a table for SQL to work. A column or a table may have another name — after a database refactoring or for some other reason. Also databases have constraints that won’t get invoked unless you do a real database query.

All that means that not only unit testing repositories is quite a complicated feat, those tests don’t even give you the confidence tests are written to give in the first place. How else would you call it if not masochism?

All in all, to really test your repositories and have confidence in them, you need to do real database queries and test for results. That means you need to write integration tests for your repositories — not unit tests. Here’s an example of doing that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
namespace Example\Repository\Doctrine\ORM;

use Example\Test\AbstractIntegrationTest;
use Example\Util\ModelFactory;

class CompanyRepositoryTest extends AbstractIntegrationTest
{
    /**
     * @var CompanyRepository
     */
    private $companyRepository;

    protected function setUp()
    {
        parent::setUp();

        $this->companyRepository = $this->getContainer()->get('company_repository');
    }

    public function testFindByAdminOrEmployeeEmailDomainWithNoMatchingDomain()
    {
        $this->companyRepository->save(ModelFactory::createCompany([
            'admin' => ModelFactory::createUser([
                'email' => 'me@other.com',
            ]),
        ]));

        $this->assertCount(0, $this->companyRepository->findByAdminOrEmployeeEmailDomain('example.com'));
    }

    public function testFindByAdminOrEmployeeEmailDomainWithAdminEmailMatching()
    {
        $company = $this->companyRepository->save(ModelFactory::createCompany([
            'admin' => ModelFactory::createUser([
                'email' => 'me@example.com',
            ]),
        ]));

        $companies = $this->companyRepository->findByAdminOrEmployeeEmailDomain('example.com');
        $this->assertCount(1, $companies);
        $this->assertEquals($company->getId(), $companies[0]->getId());
    }

    public function testFindByAdminOrEmployeeEmailDomainWithEmployeeEmailMatching()
    {
        $company = $this->companyRepository->save(ModelFactory::createCompany([
            'admin' => ModelFactory::createUser([
                'email' => 'me@other.com',
            ]),
            'employees' => [
                ModelFactory::createUser([
                    'email' => 'foo@example.com',
                ]),
            ],
        ]));

        $companies = $this->companyRepository->findByAdminOrEmployeeEmailDomain('example.com');
        $this->assertCount(1, $companies);
        $this->assertEquals($company->getId(), $companies[0]->getId());
    }

    public function testFindByAdminOrEmployeeEmailDomainWithAdminAndSeveralEmployeesEmailsMatching()
    {
        $company = $this->companyRepository->save(ModelFactory::createCompany([
            'admin' => ModelFactory::createUser([
                'email' => 'me@example.com',
            ]),
            'employees' => [
                ModelFactory::createUser([
                    'email' => 'foo@example.com',
                ]),
                ModelFactory::createUser([
                    'email' => 'bar@example.com',
                ]),
            ],
        ]));

        $companies = $this->companyRepository->findByAdminOrEmployeeEmailDomain('example.com');
        $this->assertCount(1, $companies);
        $this->assertEquals($company->getId(), $companies[0]->getId());
    }

    public function testFindByAdminOrEmployeeEmailDomainDoesNotMatchPendingEmployees()
    {
        $this->companyRepository->save(ModelFactory::createCompany([
            'admin' => ModelFactory::createUser([
                'email' => 'me@other.com',
            ]),
            'pendingEmployees' => [
                ModelFactory::createUser([
                    'email' => 'foo@example.com',
                ]),
            ],
        ]));

        $this->assertEmpty($this->companyRepository->findByAdminOrEmployeeEmailDomain('example.com'));
    }
}

To make each test isolated, the parent class clears the database before each test. Note how each test creates only those fixtures it needs, make a call on the repository and checks for results.

The model factory creates model objects with fields prepopulated with random values. This helps keep tests clean and lets me explicitly specify only the fields that are required for a particular test. I plan to cover this technique in a later post.

As you can see, these tests are short, to the point, readable, easy to write and maintain, and do really test what they’re supposed to test. Now go and compare these to your unit tests.



© Elnur Abdurrakhimov