ergebnis / factory-bot Goto Github PK
View Code? Open in Web Editor NEW๐ค Provides a composer package with a fixture factory for doctrine/orm entities.
License: MIT License
๐ค Provides a composer package with a fixture factory for doctrine/orm entities.
License: MIT License
When using factory-bot
in a framework one might want to use additional dependencies in their DefinitionProvider
s, allowing already instantiated providers to be loaded allows the framework to inject the required dependencies before passing the provider to the FixtureFactory
.
/cc @OskarStark
While I can use the following Entity with Doctrine, I cannot do so with a definition:
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Bridge\Doctrine\DBAL\Types\Type\Identifier\ElsterMappingIdType;
use App\Domain\Identifier\ElsterMappingId;
use App\Entity\Interfaces\UlidIdentifier;
use App\Entity\Traits\CreatedTimestampTrait;
use App\Entity\Traits\UpdatedTimestampTrait;
use App\Repository\ElsterMappingRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use Safe\DateTimeImmutable;
use Symfony\Component\Validator\Constraints\Length;
#[Table(name: 'elster_mappings')]
#[Entity(repositoryClass: ElsterMappingRepository::class)]
class ElsterMapping implements \Stringable, UlidIdentifier
{
use CreatedTimestampTrait;
use UpdatedTimestampTrait;
#[Id]
#[Column(type: ElsterMappingIdType::class, unique: true)]
private ElsterMappingId $id;
#[Column(type: Types::STRING, unique: true)]
private string $from;
#[Column(type: Types::STRING)]
#[Length(max: 25)]
private string $to;
public function __construct()
{
$this->id = new ElsterMappingId();
$this->createdTimestamp = new DateTimeImmutable();
$this->updatedTimestamp = new DateTimeImmutable();
}
public function __toString(): string
{
return sprintf('%s -> %s', $this->from, $this->to);
}
public function getId(): ElsterMappingId
{
return $this->id;
}
public function getFrom(): string
{
return $this->from;
}
public function setFrom(string $from): void
{
$this->from = $from;
}
public function getTo(): string
{
return $this->to;
}
public function setTo(string $to): void
{
$this->to = $to;
}
}
<?php
declare(strict_types=1);
namespace App\Tests\Fixture\Entity;
use App\Domain\Identifier\ElsterMappingId;
use App\Entity\ElsterMapping;
use Ergebnis\FactoryBot\EntityDefinitionProvider;
use Ergebnis\FactoryBot\FieldDefinition;
use Ergebnis\FactoryBot\FixtureFactory;
use Faker\Generator;
use function Symfony\Component\String\u;
/**
* @see \App\Entity\ElsterMapping
*/
final class ElsterMappingDefinition implements EntityDefinitionProvider
{
public function accept(FixtureFactory $fixtureFactory): void
{
$fixtureFactory->define(
ElsterMapping::class,
[
'id' => FieldDefinition::closure(static fn (Generator $faker): ElsterMappingId => new ElsterMappingId()),
'from' => FieldDefinition::closure(static fn (Generator $faker): string => $faker->sentence()),
'to' => FieldDefinition::closure(static fn (Generator $faker): string => u($faker->sentence())->truncate(25)->toString()),
'createdTimestamp' => FieldDefinition::closure(static fn (Generator $faker): \DateTimeImmutable => \DateTimeImmutable::createFromMutable($faker->dateTime())),
'updatedTimestamp' => FieldDefinition::closure(static fn (Generator $faker): \DateTimeImmutable => \DateTimeImmutable::createFromMutable($faker->dateTime())),
],
);
}
}
Executing the following tests, results in an error:
<?php
declare(strict_types=1);
namespace App\Tests\Integration\Entity;
use App\Entity\ElsterMapping;
use App\Tests\Integration\DatabaseTestCase;
/**
* @covers \App\Entity\ElsterMapping
*/
final class ElsterMappingTest extends DatabaseTestCase
{
protected function getEntityClass(): string
{
return ElsterMapping::class;
}
}
<?php
declare(strict_types=1);
namespace App\Tests\Integration;
abstract class DatabaseTestCase extends IntegrationTestCase
{
/**
* @test
*/
final public function canPersistBarWithoutOptionalFields(): void
{
$entity = self::persistingFixtureFactory()->withoutOptional()->createOne($this->getEntityClass());
$entityManager = self::entityManager();
$entityManager->flush();
$entityManager->clear();
self::assertInstanceOf(
$this->getEntityClass(),
self::entityManager()->getRepository($this->getEntityClass())->find($entity->getId()),
);
}
/**
* @test
*/
final public function canPersistBarWithOptionalFields(): void
{
$entity = self::persistingFixtureFactory()->withOptional()->createOne($this->getEntityClass());
$entityManager = self::entityManager();
$entityManager->flush();
$entityManager->clear();
self::assertInstanceOf(
$this->getEntityClass(),
self::entityManager()->getRepository($this->getEntityClass())->find($entity->getId()),
);
}
/**
* @return class-string
*/
abstract protected function getEntityClass(): string;
}
breerly/factory-girl-php
offers a Singletons functionality which was removed in #124.
Per request of @greg0ire, it should be considered to restore this functionality.
Sometimes data in a column is dependent on data in another column, passing already resolved fieldDefinitions would allow the user to handle the use cases where this is the case, without having to use the afterCreate
callback.
Hello!
Is there is a way to create factory variants ?
As an example, I have some Post
with several possible states, based on multiple field values (ex: published_by_id
+ published_at
= published
state).
As I see how define
works now, maybe the easiest way would be to add a name
parameter (define($className, $fieldDefinitions, $afterCreate, $name)
so we can use it like
// Factory
// -----------------------
$baseFields = [
// ... common fields
];
$fixtureFactory->define(Entity\Post::class, $baseFields);
$fixtureFactory->define(Entity\Post::class, array_merge($baseFields, [
// Overrides and/or new fields
], null, 'published'));
// In tests
// -----------------------
$this->fixtureFactory()->createOne(Post, 'published');
What do you think ?
As the min php version is 7.3 it is not necessary to install polyfills for 7.2 and 7.3.
Sorry, my code problem.
When creating an entity via the factory, the factory allows one to override the default value set, I propose also allowing that when referencing other entities in the factory definition as the user might want to set custom behavior based on the reference.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.