This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from localheinz/feature/abstract-definition
Enhancement: Provide AbstractDefinition which partially implements the FakerAwareDefinition
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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; | ||
|
||
abstract class AbstractDefinition implements FakerAwareDefinition | ||
{ | ||
/** | ||
* @var Generator | ||
*/ | ||
private $faker; | ||
|
||
final public function provideWith(Generator $faker) | ||
{ | ||
$this->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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/Fixture/Definition/ExtendsAbstractDefinition/UserDefinition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\ExtendsAbstractDefinition; | ||
|
||
use FactoryGirl\Provider\Doctrine\FixtureFactory; | ||
use Localheinz\FactoryGirl\Definition\AbstractDefinition; | ||
use Localheinz\FactoryGirl\Definition\Test\Fixture\Entity; | ||
|
||
final class UserDefinition extends AbstractDefinition | ||
{ | ||
public function accept(FixtureFactory $factory) | ||
{ | ||
$factory->defineEntity(Entity\User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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 Faker\Generator; | ||
use Localheinz\FactoryGirl\Definition\AbstractDefinition; | ||
use Localheinz\FactoryGirl\Definition\Definition; | ||
use Localheinz\FactoryGirl\Definition\FakerAwareDefinition; | ||
use Localheinz\FactoryGirl\Definition\Test\Fixture; | ||
use Localheinz\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
final class AbstractDefinitionTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
public function testImplementsFakerAwareDefinitionInterface() | ||
{ | ||
$this->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()); | ||
} | ||
} |