1
1
.. index ::
2
2
single: Tests; Database
3
3
4
- How to test code which interacts with the database
5
- ==================================================
4
+ How to test code that interacts with the Database
5
+ =================================================
6
6
7
- If your code interacts with the database, e.g. reads data from or stores data into
8
- it, you need to adjust your tests to take this into account. There are many ways
9
- how to deal with this. In a unit test, you can create a mock for a ``Repository ``
10
- and use it to return expected objects. In a functional test, you may need to
11
- prepare a test database with predefined values, so a test always has the same data
12
- to work with.
7
+ If your code interacts with the database, e.g. reads data from or stores data
8
+ into it, you need to adjust your tests to take this into account. There are
9
+ many ways how to deal with this. In a unit test, you can create a mock for
10
+ a ``Repository `` and use it to return expected objects. In a functional test,
11
+ you may need to prepare a test database with predefined values to ensure that
12
+ your test always has the same data to work with.
13
+
14
+ .. note ::
15
+
16
+ If you want to test your queries directly, see :doc: `/cookbook/testing/doctrine `.
13
17
14
18
Mocking the ``Repository `` in a Unit Test
15
19
-----------------------------------------
16
20
17
- If you want to test code which depends on a doctrine ``Repository `` in isolation, you
18
- need to mock the ``Repository ``. Normally you get the ``Repository `` from the ``EntityManager ``,
19
- so you would need to mock those as well. Suppose the class you want to test looks like this::
21
+ If you want to test code which depends on a doctrine ``Repository `` in isolation,
22
+ you need to mock the ``Repository ``. Normally you inject the ``EntityManager ``
23
+ into your class and use it to get the repository. This makes things a little
24
+ more difficult as you need to mock both the ``EntityManager `` and your repository
25
+ class.
26
+
27
+ .. tip ::
28
+
29
+ It is possible (and a good idea) to inject your repository directly by
30
+ registering your repository as a :doc: `factory service</components/dependency_injection/factories> `
31
+ This is a little bit more work to setup, but makes testing easier as you
32
+ only need to mock the repository.
33
+
34
+ Suppose the class you want to test looks like this::
20
35
21
36
namespace Acme\DemoBundle\Salary;
22
37
@@ -40,8 +55,8 @@ so you would need to mock those as well. Suppose the class you want to test look
40
55
}
41
56
}
42
57
43
- As the ``ObjectManager `` gets injected into the class through the constructor, it's
44
- easy to pass a mock object within a test::
58
+ Since the ``ObjectManager `` gets injected into the class through the constructor,
59
+ it's easy to pass a mock object within a test::
45
60
46
61
use Acme\DemoBundle\Salary\SalaryCalculator;
47
62
@@ -80,11 +95,12 @@ easy to pass a mock object within a test::
80
95
}
81
96
}
82
97
83
- We are building our mocks from the inside out, first creating the employee which
84
- gets returned by the ``Repository `` which itself gets returned by the ``EntityManager ``.
85
- This way, no real class is involved in testing.
98
+ In this example, you are building the mocks from the inside out, first creating
99
+ the employee which gets returned by the ``Repository ``, which itself gets
100
+ returned by the ``EntityManager ``. This way, no real class is involved in
101
+ testing.
86
102
87
- Changing database settings for functional tests
103
+ Changing database Settings for functional Tests
88
104
-----------------------------------------------
89
105
90
106
If you have functional tests, you want them to interact with a real database.
@@ -129,6 +145,6 @@ configuration:
129
145
'password' => 'testdb',
130
146
),
131
147
));
132
-
148
+
133
149
Make sure that your database runs on localhost and has the defined database and
134
- user credentials set up.
150
+ user credentials set up.
0 commit comments