ergebnis / factory-girl-definition
Provides an interface for, and an easy way to find and register entity definitions for breerly/factory-girl-php.
Fund package maintenance!
localheinz
Installs: 52 115
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.2
- breerly/factory-girl-php: ^1.0.3
- ergebnis/classy: ~0.5.0
- fzaninotto/faker: ^1.9.0
Requires (Dev)
- ergebnis/php-cs-fixer-config: ~1.0.0
- ergebnis/phpstan-rules: ~0.14.0
- ergebnis/test-util: ~0.9.0
- infection/infection: ~0.14.2
- jangregor/phpstan-prophecy: ~0.4.2
- localheinz/composer-normalize: ^1.3.1
- phpstan/extension-installer: ^1.0.3
- phpstan/phpstan: ~0.11.19
- phpstan/phpstan-deprecation-rules: ~0.11.2
- phpstan/phpstan-strict-rules: ~0.11.1
- phpunit/phpunit: ^8.4.3
Suggests
- breerly/factory-girl-php: For creating a fixture factory the definitions can be used with.
README
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.