Skip to content

Commit 26aaf47

Browse files
authored
Add documentation to use Zenstruck Foundry instead of Alice (#1699)
1 parent 1102a58 commit 26aaf47

File tree

1 file changed

+110
-28
lines changed

1 file changed

+110
-28
lines changed

distribution/testing.md

Lines changed: 110 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,133 @@ integration](https://github.com/theofidry/AliceBundle#database-testing), an expr
2020

2121
Before creating your functional tests, you will need a dataset to pre-populate your API and be able to test it.
2222

23-
First, install [Alice](https://github.com/nelmio/alice):
23+
First, install [Foundry](https://github.com/zenstruck/foundry) and [Doctrine/DoctrineFixturesBundle](https://github.com/doctrine/DoctrineFixturesBundle):
2424

2525
```console
2626
docker compose exec php \
27-
composer require --dev alice
27+
composer require --dev foundry orm-fixtures
2828
```
2929

30-
Thanks to Symfony Flex, Alice (and [AliceBundle](https://github.com/theofidry/AliceBundle)) are ready to use!
31-
Place your data fixtures files in a directory named `fixtures/`.
30+
Thanks to Symfony Flex, [DoctrineFixturesBundle](https://github.com/doctrine/DoctrineFixturesBundle) and [Foundry](https://github.com/zenstruck/foundry) are ready to use!
3231

33-
Then, create some fixtures for [the bookstore API you created in the tutorial](index.md):
32+
Then, create some factories for [the bookstore API you created in the tutorial](index.md):
3433

35-
```yaml
36-
# api/fixtures/books.yaml
37-
App\Entity\Book:
38-
book_{1..100}:
39-
isbn: <isbn13()>
40-
title: <sentence(4)>
41-
description: <text()>
42-
author: <name()>
43-
publicationDate: <dateTimeImmutable()>
34+
```console
35+
docker compose exec php \
36+
bin/console make:factory 'App\Entity\Book'
37+
docker compose exec php \
38+
bin/console make:factory 'App\Entity\Review'
39+
40+
Improve the default values:
41+
42+
```php
43+
// src/Factory/BookFactory.php
44+
45+
// ...
46+
47+
protected function getDefaults(): array
48+
{
49+
return [
50+
'author' => self::faker()->name(),
51+
'description' => self::faker()->text(),
52+
'isbn' => self::faker()->isbn13(),
53+
'publication_date' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
54+
'title' => self::faker()->sentence(4),
55+
];
56+
}
57+
```
58+
59+
```php
60+
// src/Factory/ReviewFactory.php
61+
62+
// ...
63+
64+
protected function getDefaults(): array
65+
{
66+
return [
67+
'author' => self::faker()->name(),
68+
'body' => self::faker()->text(),
69+
'book' => lazy(fn() => BookFactory::randomOrCreate()),
70+
'publicationDate' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
71+
'rating' => self::faker()->numberBetween(0, 5),
72+
];
73+
}
74+
```
75+
76+
Create some stories:
77+
78+
```console
79+
docker compose exec php \
80+
bin/console make:story 'DefaultBooks'
81+
docker compose exec php \
82+
bin/console make:story 'DefaultReviews'
83+
84+
```php
85+
// src/Story/DefaultBooksStory.php
86+
87+
namespace App\Story;
88+
89+
use App\Factory\BookFactory;
90+
use Zenstruck\Foundry\Story;
91+
92+
final class DefaultBooksStory extends Story
93+
{
94+
public function build(): void
95+
{
96+
BookFactory::createMany(100);
97+
}
98+
}
99+
100+
```
101+
102+
```php
103+
// src/Story/DefaultReviewsStory.php
104+
105+
namespace App\Story;
106+
107+
use App\Factory\ReviewFactory;
108+
use Zenstruck\Foundry\Story;
109+
110+
final class DefaultReviewsStory extends Story
111+
{
112+
public function build(): void
113+
{
114+
ReviewFactory::createMany(200);
115+
}
116+
}
44117
```
45118

46-
```yaml
47-
# api/fixtures/reviews.yaml
48-
App\Entity\Review:
49-
review_{1..200}:
50-
rating: <numberBetween(0, 5)>
51-
body: <text()>
52-
author: <name()>
53-
publicationDate: <dateTimeImmutable()>
54-
book: '@book_*'
119+
Edit your Fixtures:
120+
121+
```php
122+
//src/DataFixtures/AppFixtures.php
123+
124+
namespace App\DataFixtures;
125+
126+
use App\Story\DefaultBooksStory;
127+
use App\Story\DefaultReviewsStory;
128+
use Doctrine\Bundle\FixturesBundle\Fixture;
129+
use Doctrine\Persistence\ObjectManager;
130+
131+
class AppFixtures extends Fixture
132+
{
133+
public function load(ObjectManager $manager): void
134+
{
135+
DefaultBooksStory::load();
136+
DefaultReviewsStory::load();
137+
}
138+
}
55139
```
56140

57141
You can now load your fixtures in the database with the following command:
58142

59143
```console
60144
docker compose exec php \
61-
bin/console hautelook:fixtures:load
145+
bin/console doctrine:fixtures:load
62146
```
63147

64-
To learn more about fixtures, take a look at the documentation of [Alice](https://github.com/nelmio/alice)
65-
and [AliceBundle](https://github.com/theofidry/AliceBundle).
66-
The list of available generators as well as a cookbook explaining how to create custom generators can be found in the documentation of [Faker](https://github.com/fakerphp/faker), the library used by Alice under the hood.
148+
To learn more about fixtures, take a look at the documentation of [Foundry](https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html).
149+
The list of available generators as well as a cookbook explaining how to create custom generators can be found in the documentation of [Faker](https://github.com/fakerphp/faker), the library used by Foundry under the hood.
67150

68151
## Writing Functional Tests
69152

@@ -260,7 +343,6 @@ The API Platform Demo [contains a CD worklow](https://github.com/api-platform/de
260343

261344
You may also be interested in these alternative testing tools (not included in the API Platform distribution):
262345

263-
* [Foundry](https://github.com/zenstruck/foundry), a modern fixtures library that will replace Alice as the recommended fixtures library soon;
264346
* [Hoppscotch](https://docs.hoppscotch.io/features/tests), create functional test for your API
265347
Platform project using a nice UI, benefit from its Swagger integration and run tests in the CI using [the command-line tool](https://docs.hoppscotch.io/cli);
266348
* [Behat](http://behat.org), a

0 commit comments

Comments
 (0)