Skip to content

Commit ad0b8d1

Browse files
authored
[DX][Internal] Start using Foundry (#969)
| Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Foundry is the official Symfony package for data fixtures and it's maintained by Symfony core team members.
2 parents 7f99b30 + eb8c5c4 commit ad0b8d1

File tree

7 files changed

+249
-82
lines changed

7 files changed

+249
-82
lines changed

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"phpstan/phpstan": "^1.12",
6969
"phpstan/phpstan-phpunit": "^1.4",
7070
"phpstan/phpstan-webmozart-assert": "^1.2",
71-
"phpunit/phpunit": "^9.6",
71+
"phpunit/phpunit": "^10.0",
7272
"rector/rector": "^0.18.2",
7373
"sylius-labs/coding-standard": "^4.4",
7474
"sylius/grid-bundle": "^1.13",
@@ -77,16 +77,17 @@
7777
"symfony/dependency-injection": "^6.4 || ^7.1",
7878
"symfony/dotenv": "^6.4 || ^7.1",
7979
"symfony/http-kernel": "^6.4 || ^7.1",
80+
"symfony/messenger": "^6.4 || ^7.1",
81+
"symfony/security-bundle": "^6.4 || ^7.1",
82+
"symfony/serializer": "^6.4 || ^7.1",
8083
"symfony/stopwatch": "^6.4 || ^7.1",
8184
"symfony/uid": "^6.4 || ^7.1",
8285
"symfony/workflow": "^6.4 || ^7.1",
83-
"symfony/messenger": "^6.4 || ^7.1",
84-
"symfony/serializer": "^6.4 || ^7.1",
85-
"symfony/security-bundle": "^6.4 || ^7.1",
8686
"twig/twig": "^3.14",
8787
"vimeo/psalm": "^5.26",
8888
"willdurand/hateoas-bundle": "^2.5",
89-
"winzou/state-machine-bundle": "^0.6.2"
89+
"winzou/state-machine-bundle": "^0.6.2",
90+
"zenstruck/foundry": "^2.3"
9091
},
9192
"conflict": {
9293
"doctrine/orm": "<2.18",

tests/Application/config/bundles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@
3939
NelmioAliceBundle::class => ['all' => true],
4040
winzouStateMachineBundle::class => ['all' => true, 'test_without_state_machine' => false],
4141
SyliusGridBundle::class => ['all' => true, 'test_without_twig' => false],
42+
Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],
4243
];
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Factory;
15+
16+
use App\Entity\Book;
17+
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
18+
19+
/**
20+
* @extends PersistentProxyObjectFactory<Book>
21+
*/
22+
final class BookFactory extends PersistentProxyObjectFactory
23+
{
24+
public static function class(): string
25+
{
26+
return Book::class;
27+
}
28+
29+
public function withTranslations(array $translations): self
30+
{
31+
return $this->with(['translations' => $translations]);
32+
}
33+
34+
public function withTitle(string $title): self
35+
{
36+
return $this->with(['title' => $title]);
37+
}
38+
39+
public function withAuthor(string $author): self
40+
{
41+
return $this->with(['author' => $author]);
42+
}
43+
44+
protected function defaults(): array
45+
{
46+
return [
47+
'fallbackLocale' => 'en_US',
48+
'currentLocale' => 'en_US',
49+
'author' => self::faker()->firstName() . ' ' . self::faker()->lastName(),
50+
];
51+
}
52+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Factory;
15+
16+
use App\Entity\BookTranslation;
17+
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
18+
19+
/**
20+
* @extends PersistentProxyObjectFactory<BookTranslation>
21+
*/
22+
final class BookTranslationFactory extends PersistentProxyObjectFactory
23+
{
24+
public static function class(): string
25+
{
26+
return BookTranslation::class;
27+
}
28+
29+
public function withLocale(string $locale): self
30+
{
31+
return $this->with(['locale' => $locale]);
32+
}
33+
34+
public function withTitle(string $title): self
35+
{
36+
return $this->with(['title' => $title]);
37+
}
38+
39+
protected function defaults(): array
40+
{
41+
return [
42+
'locale' => self::faker()->locale(),
43+
'title' => ucfirst(self::faker()->words(2, true)),
44+
];
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Story;
15+
16+
use App\Foundry\Factory\BookFactory;
17+
use App\Foundry\Factory\BookTranslationFactory;
18+
use Zenstruck\Foundry\Story;
19+
20+
final class DefaultBooksStory extends Story
21+
{
22+
public function build(): void
23+
{
24+
BookFactory::new()
25+
->withTranslations([
26+
BookTranslationFactory::new()
27+
->withLocale('en_US')
28+
->withTitle('Lord of The Rings'),
29+
BookTranslationFactory::new()
30+
->withLocale('pl_PL')
31+
->withTitle('Władca Pierścieni'),
32+
])
33+
->withAuthor('J.R.R. Tolkien')
34+
->create()
35+
;
36+
37+
BookFactory::new()
38+
->withTitle('Game of Thrones')
39+
->withAuthor('George R. R. Martin')
40+
->create()
41+
;
42+
}
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Foundry\Story;
15+
16+
use App\Foundry\Factory\BookFactory;
17+
use function Zenstruck\Foundry\Persistence\flush_after;
18+
use Zenstruck\Foundry\Story;
19+
20+
final class MoreBooksStory extends Story
21+
{
22+
public function build(): void
23+
{
24+
flush_after(function () {
25+
foreach (range(1, 22) as $number) {
26+
BookFactory::new()
27+
->withTitle('Book ' . $number)
28+
->create()
29+
;
30+
}
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)