@@ -114,13 +114,13 @@ message contains the ``"foobar"`` string.
114
114
Making Tests Fail
115
115
-----------------
116
116
117
- By default, any non-legacy-tagged or any non-`@-silenced `_ deprecation notices will
118
- make tests fail. Alternatively, setting ``SYMFONY_DEPRECATIONS_HELPER `` to an
119
- arbitrary value (ex: ``320 ``) will make the tests fails only if a higher number
120
- of deprecation notices is reached (``0 `` is the default value). You can also set
121
- the value ``"weak" `` which will make the bridge ignore any deprecation notices.
122
- This is useful to projects that must use deprecated interfaces for backward
123
- compatibility reasons.
117
+ By default, any non-legacy-tagged or any non-`@-silenced `_ deprecation notices
118
+ will make tests fail. Alternatively, setting ``SYMFONY_DEPRECATIONS_HELPER `` to
119
+ an arbitrary value (ex: ``320 ``) will make the tests fails only if a higher
120
+ number of deprecation notices is reached (``0 `` is the default value). You can
121
+ also set the value ``"weak" `` which will make the bridge ignore any deprecation
122
+ notices. This is useful to projects that must use deprecated interfaces for
123
+ backward compatibility reasons.
124
124
125
125
Time-sensitive Tests
126
126
--------------------
@@ -171,7 +171,7 @@ To use the ``ClockMock`` class in your test, you can:
171
171
``ClockMock::withClockMock(true) `` before the test and
172
172
``ClockMock::withClockMock(false) `` after the test.
173
173
174
- As a result, the following is guarenteed to work and is no longer a transient
174
+ As a result, the following is guaranteed to work and is no longer a transient
175
175
test::
176
176
177
177
use Symfony\Component\Stopwatch\Stopwatch;
@@ -203,17 +203,97 @@ And that's all!
203
203
advances the internal clock the given number of seconds without actually
204
204
waiting that time, so your test will execute 10 seconds faster.
205
205
206
+ DNS-sensitive Tests
207
+ -------------------
208
+
209
+ .. versionadded :: 3.1
210
+ The mocks for DNS related functions were introduced in Symfony 3.1.
211
+
212
+ Tests that make network connections, for example to check the validity of a DNS
213
+ record, can be slow to execute and unreliable due to the conditions of the
214
+ network. For that reason, this component also provides mocks for these PHP
215
+ functions:
216
+
217
+ * :phpfunction: `checkdnsrr() `
218
+ * :phpfunction: `dns_check_record() `
219
+ * :phpfunction: `getmxrr() `
220
+ * :phpfunction: `dns_get_mx() `
221
+ * :phpfunction: `gethostbyaddr() `
222
+ * :phpfunction: `gethostbyname() `
223
+ * :phpfunction: `gethostbynamel() `
224
+ * :phpfunction: `dns_get_record() `
225
+
226
+ Use Case
227
+ ~~~~~~~~
228
+
229
+ Consider the following example that uses the ``checkMX `` option of the ``Email ``
230
+ constraint to test the validity of the email domain::
231
+
232
+ use Symfony\Component\Validator\Constraints\Email;
233
+
234
+ class MyTest extends \PHPUnit_Framework_TestCase
235
+ {
236
+ public function testEmail()
237
+ {
238
+ $validator = ...
239
+ $constraint = new Email(array('checkMX' => true));
240
+
241
+ $result = $validator->validate('foo@example.com', $constraint);
242
+
243
+ // ...
244
+ }
245
+
246
+ In order to avoid making a real network connection, add the ``@dns-sensitive ``
247
+ annotation to the class and use the ``DnsMock::withMockedHosts() `` to configure
248
+ the data you expect to get for the given hosts::
249
+
250
+ use Symfony\Component\Validator\Constraints\Email;
251
+
252
+ /**
253
+ * @group dns-sensitive
254
+ */
255
+ class MyTest extends \PHPUnit_Framework_TestCase
256
+ {
257
+ public function testEmails()
258
+ {
259
+ DnsMock::withMockedHosts(array('example.com' => array(array('type' => 'MX'))));
260
+
261
+ $validator = ...
262
+ $constraint = new Email(array('checkMX' => true));
263
+
264
+ $result = $validator->validate('foo@example.com', $constraint);
265
+
266
+ // ...
267
+ }
268
+
269
+ The ``withMockedHosts() `` method can return any number of hosts with different
270
+ configuration, so you can simulate diverse network conditions::
271
+
272
+ DnsMock::withMockedHosts(array(
273
+ 'example.com' => array(
274
+ array(
275
+ 'type' => 'A',
276
+ 'ip' => '1.2.3.4',
277
+ ),
278
+ array(
279
+ 'type' => 'AAAA',
280
+ 'ipv6' => '::12',
281
+ ),
282
+ ),
283
+ ));
284
+
206
285
Troubleshooting
207
- ~~~~~~~~~~~~~~~
286
+ ---------------
208
287
209
- The ``@group time-sensitive `` works "by convention" and assumes that the
210
- namespace of the tested class can be obtained just by removing the ``\Tests\ ``
211
- part from the test namespace. I.e. that if the your test case fully-qualified
212
- class name (FQCN) is ``App\Tests\Watch\DummyWatchTest ``, it assumes the tested
213
- class FQCN is ``App\Watch\DummyWatch ``.
288
+ The ``@group time-sensitive `` and ``@group dns-sensitive `` annotations work
289
+ "by convention" and assume that the namespace of the tested class can be
290
+ obtained just by removing the ``\Tests\ `` part from the test namespace. For
291
+ example, if your test case fully-qualified class name (FQCN) is
292
+ ``App\Tests\Watch\DummyWatchTest ``, it assumes the tested class FQCN is
293
+ ``App\Watch\DummyWatch ``.
214
294
215
- If this convention doesn't work for your application, you can also configure
216
- the mocked namespaces in the ``phpunit.xml `` file, as done for example in the
295
+ If this convention doesn't work for your application, configure the mocked
296
+ namespaces in the ``phpunit.xml `` file, as done for example in the
217
297
:doc: `HttpKernel Component </components/http_kernel/introduction >`:
218
298
219
299
.. code-block :: xml
0 commit comments