|
4 | 4 | How to test Doctrine Repositories
|
5 | 5 | =================================
|
6 | 6 |
|
7 |
| -Unit testing Doctrine repositories in a Symfony project is not a straightforward |
8 |
| -task. Indeed, to load a repository you need to load your entities, an entity |
9 |
| -manager, and some other stuff like a connection. |
| 7 | +Unit testing Doctrine repositories in a Symfony project is not recommended. |
| 8 | +When you're dealing with a repository, you're really dealing with something |
| 9 | +that's meant to be tested against a real database connection. |
10 | 10 |
|
11 |
| -To test your repository, you have two different options: |
12 |
| - |
13 |
| -1) **Functional test**: This includes using a real database connection with |
14 |
| - real database objects. It's easy to setup and can test anything, but is |
15 |
| - slower to execute. See :ref:`cookbook-doctrine-repo-functional-test`. |
16 |
| - |
17 |
| -2) **Unit test**: Unit testing is faster to run and more precise in how you |
18 |
| - test. It does require a little bit more setup, which is covered in this |
19 |
| - document. It can also only test methods that, for example, build queries, |
20 |
| - not methods that actually execute them. |
21 |
| - |
22 |
| -Unit Testing |
23 |
| ------------- |
24 |
| - |
25 |
| -As Symfony and Doctrine share the same testing framework, it's quite easy to |
26 |
| -implement unit tests in your Symfony project. The ORM comes with its own set |
27 |
| -of tools to ease the unit testing and mocking of everything you need, such as |
28 |
| -a connection, an entity manager, etc. By using the testing components provided |
29 |
| -by Doctrine - along with some basic setup - you can leverage Doctrine's tools |
30 |
| -to unit test your repositories. |
31 |
| - |
32 |
| -Keep in mind that if you want to test the actual execution of your queries, |
33 |
| -you'll need a functional test (see :ref:`cookbook-doctrine-repo-functional-test`). |
34 |
| -Unit testing is only possible when testing a method that builds a query. |
35 |
| - |
36 |
| -Setup |
37 |
| -~~~~~ |
38 |
| - |
39 |
| -First, you need to add the ``Doctrine\Tests`` namespace to your autoloader:: |
40 |
| - |
41 |
| - // app/autoload.php |
42 |
| - $loader->registerNamespaces(array( |
43 |
| - // ... |
44 |
| - 'Doctrine\\Tests' => __DIR__.'/../vendor/doctrine/tests', |
45 |
| - // ... |
46 |
| - )); |
47 |
| - |
48 |
| -Next, you will need to setup an entity manager in each test so that Doctrine |
49 |
| -will be able to load your entities and repositories for you. |
50 |
| - |
51 |
| -As Doctrine is not able by default to load annotation metadata from your |
52 |
| -entities, you'll need to configure the annotation reader to be able to parse |
53 |
| -and load the entities:: |
54 |
| - |
55 |
| - // src/Acme/ProductBundle/Tests/Entity/ProductRepositoryTest.php |
56 |
| - |
57 |
| - namespace Acme\ProductBundle\Tests\Entity; |
58 |
| - |
59 |
| - use Doctrine\Tests\OrmTestCase; |
60 |
| - use Doctrine\Common\Annotations\AnnotationReader; |
61 |
| - use Doctrine\ORM\Mapping\Driver\DriverChain; |
62 |
| - use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
63 |
| - |
64 |
| - class ProductRepositoryTest extends OrmTestCase |
65 |
| - { |
66 |
| - /** |
67 |
| - * @var \Doctrine\Tests\Mocks\EntityManagerMock |
68 |
| - */ |
69 |
| - private $em; |
70 |
| - |
71 |
| - /** |
72 |
| - * {@inheritDoc} |
73 |
| - */ |
74 |
| - protected function setUp() |
75 |
| - { |
76 |
| - $reader = new AnnotationReader(); |
77 |
| - $reader->setIgnoreNotImportedAnnotations(true); |
78 |
| - $reader->setEnableParsePhpImports(true); |
79 |
| - |
80 |
| - // provide the namespace of the entities you want to tests |
81 |
| - $metadataDriver = new AnnotationDriver($reader, 'Acme\\StoreBundle\\Entity'); |
82 |
| - |
83 |
| - $this->em = $this->_getTestEntityManager(); |
84 |
| - |
85 |
| - $this->em->getConfiguration()->setMetadataDriverImpl($metadataDriver); |
86 |
| - |
87 |
| - // allows you to use the AcmeStoreBundle:Product syntax |
88 |
| - $this->em->getConfiguration()->setEntityNamespaces(array( |
89 |
| - 'AcmeStoreBundle' => 'Acme\\StoreBundle\\Entity' |
90 |
| - )); |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| -If you look at the code, you can notice: |
95 |
| - |
96 |
| -* You extend from ``\Doctrine\Tests\OrmTestCase``, which provide useful methods |
97 |
| - for unit testing; |
98 |
| - |
99 |
| -* You need to setup the ``AnnotationReader`` to be able to parse and load the |
100 |
| - entities; |
101 |
| - |
102 |
| -* You create the entity manager by calling ``_getTestEntityManager``, which |
103 |
| - returns a mocked entity manager with a mocked connection. |
104 |
| - |
105 |
| -That's it! You're ready to write units tests for your Doctrine repositories. |
106 |
| - |
107 |
| -Writing your Unit Test |
108 |
| -~~~~~~~~~~~~~~~~~~~~~~ |
109 |
| - |
110 |
| -Remember that Doctrine repository methods can only be tested if they are |
111 |
| -building and returning a query (but not actually executing a query). Take |
112 |
| -the following example:: |
113 |
| - |
114 |
| - // src/Acme/StoreBundle/Entity/ProductRepository.php |
115 |
| - |
116 |
| - namespace Acme\StoreBundle\Entity; |
117 |
| - |
118 |
| - use Doctrine\ORM\EntityRepository; |
119 |
| - |
120 |
| - class ProductRepository extends EntityRepository |
121 |
| - { |
122 |
| - /** |
123 |
| - * @param string $name |
124 |
| - * @return \Doctrine\ORM\QueryBuilder |
125 |
| - */ |
126 |
| - public function createSearchByNameQueryBuilder($name) |
127 |
| - { |
128 |
| - return $this |
129 |
| - ->createQueryBuilder('p') |
130 |
| - ->where('p.name LIKE :name') |
131 |
| - ->setParameter('name', $name) |
132 |
| - ; |
133 |
| - } |
134 |
| - } |
135 |
| - |
136 |
| -In this example, the method is returning a ``QueryBuilder`` instance. You |
137 |
| -can test the result of this method in a variety of ways:: |
138 |
| - |
139 |
| - // src/Acme/StoreBundle/Tests/Entity/ProductRepositoryTest.php |
140 |
| - |
141 |
| - /* ... */ |
142 |
| - |
143 |
| - class ProductRepositoryTest extends OrmTestCase |
144 |
| - { |
145 |
| - /* ... */ |
146 |
| - |
147 |
| - public function testCreateSearchByNameQueryBuilder() |
148 |
| - { |
149 |
| - $queryBuilder = $this->em |
150 |
| - ->getRepository('AcmeStoreBundle:Product') |
151 |
| - ->createSearchByNameQueryBuilder('foo') |
152 |
| - ; |
153 |
| - |
154 |
| - $this->assertEquals('p.name LIKE :name', (string) $queryBuilder->getDqlPart('where')); |
155 |
| - $this->assertEquals(array('name' => 'foo'), $queryBuilder->getParameters()); |
156 |
| - } |
157 |
| - } |
158 |
| - |
159 |
| -In this test, you dissect the ``QueryBuilder`` object, looking that each |
160 |
| -part is as you'd expect. If you were adding other things to the query builder, |
161 |
| -you might check the dql parts: ``select``, ``from``, ``join``, ``set``, ``groupBy``, |
162 |
| -``having``, or ``orderBy``. |
163 |
| - |
164 |
| -If you only have a raw ``Query`` object or prefer to test the actual query, |
165 |
| -you can test the DQL query string directly:: |
166 |
| - |
167 |
| - public function testCreateSearchByNameQueryBuilder() |
168 |
| - { |
169 |
| - $queryBuilder = $this->em |
170 |
| - ->getRepository('AcmeStoreBundle:Product') |
171 |
| - ->createSearchByNameQueryBuilder('foo') |
172 |
| - ; |
173 |
| - |
174 |
| - $dql = $queryBuilder->getQuery()->getDql(); |
175 |
| - |
176 |
| - $this->assertEquals( |
177 |
| - 'SELECT p FROM Acme\StoreBundle\Entity\Product p WHERE p.name LIKE :name', |
178 |
| - $dql |
179 |
| - ); |
180 |
| - } |
| 11 | +Fortunately, you can easily test your queries against a real database, as |
| 12 | +described below. |
181 | 13 |
|
182 | 14 | .. _cookbook-doctrine-repo-functional-test:
|
183 | 15 |
|
|
0 commit comments