Coder Social home page Coder Social logo

factory-bot's People

Contributors

abenerd avatar alexmart avatar dependabot-preview[bot] avatar dependabot[bot] avatar ergebnis-bot avatar localheinz avatar oskarstark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

factory-bot's Issues

Allow loading of already instantiated EntityDefinitionProviders

When using factory-bot in a framework one might want to use additional dependencies in their DefinitionProviders, allowing already instantiated providers to be loaded allows the framework to inject the required dependencies before passing the provider to the FixtureFactory.

Cannot use reserved keywords (`from` and `to`) for properties

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;
}

Errors

CleanShot 2022-11-03 at 22 25 51@2x

Possibility to create variants of the same entity?

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 ?

Allow FieldDefinition::reference to override default values

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.