Skip to content

Commit fa306d3

Browse files
committed
minor #10096 Documented PHPUnit data providers (javiereguiluz)
This PR was merged into the 2.8 branch. Discussion ---------- Documented PHPUnit data providers @xabbuh this change makes the `form/unit_testing.rst` file to delete the `Testing against Different Sets of Data` section. We usually solve this adding some RST reference in the same article ... but I don't think we can do the same in this case, right? If we can't , it's no such a big deal anyway. Thanks! Commits ------- a926029 Documented PHPUnit data providers
2 parents 1327065 + a926029 commit fa306d3

File tree

3 files changed

+43
-54
lines changed

3 files changed

+43
-54
lines changed

best_practices/tests.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ functional tests, you can quickly spot any big errors before you deploy them:
2626
Define a functional test that at least checks if your application pages
2727
are successfully loading.
2828

29-
A functional test can be as easy as this::
29+
A functional test like this is simple to implement thanks to
30+
:ref:`PHPUnit data providers <testing-data-providers>`::
3031

3132
// src/AppBundle/Tests/ApplicationAvailabilityFunctionalTest.php
3233
namespace AppBundle\Tests;

form/unit_testing.rst

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ widgets you want to display are available in the children property::
112112
$this->assertArrayHasKey($key, $children);
113113
}
114114

115+
.. tip::
116+
117+
Use :ref:`PHPUnit data providers <testing-data-providers>` to test multiple
118+
form conditions using the same test code.
119+
115120
Testings Types from the Service Container
116121
-----------------------------------------
117122

@@ -212,56 +217,3 @@ allows you to return a list of extensions to register::
212217

213218
// ... your tests
214219
}
215-
216-
Testing against Different Sets of Data
217-
--------------------------------------
218-
219-
If you are not familiar yet with PHPUnit's `data providers`_, this might be
220-
a good opportunity to use them::
221-
222-
// src/AppBundle/Tests/Form/Type/TestedTypeTest.php
223-
namespace AppBundle\Tests\Form\Type;
224-
225-
use AppBundle\Form\Type\TestedType;
226-
use Symfony\Component\Form\Test\TypeTestCase;
227-
228-
class TestedTypeTest extends TypeTestCase
229-
{
230-
/**
231-
* @dataProvider getValidTestData
232-
*/
233-
public function testForm($data)
234-
{
235-
// ... your test
236-
}
237-
238-
public function getValidTestData()
239-
{
240-
return array(
241-
array(
242-
'data' => array(
243-
'test' => 'test',
244-
'test2' => 'test2',
245-
),
246-
),
247-
array(
248-
'data' => array(),
249-
),
250-
array(
251-
'data' => array(
252-
'test' => null,
253-
'test2' => null,
254-
),
255-
),
256-
);
257-
}
258-
}
259-
260-
The code above will run your test three times with 3 different sets of
261-
data. This allows for decoupling the test fixtures from the tests and
262-
easily testing against multiple sets of data.
263-
264-
You can also pass another argument, such as a boolean if the form has to
265-
be synchronized with the given set of data or not etc.
266-
267-
.. _`data providers`: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers

testing.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,41 @@ document::
294294
// ...or simply check that the response is a redirect to any URL
295295
$this->assertTrue($client->getResponse()->isRedirect());
296296

297+
.. _testing-data-providers:
298+
299+
Testing against Different Sets of Data
300+
--------------------------------------
301+
302+
It's common to have to execute the same test against different sets of data to
303+
check the multiple conditions code must handle. This is solved with PHPUnit's
304+
`data providers`_, which work both for unit and functional tests.
305+
306+
First, add one or more arguments to your test method and use them inside the
307+
test code. Then, define another method which returns a nested array with the
308+
arguments to use on each test run. Lastly, add the ``@dataProvider`` annotation
309+
to associate both methods::
310+
311+
/**
312+
* @dataProvider provideUrls
313+
*/
314+
public function testPageIsSuccessful($url)
315+
{
316+
$client = self::createClient();
317+
$client->request('GET', $url);
318+
319+
$this->assertTrue($client->getResponse()->isSuccessful());
320+
}
321+
322+
public function provideUrls()
323+
{
324+
return array(
325+
array('/'),
326+
array('/blog'),
327+
array('/contact'),
328+
// ...
329+
);
330+
}
331+
297332
.. index::
298333
single: Tests; Client
299334

@@ -927,3 +962,4 @@ Learn more
927962
.. _`documentation`: https://phpunit.de/manual/current/en/
928963
.. _`PHPUnit Bridge component`: https://symfony.com/components/PHPUnit%20Bridge
929964
.. _`$_SERVER`: https://php.net/manual/en/reserved.variables.server.php
965+
.. _`data providers`: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers

0 commit comments

Comments
 (0)