diff --git a/src/AbstractDefinition.php b/src/AbstractDefinition.php new file mode 100644 index 0000000..5e3f610 --- /dev/null +++ b/src/AbstractDefinition.php @@ -0,0 +1,46 @@ +faker = $faker; + } + + /** + * @throws \BadMethodCallException + * + * @return Generator + */ + final public function faker(): Generator + { + if (null === $this->faker) { + throw new \BadMethodCallException(\sprintf( + 'Before accessing, an instance of "%s" needs to be provided using provideWith()', + Generator::class + )); + } + + return $this->faker; + } +} diff --git a/test/Fixture/Definition/ExtendsAbstractDefinition/UserDefinition.php b/test/Fixture/Definition/ExtendsAbstractDefinition/UserDefinition.php new file mode 100644 index 0000000..c43eb7f --- /dev/null +++ b/test/Fixture/Definition/ExtendsAbstractDefinition/UserDefinition.php @@ -0,0 +1,26 @@ +defineEntity(Entity\User::class); + } +} diff --git a/test/Unit/AbstractDefinitionTest.php b/test/Unit/AbstractDefinitionTest.php new file mode 100644 index 0000000..86f0224 --- /dev/null +++ b/test/Unit/AbstractDefinitionTest.php @@ -0,0 +1,56 @@ +assertClassImplementsInterface(FakerAwareDefinition::class, AbstractDefinition::class); + } + + public function testFakerThrowsBadMethodCallExceptionIfDefinitionHasNotBeenProvidedWithFaker() + { + $definition = new Fixture\Definition\ExtendsAbstractDefinition\UserDefinition(); + + $this->expectException(\BadMethodCallException::class); + $this->expectExceptionMessage(\sprintf( + 'Before accessing, an instance of "%s" needs to be provided using provideWith()', + Generator::class + )); + + $definition->faker(); + } + + public function testFakerReturnsFakerWhenProvidedWithIt() + { + $faker = new Generator(); + + $definition = new Fixture\Definition\ExtendsAbstractDefinition\UserDefinition(); + + $definition->provideWith($faker); + + $this->assertSame($faker, $definition->faker()); + } +}