Skip to content

Commit 4eb546c

Browse files
committed
[doc] document non-Kernel testing
1 parent c8040f7 commit 4eb546c

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ Want to watch a screencast 🎥 about it? Check out https://symfonycasts.com/fou
5353
7. [Performance](#performance)
5454
1. [DAMADoctrineTestBundle](#damadoctrinetestbundle)
5555
2. [Miscellaneous](#miscellaneous)
56-
8. [Using without the Bundle](#using-without-the-bundle)
56+
8. [Non-Kernel Test](#non-kernel-tests)
57+
9. [Test-Only Configuration](#test-only-configuration)
58+
10. [Using without the Bundle](#using-without-the-bundle)
5759
7. [Stories](#stories)
5860
1. [Stories as Services](#stories-as-services)
5961
2. [Story State](#story-state)
@@ -1300,12 +1302,41 @@ these tests to be unnecessarily slow. You can improve the speed by reducing the
13001302
Now, in your tests, when you need access to the unencoded password for a user created with `UserFactory`, use
13011303
`UserFactory::DEFAULT_PASSWORD`.
13021304

1303-
### Using without the Bundle
1305+
### Non-Kernel Tests
13041306

1305-
The provided bundle is not strictly required to use Foundry for tests. You can have all your factories, stories, and
1306-
configuration live in your `tests/` directory.
1307+
Foundry can be used in standard PHPUnit unit tests (TestCase's that just extend `PHPUnit\Framework\TestCase` and not
1308+
`Symfony\Bundle\FrameworkBundle\Test\KernelTestCase`). These tests still require using the `Factories` trait to boot
1309+
Foundry but will not have doctrine available. Factories created in these tests will not be persisted (calling
1310+
[`->withoutPersisting()`](#without-persisting) is not necessary). Because the bundle is not available in these tests,
1311+
any bundle configuration you have will not be picked up. You will need to add
1312+
[Test-Only Configuration](#test-only-configuration). Unfortunately, this may mean duplicating your bundle configuration
1313+
here.
1314+
1315+
```php
1316+
use App\Factory\PostFactory;
1317+
use PHPUnit\Framework\TestCase;
1318+
use Zenstruck\Foundry\Test\Factories;
1319+
1320+
class MyUnitTest extends TestCase
1321+
{
1322+
use Factories;
1323+
1324+
public function some_test(): void
1325+
{
1326+
$post = PostFactory::new()->create();
13071327

1308-
The best place to configure Foundry without the bundle is in your `tests/bootstrap.php` file:
1328+
// $post is not persisted to the database
1329+
}
1330+
}
1331+
```
1332+
1333+
**NOTE**: [Factories as Services](#factories-as-services) and [Stories as Services](#stories-as-services) are not
1334+
usable in non-Kernel tests.
1335+
1336+
### Test-Only Configuration
1337+
1338+
Foundry can be configured statically, with pure PHP, in your `tests/bootstrap.php`. This is useful if you have a mix
1339+
of Kernel and [non-Kernel tests](#non-kernel-tests) or if using Foundry [without the bundle](#using-without-the-bundle):
13091340

13101341
```php
13111342
// tests/bootstrap.php
@@ -1326,6 +1357,15 @@ Zenstruck\Foundry\Test\TestState::setFaker(Faker\Factory::create('fr_FR'));
13261357
Zenstruck\Foundry\Test\TestState::alwaysAutoRefreshProxies();
13271358
```
13281359

1360+
**NOTE**: If using [bundle configuration](#bundle-configuration) as well, *test-only configuration* will override the
1361+
bundle configuration.
1362+
1363+
### Using without the Bundle
1364+
1365+
The provided bundle is not strictly required to use Foundry for tests. You can have all your factories, stories, and
1366+
configuration live in your `tests/` directory. You can configure foundry with
1367+
[Test-Only Configuration](#test-only-configuration).
1368+
13291369
## Stories
13301370

13311371
Stories are useful if you find your test's *arrange* step is getting complex (loading lots of fixtures) or duplicating

0 commit comments

Comments
 (0)