From f08c837228cebc6ec05e2f83cd723dd7370829cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 27 Dec 2021 19:43:53 +0100 Subject: [PATCH] Enhancement: Allow registering already instantiated entity definition providers --- CHANGELOG.md | 15 ++++++++++++-- README.md | 34 ++++++++++++++++++++++++++++++++ phpstan-baseline.neon | 6 +++--- src/FixtureFactory.php | 7 +++++++ test/Unit/FixtureFactoryTest.php | 25 +++++++++++++++++++++++ 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 745dcd37..cd89ce62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`0.5.0...main`][0.5.0...main]. +For a full diff see [`0.6.0...main`][0.6.0...main]. + +## [`0.6.0`][0.6.0] + +For a full diff see [`0.5.0...0.6.0`][0.5.0...0.6.0]. + +### Added + +- Allowed registering already instantiated entity definition providers ([#702]), by [@localheinz] ## [`0.5.0`][0.5.0] @@ -171,6 +179,7 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0]. [0.3.2]: https://github.com/ergebnis/factory-bot/releases/tag/0.3.2 [0.4.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.4.0 [0.5.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.5.0 +[0.6.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.6.0 [fa9c564...0.1.0]: https://github.com/ergebnis/factory-bot/compare/fa9c564...0.1.0 [0.1.0...0.2.0]: https://github.com/ergebnis/factory-bot/compare/0.1.0...0.2.0 @@ -180,7 +189,8 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0]. [0.3.1...0.3.2]: https://github.com/ergebnis/factory-bot/compare/0.3.1...0.3.2 [0.3.2...0.4.0]: https://github.com/ergebnis/factory-bot/compare/0.3.2...0.4.0 [0.4.0...0.5.0]: https://github.com/ergebnis/factory-bot/compare/0.4.0...0.5.0 -[0.5.0...main]: https://github.com/ergebnis/factory-bot/compare/0.5.0...main +[0.5.0...0.6.0]: https://github.com/ergebnis/factory-bot/compare/0.5.0...0.6.0 +[0.6.0...main]: https://github.com/ergebnis/factory-bot/compare/0.6.0...main [#1]: https://github.com/ergebnis/factory-bot/pull/1 [#3]: https://github.com/ergebnis/factory-bot/pull/3 @@ -260,6 +270,7 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0]. [#498]: https://github.com/ergebnis/factory-bot/pull/498 [#499]: https://github.com/ergebnis/factory-bot/pull/499 [#682]: https://github.com/ergebnis/factory-bot/pull/682 +[#702]: https://github.com/ergebnis/factory-bot/pull/702 [@abenerd]: https://github.com/abenerd [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index c5056b0d..9a5b8034 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ You will use the fixture factory to create entity definitions and to create Doct - [Creating a fixture factory](#creating-a-fixture-factory) - [Creating entity definitions](#creating-entity-definitions) - [Loading entity definitions](#loading-entity-definitions) +- [Registering entity definitions](#registering-entity-definitions) - [Creating entities](#creating-entities) - [Persisting entities](#persisting-entities) - [Flushing entities](#flushing-entities) @@ -889,6 +890,39 @@ abstract class AbstractTestCase extends Framework\TestCase } ``` +### Registering entity definitions + +Instead of loading entity definition providers contained within a directory with the fixture factory, you can also register entity definition providers that you have already instantiated. + +```php +register(new Fixture\UserDefinitionProvider()); + + return $fixtureFactory; + } + + // ... +} +``` + ### Creating entities Now that you have created (or loaded) entity definitions, you can create Doctrine entities populated with fake data. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 36a43270..591d0754 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -377,7 +377,7 @@ parameters: - message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Example\\\\\\\\Entity\\\\\\\\Organization' and Example\\\\Entity\\\\Organization will always evaluate to true\\.$#" - count: 2 + count: 3 path: test/Unit/FixtureFactoryTest.php - @@ -387,12 +387,12 @@ parameters: - message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Example\\\\\\\\Entity\\\\\\\\Repository' and Example\\\\Entity\\\\Repository will always evaluate to true\\.$#" - count: 2 + count: 3 path: test/Unit/FixtureFactoryTest.php - message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Example\\\\\\\\Entity\\\\\\\\User' and Example\\\\Entity\\\\User will always evaluate to true\\.$#" - count: 2 + count: 3 path: test/Unit/FixtureFactoryTest.php - diff --git a/src/FixtureFactory.php b/src/FixtureFactory.php index c5983059..ce23b7a9 100644 --- a/src/FixtureFactory.php +++ b/src/FixtureFactory.php @@ -178,6 +178,13 @@ public function load(string $directory): void } } + public function register(EntityDefinitionProvider ...$providers): void + { + foreach ($providers as $provider) { + $provider->accept($this); + } + } + /** * Creates a single entity with all of its dependencies. * diff --git a/test/Unit/FixtureFactoryTest.php b/test/Unit/FixtureFactoryTest.php index 95376636..9d7034b8 100644 --- a/test/Unit/FixtureFactoryTest.php +++ b/test/Unit/FixtureFactoryTest.php @@ -19,6 +19,7 @@ use Ergebnis\FactoryBot\FixtureFactory; use Ergebnis\FactoryBot\Test; use Example\Entity; +use Example\Test\Fixture; use Faker\Generator; /** @@ -219,6 +220,30 @@ public function testLoadAcceptsClassesWhichImplementDefinitionProviderInterfaceA self::assertInstanceOf(Entity\User::class, $user); } + public function testRegisterAcceptsDefinitionProviders(): void + { + $fixtureFactory = new FixtureFactory( + self::entityManager(), + self::faker(), + ); + + $fixtureFactory->register( + new Fixture\Entity\AvatarDefinitionProvider(), + new Fixture\Entity\CodeOfConductDefinitionProvider(), + new Fixture\Entity\OrganizationDefinitionProvider(), + new Fixture\Entity\RepositoryDefinitionProvider(), + new Fixture\Entity\UserDefinitionProvider(), + ); + + $organization = $fixtureFactory->createOne(Entity\Organization::class); + $repository = $fixtureFactory->createOne(Entity\Repository::class); + $user = $fixtureFactory->createOne(Entity\User::class); + + self::assertInstanceOf(Entity\Organization::class, $organization); + self::assertInstanceOf(Entity\Repository::class, $repository); + self::assertInstanceOf(Entity\User::class, $user); + } + public function testCreateOneThrowsEntityDefinitionNotRegisteredWhenEntityDefinitionHasNotBeenRegistered(): void { $className = self::class;