Skip to content

Documented PHPUnit data providers #10096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion best_practices/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ functional tests, you can quickly spot any big errors before you deploy them:
Define a functional test that at least checks if your application pages
are successfully loading.

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

// src/AppBundle/Tests/ApplicationAvailabilityFunctionalTest.php
namespace AppBundle\Tests;
Expand Down
58 changes: 5 additions & 53 deletions form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ widgets you want to display are available in the children property::
$this->assertArrayHasKey($key, $children);
}

.. tip::

Use :ref:`PHPUnit data providers <testing-data-providers>` to test multiple
form conditions using the same test code.

Testings Types from the Service Container
-----------------------------------------

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

// ... your tests
}

Testing against Different Sets of Data
--------------------------------------

If you are not familiar yet with PHPUnit's `data providers`_, this might be
a good opportunity to use them::

// src/AppBundle/Tests/Form/Type/TestedTypeTest.php
namespace AppBundle\Tests\Form\Type;

use AppBundle\Form\Type\TestedType;
use Symfony\Component\Form\Test\TypeTestCase;

class TestedTypeTest extends TypeTestCase
{
/**
* @dataProvider getValidTestData
*/
public function testForm($data)
{
// ... your test
}

public function getValidTestData()
{
return array(
array(
'data' => array(
'test' => 'test',
'test2' => 'test2',
),
),
array(
'data' => array(),
),
array(
'data' => array(
'test' => null,
'test2' => null,
),
),
);
}
}

The code above will run your test three times with 3 different sets of
data. This allows for decoupling the test fixtures from the tests and
easily testing against multiple sets of data.

You can also pass another argument, such as a boolean if the form has to
be synchronized with the given set of data or not etc.

.. _`data providers`: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers
36 changes: 36 additions & 0 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,41 @@ document::
// ...or simply check that the response is a redirect to any URL
$this->assertTrue($client->getResponse()->isRedirect());

.. _testing-data-providers:

Testing against Different Sets of Data
--------------------------------------

It's common to have to execute the same test against different sets of data to
check the multiple conditions code must handle. This is solved with PHPUnit's
`data providers`_, which work both for unit and functional tests.

First, add one or more arguments to your test method and use them inside the
test code. Then, define another method which returns a nested array with the
arguments to use on each test run. Lastly, add the ``@dataProvider`` annotation
to associate both methods::

/**
* @dataProvider provideUrls
*/
public function testPageIsSuccessful($url)
{
$client = self::createClient();
$client->request('GET', $url);

$this->assertTrue($client->getResponse()->isSuccessful());
}

public function provideUrls()
{
return array(
array('/'),
array('/blog'),
array('/contact'),
// ...
);
}

.. index::
single: Tests; Client

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