Skip to content

Commit

Permalink
Merge pull request #702 from ergebnis/feature/register
Browse files Browse the repository at this point in the history
Enhancement: Allow registering already instantiated entity definition providers
  • Loading branch information
localheinz committed Dec 27, 2021
2 parents 6043661 + f08c837 commit bb7f944
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
34 changes: 34 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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
<?php

namespace App\Test\Functional;

use Ergebnis\FactoryBot;
use Example\Test\Fixture;
use PHPUnit\Framework;

abstract class AbstractTestCase extends Framework\TestCase
{
// ...

final protected static function fixtureFactory(): FactoryBot\FixtureFactory
{
$fixtureFactory = new FactoryBot\FixtureFactory(
static::entityManager(),
static::faker()
);

$fixtureFactory->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.
Expand Down
6 changes: 3 additions & 3 deletions phpstan-baseline.neon
Expand Up @@ -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

-
Expand All @@ -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

-
Expand Down
7 changes: 7 additions & 0 deletions src/FixtureFactory.php
Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions test/Unit/FixtureFactoryTest.php
Expand Up @@ -19,6 +19,7 @@
use Ergebnis\FactoryBot\FixtureFactory;
use Ergebnis\FactoryBot\Test;
use Example\Entity;
use Example\Test\Fixture;
use Faker\Generator;

/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bb7f944

Please sign in to comment.