Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #41 from localheinz/feature/faker-aware-definition
Browse files Browse the repository at this point in the history
Enhancement: Allow to provide definitions with an instance of Faker\Generator
  • Loading branch information
localheinz authored Feb 20, 2018
2 parents 4835ef0 + e752b3c commit 85c37a6
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 7 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ $ composer require --dev localheinz/factory-girl-definition

### Create Definitions

Implement the `Definition` interface and use the instance of `FactoryGirl\Provider\Doctrine\FixtureFactory`
that is passed in into `accept()` to define entities:
Implement one of the

* `Localheinz\FactoryGirl\Definition\Definition`
* `Localheinz\FactoryGirl\Definition\FakerAwareDefinition`

interfaces and use the instance of `FactoryGirl\Provider\Doctrine\FixtureFactory`
that is passed into `accept()` to define entities:

```php
<?php
Expand Down Expand Up @@ -48,7 +53,9 @@ 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 and register them with the factory:
and use `Definitions` to find definitions, register definitions with the
fixture factory, and optionally provide definitions with an instance of
`Faker\Generator`:

```php
<?php
Expand All @@ -57,6 +64,7 @@ namespace Foo\Bar\Test\Integration;

use Doctrine\ORM;
use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Faker\Generator;
use Localheinz\FactoryGirl\Definition\Definitions;
use PHPUnit\Framework;

Expand All @@ -72,13 +80,22 @@ abstract class AbstractIntegrationTestCase extends Framework\TestCase
// ...
}

final protected function faker(): Generator
{
// ...
}

final protected function fixtureFactory(): FixtureFactory
{
if (null === $this->fixtureFactory) {
$this->fixtureFactory = new FixtureFactory($this->entityManager());
$this->fixtureFactory->persistOnGet(true);
$fixtureFactory = new FixtureFactory($this->entityManager());
$fixtureFactory->persistOnGet(true);

Definitions::in(__DIR__ . '/../Fixture')
->registerWith($fixtureFactory)
->provideWith($this->faker());

Definitions::in(__DIR__ . '/../Fixture')->registerWith($this->fixtureFactory);
$this->fixtureFactory = $fixtureFactory;
}

return $this->fixtureFactory;
Expand Down
25 changes: 24 additions & 1 deletion src/Definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Localheinz\FactoryGirl\Definition;

use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Faker\Generator;
use Localheinz\Classy;

final class Definitions
Expand Down Expand Up @@ -79,11 +80,33 @@ public static function in(string $directory)
* Registers all found definitions with the specified fixture factory.
*
* @param FixtureFactory $fixtureFactory
*
* @return self
*/
public function registerWith(FixtureFactory $fixtureFactory)
public function registerWith(FixtureFactory $fixtureFactory): self
{
foreach ($this->definitions as $definition) {
$definition->accept($fixtureFactory);
}

return $this;
}

/**
* Provides all found definitions with the specified faker generator if they desire it.
*
* @param Generator $faker
*
* @return self
*/
public function provideWith(Generator $faker): self
{
foreach ($this->definitions as $definition) {
if ($definition instanceof FakerAwareDefinition) {
$definition->provideWith($faker);
}
}

return $this;
}
}
21 changes: 21 additions & 0 deletions src/FakerAwareDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2017 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @link https://github.com/localheinz/factory-girl-definition
*/

namespace Localheinz\FactoryGirl\Definition;

use Faker\Generator;

interface FakerAwareDefinition extends Definition
{
public function provideWith(Generator $faker);
}
42 changes: 42 additions & 0 deletions test/Fixture/Definition/FakerAware/GroupDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2017 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @link https://github.com/localheinz/factory-girl-definition
*/

namespace Localheinz\FactoryGirl\Definition\Test\Fixture\Definition\FakerAware;

use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Faker\Generator;
use Localheinz\FactoryGirl\Definition\FakerAwareDefinition;
use Localheinz\FactoryGirl\Definition\Test\Fixture\Entity;

final class GroupDefinition implements FakerAwareDefinition
{
/**
* @var Generator
*/
private $faker;

public function accept(FixtureFactory $factory)
{
$factory->defineEntity(Entity\Group::class);
}

public function provideWith(Generator $faker)
{
$this->faker = $faker;
}

public function faker()
{
return $this->faker;
}
}
26 changes: 26 additions & 0 deletions test/Fixture/Definition/FakerAware/UserDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2017 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @link https://github.com/localheinz/factory-girl-definition
*/

namespace Localheinz\FactoryGirl\Definition\Test\Fixture\Definition\FakerAware;

use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Localheinz\FactoryGirl\Definition\Definition;
use Localheinz\FactoryGirl\Definition\Test\Fixture\Entity;

final class UserDefinition implements Definition
{
public function accept(FixtureFactory $factory)
{
$factory->defineEntity(Entity\Group::class);
}
}
18 changes: 18 additions & 0 deletions test/Fixture/Entity/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2017 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @link https://github.com/localheinz/factory-girl-definition
*/

namespace Localheinz\FactoryGirl\Definition\Test\Fixture\Entity;

final class Group
{
}
43 changes: 43 additions & 0 deletions test/Unit/DefinitionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
namespace Localheinz\FactoryGirl\Definition\Test\Unit;

use FactoryGirl\Provider\Doctrine\FixtureFactory;
use Faker\Generator;
use Localheinz\FactoryGirl\Definition\Definition;
use Localheinz\FactoryGirl\Definition\Definitions;
use Localheinz\FactoryGirl\Definition\Exception;
use Localheinz\FactoryGirl\Definition\FakerAwareDefinition;
use Localheinz\FactoryGirl\Definition\Test\Fixture;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;
Expand Down Expand Up @@ -86,6 +89,46 @@ public function testInAcceptsClassesWhichAreAcceptable()
Definitions::in(__DIR__ . '/../Fixture/Definition/Acceptable')->registerWith($fixtureFactory->reveal());
}

public function testFluentInterface()
{
$definitions = Definitions::in(__DIR__ . '/../Fixture/Definition/Acceptable');

$this->assertInstanceOf(Definitions::class, $definitions);

$this->assertSame($definitions, $definitions->registerWith($this->prophesize(FixtureFactory::class)->reveal()));
$this->assertSame($definitions, $definitions->provideWith($this->prophesize(Generator::class)->reveal()));
}

public function testInAcceptsClassesWhichAreAcceptableAndFakerAwareAndProvidesThemWithFaker()
{
$faker = $this->prophesize(Generator::class);

$definitions = Definitions::in(__DIR__ . '/../Fixture/Definition/FakerAware')->provideWith($faker->reveal());

$reflection = new \ReflectionClass(Definitions::class);

$property = $reflection->getProperty('definitions');

$property->setAccessible(true);

$definitions = $property->getValue($definitions);

$this->assertInternalType('array', $definitions);

$fakerAwareDefinitions = \array_filter($definitions, function (Definition $definition) {
return $definition instanceof FakerAwareDefinition;
});

$this->assertCount(1, $fakerAwareDefinitions);
$this->assertContainsOnlyInstancesOf(FakerAwareDefinition::class, $fakerAwareDefinitions);

/** @var Fixture\Definition\FakerAware\GroupDefinition $fakerAwareDefinition */
$fakerAwareDefinition = \array_shift($fakerAwareDefinitions);

$this->assertInstanceOf(Fixture\Definition\FakerAware\GroupDefinition::class, $fakerAwareDefinition);
$this->assertSame($faker->reveal(), $fakerAwareDefinition->faker());
}

public function testThrowsInvalidDefinitionExceptionIfInstantiatingDefinitionsThrowsException()
{
$this->expectException(Exception\InvalidDefinition::class);
Expand Down
29 changes: 29 additions & 0 deletions test/Unit/FakerAwareDefinitionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2017 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @link https://github.com/localheinz/factory-girl-definition
*/

namespace Localheinz\FactoryGirl\Definition\Test\Unit;

use Localheinz\FactoryGirl\Definition\Definition;
use Localheinz\FactoryGirl\Definition\FakerAwareDefinition;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;

final class FakerAwareDefinitionTest extends Framework\TestCase
{
use Helper;

public function testExtendsDefinitionInterface()
{
$this->assertInterfaceExtends(Definition::class, FakerAwareDefinition::class);
}
}

0 comments on commit 85c37a6

Please sign in to comment.