Skip to content

Commit

Permalink
fix: migrate factories to Foundry 2
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Jun 26, 2024
1 parent 554fb5d commit 07ee4e4
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 305 deletions.
82 changes: 43 additions & 39 deletions api/src/DataFixtures/Factory/BookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,51 @@

use App\Entity\Book;
use App\Enum\BookCondition;
use Doctrine\ORM\EntityRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
use App\Repository\BookRepository;
use Zenstruck\Foundry\FactoryCollection;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;

/**
* @extends ModelFactory<Book>
* @method Book|Proxy create(array|callable $attributes = [])
* @method static Book|Proxy createOne(array $attributes = [])
* @method static Book|Proxy find(object|array|mixed $criteria)
* @method static Book|Proxy findOrCreate(array $attributes)
* @method static Book|Proxy first(string $sortedField = 'id')
* @method static Book|Proxy last(string $sortedField = 'id')
* @method static Book|Proxy random(array $attributes = [])
* @method static Book|Proxy randomOrCreate(array $attributes = [])
* @method static Book[]|Proxy[] all()
* @method static Book[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Book[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Book[]|Proxy[] findBy(array $attributes)
* @method static Book[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Book[]|Proxy[] randomSet(int $number, array $attributes = [])
* @method FactoryCollection<Book|Proxy> many(int $min, int|null $max = null)
* @method FactoryCollection<Book|Proxy> sequence(iterable|callable $sequence)
* @method static ProxyRepositoryDecorator<Book, BookRepository> repository()
*
* @method Book|Proxy create(array|callable $attributes = [])
* @method static Book|Proxy createOne(array $attributes = [])
* @method static Book|Proxy find(object|array|mixed $criteria)
* @method static Book|Proxy findOrCreate(array $attributes)
* @method static Book|Proxy first(string $sortedField = 'id')
* @method static Book|Proxy last(string $sortedField = 'id')
* @method static Book|Proxy random(array $attributes = [])
* @method static Book|Proxy randomOrCreate(array $attributes = [])
* @method static EntityRepository|RepositoryProxy repository()
* @method static Book[]|Proxy[] all()
* @method static Book[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Book[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Book[]|Proxy[] findBy(array $attributes)
* @method static Book[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Book[]|Proxy[] randomSet(int $number, array $attributes = [])
* @phpstan-method Book&Proxy<Book> create(array|callable $attributes = [])
* @phpstan-method static Book&Proxy<Book> createOne(array $attributes = [])
* @phpstan-method static Book&Proxy<Book> find(object|array|mixed $criteria)
* @phpstan-method static Book&Proxy<Book> findOrCreate(array $attributes)
* @phpstan-method static Book&Proxy<Book> first(string $sortedField = 'id')
* @phpstan-method static Book&Proxy<Book> last(string $sortedField = 'id')
* @phpstan-method static Book&Proxy<Book> random(array $attributes = [])
* @phpstan-method static Book&Proxy<Book> randomOrCreate(array $attributes = [])
* @phpstan-method static list<Book&Proxy<Book>> all()
* @phpstan-method static list<Book&Proxy<Book>> createMany(int $number, array|callable $attributes = [])
* @phpstan-method static list<Book&Proxy<Book>> createSequence(iterable|callable $sequence)
* @phpstan-method static list<Book&Proxy<Book>> findBy(array $attributes)
* @phpstan-method static list<Book&Proxy<Book>> randomRange(int $min, int $max, array $attributes = [])
* @phpstan-method static list<Book&Proxy<Book>> randomSet(int $number, array $attributes = [])
* @phpstan-method FactoryCollection<Book&Proxy<Book>> many(int $min, int|null $max = null)
* @phpstan-method FactoryCollection<Book&Proxy<Book>> sequence(iterable|callable $sequence)
*
* @psalm-method Proxy<Book> create(array|callable $attributes = [])
* @psalm-method static Proxy<Book> createOne(array $attributes = [])
* @psalm-method static Proxy<Book> find(object|array|mixed $criteria)
* @psalm-method static Proxy<Book> findOrCreate(array $attributes)
* @psalm-method static Proxy<Book> first(string $sortedField = 'id')
* @psalm-method static Proxy<Book> last(string $sortedField = 'id')
* @psalm-method static Proxy<Book> random(array $attributes = [])
* @psalm-method static Proxy<Book> randomOrCreate(array $attributes = [])
* @psalm-method static RepositoryProxy<Book> repository()
* @psalm-method static list<Proxy<Book>> all()
* @psalm-method static list<Proxy<Book>> createMany(int $number, array|callable $attributes = [])
* @psalm-method static list<Proxy<Book>> createSequence(iterable|callable $sequence)
* @psalm-method static list<Proxy<Book>> findBy(array $attributes)
* @psalm-method static list<Proxy<Book>> randomRange(int $min, int $max, array $attributes = [])
* @psalm-method static list<Proxy<Book>> randomSet(int $number, array $attributes = [])
* @extends PersistentProxyObjectFactory<Book>
*/
final class BookFactory extends ModelFactory
final class BookFactory extends PersistentProxyObjectFactory
{
private array $data;

Expand All @@ -64,7 +68,7 @@ public function __construct()
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*/
protected function getDefaults(): array
protected function defaults(): array
{
return [
'condition' => self::faker()->randomElement(BookCondition::getCases()),
Expand All @@ -74,7 +78,7 @@ protected function getDefaults(): array
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
protected function initialize(): static
{
return $this
->afterInstantiate(function (Book $book): void {
Expand Down Expand Up @@ -110,7 +114,7 @@ protected function initialize(): self
;
}

protected static function getClass(): string
public static function class(): string
{
return Book::class;
}
Expand Down
97 changes: 42 additions & 55 deletions api/src/DataFixtures/Factory/BookmarkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,58 @@
namespace App\DataFixtures\Factory;

use App\Entity\Bookmark;
use Doctrine\ORM\EntityRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
use App\Repository\BookmarkRepository;
use Zenstruck\Foundry\FactoryCollection;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;

use function Zenstruck\Foundry\lazy;

/**
* @extends ModelFactory<Bookmark>
* @method Bookmark|Proxy create(array|callable $attributes = [])
* @method static Bookmark|Proxy createOne(array $attributes = [])
* @method static Bookmark|Proxy find(object|array|mixed $criteria)
* @method static Bookmark|Proxy findOrCreate(array $attributes)
* @method static Bookmark|Proxy first(string $sortedField = 'id')
* @method static Bookmark|Proxy last(string $sortedField = 'id')
* @method static Bookmark|Proxy random(array $attributes = [])
* @method static Bookmark|Proxy randomOrCreate(array $attributes = [])
* @method static Bookmark[]|Proxy[] all()
* @method static Bookmark[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Bookmark[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Bookmark[]|Proxy[] findBy(array $attributes)
* @method static Bookmark[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Bookmark[]|Proxy[] randomSet(int $number, array $attributes = [])
* @method FactoryCollection<Bookmark|Proxy> many(int $min, int|null $max = null)
* @method FactoryCollection<Bookmark|Proxy> sequence(iterable|callable $sequence)
* @method static ProxyRepositoryDecorator<Bookmark, BookmarkRepository> repository()
*
* @method Bookmark|Proxy create(array|callable $attributes = [])
* @method static Bookmark|Proxy createOne(array $attributes = [])
* @method static Bookmark|Proxy find(object|array|mixed $criteria)
* @method static Bookmark|Proxy findOrCreate(array $attributes)
* @method static Bookmark|Proxy first(string $sortedField = 'id')
* @method static Bookmark|Proxy last(string $sortedField = 'id')
* @method static Bookmark|Proxy random(array $attributes = [])
* @method static Bookmark|Proxy randomOrCreate(array $attributes = [])
* @method static EntityRepository|RepositoryProxy repository()
* @method static Bookmark[]|Proxy[] all()
* @method static Bookmark[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Bookmark[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Bookmark[]|Proxy[] findBy(array $attributes)
* @method static Bookmark[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Bookmark[]|Proxy[] randomSet(int $number, array $attributes = [])
* @phpstan-method Bookmark&Proxy<Bookmark> create(array|callable $attributes = [])
* @phpstan-method static Bookmark&Proxy<Bookmark> createOne(array $attributes = [])
* @phpstan-method static Bookmark&Proxy<Bookmark> find(object|array|mixed $criteria)
* @phpstan-method static Bookmark&Proxy<Bookmark> findOrCreate(array $attributes)
* @phpstan-method static Bookmark&Proxy<Bookmark> first(string $sortedField = 'id')
* @phpstan-method static Bookmark&Proxy<Bookmark> last(string $sortedField = 'id')
* @phpstan-method static Bookmark&Proxy<Bookmark> random(array $attributes = [])
* @phpstan-method static Bookmark&Proxy<Bookmark> randomOrCreate(array $attributes = [])
* @phpstan-method static list<Bookmark&Proxy<Bookmark>> all()
* @phpstan-method static list<Bookmark&Proxy<Bookmark>> createMany(int $number, array|callable $attributes = [])
* @phpstan-method static list<Bookmark&Proxy<Bookmark>> createSequence(iterable|callable $sequence)
* @phpstan-method static list<Bookmark&Proxy<Bookmark>> findBy(array $attributes)
* @phpstan-method static list<Bookmark&Proxy<Bookmark>> randomRange(int $min, int $max, array $attributes = [])
* @phpstan-method static list<Bookmark&Proxy<Bookmark>> randomSet(int $number, array $attributes = [])
* @phpstan-method FactoryCollection<Bookmark&Proxy<Bookmark>> many(int $min, int|null $max = null)
* @phpstan-method FactoryCollection<Bookmark&Proxy<Bookmark>> sequence(iterable|callable $sequence)
*
* @psalm-method Proxy<Bookmark> create(array|callable $attributes = [])
* @psalm-method static Proxy<Bookmark> createOne(array $attributes = [])
* @psalm-method static Proxy<Bookmark> find(object|array|mixed $criteria)
* @psalm-method static Proxy<Bookmark> findOrCreate(array $attributes)
* @psalm-method static Proxy<Bookmark> first(string $sortedField = 'id')
* @psalm-method static Proxy<Bookmark> last(string $sortedField = 'id')
* @psalm-method static Proxy<Bookmark> random(array $attributes = [])
* @psalm-method static Proxy<Bookmark> randomOrCreate(array $attributes = [])
* @psalm-method static RepositoryProxy<Bookmark> repository()
* @psalm-method static list<Proxy<Bookmark>> all()
* @psalm-method static list<Proxy<Bookmark>> createMany(int $number, array|callable $attributes = [])
* @psalm-method static list<Proxy<Bookmark>> createSequence(iterable|callable $sequence)
* @psalm-method static list<Proxy<Bookmark>> findBy(array $attributes)
* @psalm-method static list<Proxy<Bookmark>> randomRange(int $min, int $max, array $attributes = [])
* @psalm-method static list<Proxy<Bookmark>> randomSet(int $number, array $attributes = [])
* @extends PersistentProxyObjectFactory<Bookmark>
*/
final class BookmarkFactory extends ModelFactory
final class BookmarkFactory extends PersistentProxyObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*/
public function __construct()
{
parent::__construct();
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*/
protected function getDefaults(): array
protected function defaults(): array
{
return [
'user' => lazy(static fn () => UserFactory::new()),
Expand All @@ -69,16 +65,7 @@ protected function getDefaults(): array
];
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this;
// ->afterInstantiate(function(Bookmark $bookmark): void {})
}

protected static function getClass(): string
public static function class(): string
{
return Bookmark::class;
}
Expand Down
Loading

0 comments on commit 07ee4e4

Please sign in to comment.