@@ -1607,7 +1607,68 @@ PHPUnit Data Providers
16071607~~~~~~~~~~~~~~~~~~~~~~
16081608
16091609It is possible to use factories in
1610- `PHPUnit data providers <https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#data-providers >`_:
1610+ `PHPUnit data providers <https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#data-providers >`_.
1611+ Their usage depends on which Foundry version you are running:
1612+
1613+ Data Providers with Foundry ^2.2
1614+ ................................
1615+
1616+ From version 2.2, Foundry provides an extension for PHPUnit.
1617+ You can install it by modifying you ``phpunit.xml.dist ``:
1618+
1619+ .. configuration-block ::
1620+
1621+ .. code-block :: xml
1622+
1623+ <phpunit>
1624+ <extensions>
1625+ <bootstrap class="Zenstruck\F oundry\P HPUnit\F oundryExtension"/>
1626+ </extensions>
1627+ </phpunit>
1628+
1629+ .. warning ::
1630+
1631+ This PHPUnit extension requires at least PHPUnit 11.4.
1632+
1633+ Using this extension will allow to use your factories in your data providers the same way you're using them in tests.
1634+ Thanks to it, you can:
1635+ * Call ``->create() `` or ``::createOne() `` or any other method which creates objects in unit tests
1636+ (using ``PHPUnit\Framework\TestCase ``) and functional tests (``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase ``)
1637+ * Use `Factories as Services `_ in functional tests
1638+ * Use `faker() ` normally, without wrapping its call in a callable
1639+
1640+ ::
1641+
1642+ use App\Factory\PostFactory;
1643+ use PHPUnit\Framework\Attributes\DataProvider;
1644+
1645+ #[DataProvider('createMultipleObjectsInDataProvider')]
1646+ public function test_post_via_data_provider(Post $post): void
1647+ {
1648+ // at this point, `$post` exists, and is already stored in database
1649+ }
1650+
1651+ public static function postDataProvider(): iterable
1652+ {
1653+ yield [PostFactory::createOne()];
1654+ yield [PostWithServiceFactory::createOne()];
1655+ yield [PostFactory::createOne(['body' => faker()->sentence()];
1656+ }
1657+
1658+
1659+ .. warning ::
1660+
1661+ Because Foundry is relying on its `Proxy mechanism <object-proxy >`_, when using persistence,
1662+ your factories must extend ``Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory `` to work in your data providers.
1663+
1664+ .. warning ::
1665+
1666+ For the same reason, you should not call methods from `Proxy ` class in your data providers,
1667+ not even ``->_real() ``.
1668+
1669+
1670+ Data Providers before Foundry v2.2
1671+ ..................................
16111672
16121673::
16131674
0 commit comments