Coder Social home page Coder Social logo

ergebnis / factory-girl-definition Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 1.41 MB

:girl: Provides an interface for, and an easy way to find and register entity definitions for breerly/factory-girl-php.

License: MIT License

PHP 90.56% Makefile 9.44%
factory-girl factory-girl-definition

factory-girl-definition's Introduction

factory-girl-definition

Integrate Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads

Provides an interface for, and an easy way to find and register entity definitions for breerly/factory-girl-php.

Installation

Run

$ composer require --dev ergebnis/factory-girl-definition

Usage

Create Definitions

Implement one of the

  • Ergebnis\FactoryGirl\Definition\Definition
  • Ergebnis\FactoryGirl\Definition\FakerAwareDefinition

interfaces and use the instance of FactoryGirl\Provider\Doctrine\FixtureFactory that is passed into accept() to define entities:

<?php

namespace Foo\Bar\Test\Fixture\Entity;

use Ergebnis\FactoryGirl\Definition\Definition;
use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Foo\Bar\Entity;

final class UserDefinition implements Definition
{
    public function accept(FixtureFactory $fixtureFactory): void
    {
        $fixtureFactory->defineEntity(Entity\User::class, [
            // ...
        ]);
    }
}

๐Ÿ’ก Any number of entities can be defined within a definition. However, it's probably a good idea to create a definition for each entity.

Register Definitions

Lazily instantiate an instance of FactoryGirl\Provider\Doctrine\FixtureFactory and use Definitions to find definitions, register definitions with the fixture factory, and optionally provide definitions with an instance of Faker\Generator:

<?php

namespace Foo\Bar\Test\Integration;

use Doctrine\ORM;
use Ergebnis\FactoryGirl\Definition\Definitions;
use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Faker\Generator;
use PHPUnit\Framework;

abstract class AbstractIntegrationTestCase extends Framework\TestCase
{
    /**
     * @var FixtureFactory
     */
    private $fixtureFactory;

    final protected function entityManager(): ORM\EntityManager
    {
        // ...
    }

    final protected function faker(): Generator
    {
        // ...
    }

    final protected function fixtureFactory(): FixtureFactory
    {
        if (null === $this->fixtureFactory) {
            $fixtureFactory = new FixtureFactory($this->entityManager());
            $fixtureFactory->persistOnGet(true);

            Definitions::in(__DIR__ . '/../Fixture')
                ->registerWith($fixtureFactory)
                ->provideWith($this->faker());

            $this->fixtureFactory = $fixtureFactory;
        }

        return $this->fixtureFactory;
    }
}

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CODE_OF_CONDUCT.md.

This package is licensed using the MIT License.

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

Curious what I am building?

๐Ÿ“ฌ Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.

factory-girl-definition's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar ergebnis-bot avatar localheinz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.