11..index ::
22 single: Tests; Database
33
4- How to test codewhich interacts with thedatabase
5- ==================================================
4+ How to test codethat interacts with theDatabase
5+ =================================================
66
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 `.
1317
1418Mocking the ``Repository `` in a Unit Test
1519-----------------------------------------
1620
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::
2035
2136 namespace Acme\DemoBundle\Salary;
2237
@@ -40,8 +55,8 @@ so you would need to mock those as well. Suppose the class you want to test look
4055 }
4156 }
4257
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::
4560
4661 use Acme\DemoBundle\Salary\SalaryCalculator;
4762
@@ -80,11 +95,12 @@ easy to pass a mock object within a test::
8095 }
8196 }
8297
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.
86102
87- Changing databasesettings for functionaltests
103+ Changing databaseSettings for functionalTests
88104-----------------------------------------------
89105
90106If you have functional tests, you want them to interact with a real database.
@@ -129,6 +145,6 @@ configuration:
129145 'password' => 'testdb',
130146 ),
131147 ));
132-
148+
133149 Make sure that your database runs on localhost and has the defined database and
134- user credentials set up.
150+ user credentials set up.