Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0506361

Browse files
committed
[symfony#2407] Minor tweaks to testing with the database entry
1 parent53174e0 commit0506361

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

‎cookbook/testing/database.rst

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
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

1418
Mocking 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'seasy 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

90106
If 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.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp