Skip to content

Commit

Permalink
Enhancement: Throw EntityDefinitionAlreadyRegistered exception
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Mar 28, 2020
1 parent ed73b90 commit 75ae235
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ For a full diff see [`fa9c564...master`][fa9c564...master].
* Renamed `FieldDef` to `FieldDefinition` ([#92]), by [@localheinz]
* Turned `$configuration` parameter of `FixtureFactory::defineEntity()` into `$afterCreate`, a `Closure` that will be invoked after object construction ([#101]), by [@localheinz]
* Started throwing an `InvalidCount` exception instead of a generic `Exception` when an invalid number of entities are requested ([#105]), by [@localheinz]
* Started throwing an `EntityDefinitionAlreadyRegistered` exception instead of a generic `Exception` when an entity definition for a class name has already been registered ([#106]), by [@localheinz]

### Fixed

Expand All @@ -51,5 +52,6 @@ For a full diff see [`fa9c564...master`][fa9c564...master].
[#92]: https://github.com/ergebnis/factory-bot/pull/92
[#101]: https://github.com/ergebnis/factory-bot/pull/101
[#105]: https://github.com/ergebnis/factory-bot/pull/105
[#106]: https://github.com/ergebnis/factory-bot/pull/106

[@localheinz]: https://github.com/localheinz
25 changes: 25 additions & 0 deletions src/Exception/EntityDefinitionAlreadyRegistered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/factory-bot
*/

namespace Ergebnis\FactoryBot\Exception;

final class EntityDefinitionAlreadyRegistered extends \RuntimeException implements Exception
{
public static function for(string $className): self
{
return new self(\sprintf(
'An entity definition for class name "%s" has already been registered.',
$className
));
}
}
6 changes: 2 additions & 4 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct(ORM\EntityManagerInterface $entityManager)
* @param array $fieldDefinitions
* @param \Closure $afterCreate
*
* @throws Exception\EntityDefinitionAlreadyRegistered
* @throws Exception\InvalidFieldNames
* @throws \Exception
*
Expand All @@ -62,10 +63,7 @@ public function __construct(ORM\EntityManagerInterface $entityManager)
public function defineEntity(string $className, array $fieldDefinitions = [], ?\Closure $afterCreate = null)
{
if (\array_key_exists($className, $this->entityDefinitions)) {
throw new \Exception(\sprintf(
"Entity '%s' already defined in fixture factory",
$className
));
throw Exception\EntityDefinitionAlreadyRegistered::for($className);
}

if (!\class_exists($className, true)) {
Expand Down
40 changes: 40 additions & 0 deletions test/Unit/Exception/EntityDefinitionAlreadyRegisteredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/factory-bot
*/

namespace Ergebnis\FactoryBot\Test\Unit\Exception;

use Ergebnis\FactoryBot\Exception;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\FactoryBot\Exception\EntityDefinitionAlreadyRegistered
*/
final class EntityDefinitionAlreadyRegisteredTest extends Framework\TestCase
{
public function testForReturnsException(): void
{
$className = self::class;

$exception = Exception\EntityDefinitionAlreadyRegistered::for($className);

$message = \sprintf(
'An entity definition for class name "%s" has already been registered.',
$className
);

self::assertSame($message, $exception->getMessage());
self::assertSame(0, $exception->getCode());
}
}
5 changes: 3 additions & 2 deletions test/Unit/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @covers \Ergebnis\FactoryBot\FixtureFactory
*
* @uses \Ergebnis\FactoryBot\EntityDefinition
* @uses \Ergebnis\FactoryBot\Exception\EntityDefinitionAlreadyRegistered
* @uses \Ergebnis\FactoryBot\Exception\EntityDefinitionUnavailable
* @uses \Ergebnis\FactoryBot\Exception\InvalidCount
* @uses \Ergebnis\FactoryBot\Exception\InvalidFieldNames
Expand All @@ -41,9 +42,9 @@ public function testDefineEntityThrowsExceptionWhenDefinitionHasAlreadyBeenProvi

$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Organization::class);

$this->expectException(\Exception::class);
$this->expectException(Exception\EntityDefinitionAlreadyRegistered::class);
$this->expectExceptionMessage(\sprintf(
'Entity \'%s\' already defined in fixture factory',
'An entity definition for class name "%s" has already been registered.',
Fixture\FixtureFactory\Entity\Organization::class
));

Expand Down

0 comments on commit 75ae235

Please sign in to comment.