Skip to content

Commit

Permalink
Start using Foundry
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jan 15, 2025
1 parent eebdf34 commit 7b03389
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 82 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-phpunit": "^1.4",
"phpstan/phpstan-webmozart-assert": "^1.2",
"phpunit/phpunit": "^9.6",
"phpunit/phpunit": "^10.0",
"rector/rector": "^0.18.2",
"sylius-labs/coding-standard": "^4.4",
"sylius/grid-bundle": "^1.13",
Expand All @@ -77,16 +77,17 @@
"symfony/dependency-injection": "^6.4 || ^7.1",
"symfony/dotenv": "^6.4 || ^7.1",
"symfony/http-kernel": "^6.4 || ^7.1",
"symfony/messenger": "^6.4 || ^7.1",
"symfony/security-bundle": "^6.4 || ^7.1",
"symfony/serializer": "^6.4 || ^7.1",
"symfony/stopwatch": "^6.4 || ^7.1",
"symfony/uid": "^6.4 || ^7.1",
"symfony/workflow": "^6.4 || ^7.1",
"symfony/messenger": "^6.4 || ^7.1",
"symfony/serializer": "^6.4 || ^7.1",
"symfony/security-bundle": "^6.4 || ^7.1",
"twig/twig": "^3.14",
"vimeo/psalm": "^5.26",
"willdurand/hateoas-bundle": "^2.5",
"winzou/state-machine-bundle": "^0.6.2"
"winzou/state-machine-bundle": "^0.6.2",
"zenstruck/foundry": "^2.3"
},
"conflict": {
"doctrine/orm": "<2.18 || ^3.0",
Expand Down
1 change: 1 addition & 0 deletions tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
NelmioAliceBundle::class => ['all' => true],
winzouStateMachineBundle::class => ['all' => true, 'test_without_state_machine' => false],
SyliusGridBundle::class => ['all' => true, 'test_without_twig' => false],
Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],
];
52 changes: 52 additions & 0 deletions tests/Application/src/Foundry/Factory/BookFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Factory;

use App\Entity\Book;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

/**
* @extends PersistentProxyObjectFactory<Book>
*/
final class BookFactory extends PersistentProxyObjectFactory
{
public static function class(): string
{
return Book::class;
}

public function withTranslations(array $translations): self
{
return $this->with(['translations' => $translations]);
}

public function withTitle(string $title): self
{
return $this->with(['title' => $title]);
}

public function withAuthor(string $author): self
{
return $this->with(['author' => $author]);
}

protected function defaults(): array
{
return [
'fallbackLocale' => 'en_US',
'currentLocale' => 'en_US',
'author' => self::faker()->firstName() . ' ' . self::faker()->lastName(),
];
}
}
46 changes: 46 additions & 0 deletions tests/Application/src/Foundry/Factory/BookTranslationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Factory;

use App\Entity\BookTranslation;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

/**
* @extends PersistentProxyObjectFactory<BookTranslation>
*/
final class BookTranslationFactory extends PersistentProxyObjectFactory
{
public static function class(): string
{
return BookTranslation::class;
}

public function withLocale(string $locale): self
{
return $this->with(['locale' => $locale]);
}

public function withTitle(string $title): self
{
return $this->with(['title' => $title]);
}

protected function defaults(): array
{
return [
'locale' => self::faker()->locale(),
'title' => ucfirst(self::faker()->words(2, true)),
];
}
}
43 changes: 43 additions & 0 deletions tests/Application/src/Foundry/Story/DefaultBooksStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Story;

use App\Foundry\Factory\BookFactory;
use App\Foundry\Factory\BookTranslationFactory;
use Zenstruck\Foundry\Story;

final class DefaultBooksStory extends Story
{
public function build(): void
{
BookFactory::new()
->withTranslations([
BookTranslationFactory::new()
->withLocale('en_US')
->withTitle('Lord of The Rings'),
BookTranslationFactory::new()
->withLocale('pl_PL')
->withTitle('Władca Pierścieni'),
])
->withAuthor('J.R.R. Tolkien')
->create()
;

BookFactory::new()
->withTitle('Game of Thrones')
->withAuthor('George R. R. Martin')
->create()
;
}
}
33 changes: 33 additions & 0 deletions tests/Application/src/Foundry/Story/MoreBooksStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Foundry\Story;

use App\Foundry\Factory\BookFactory;
use function Zenstruck\Foundry\Persistence\flush_after;
use Zenstruck\Foundry\Story;

final class MoreBooksStory extends Story
{
public function build(): void
{
flush_after(function () {
foreach (range(1, 22) as $number) {
BookFactory::new()
->withTitle('Book ' . $number)
->create()
;
}
});
}
}
Loading

0 comments on commit 7b03389

Please sign in to comment.