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 #1 from localheinz/feature/provider
Enhancement: Add interface and finder
- Loading branch information
Showing
18 changed files
with
665 additions
and
5 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
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
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
Empty file.
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,19 @@ | ||
<?php | ||
|
||
/** | ||
* 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 FactoryGirl\Provider\Doctrine\FixtureFactory; | ||
|
||
interface Definition | ||
{ | ||
public function accept(FixtureFactory $fixtureFactory); | ||
} |
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,87 @@ | ||
<?php | ||
|
||
/** | ||
* 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 FactoryGirl\Provider\Doctrine\FixtureFactory; | ||
use Zend\File; | ||
|
||
final class Definitions | ||
{ | ||
/** | ||
* @var Definition[] | ||
*/ | ||
private $definitions = []; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $directory | ||
* | ||
* @throws Exception\InvalidDirectory | ||
* @throws Exception\InvalidDefinition | ||
* | ||
* @return self | ||
*/ | ||
public static function in($directory) | ||
{ | ||
if (!\is_string($directory)) { | ||
throw Exception\InvalidDirectory::notString($directory); | ||
} | ||
|
||
if (!\is_dir($directory)) { | ||
throw Exception\InvalidDirectory::notDirectory($directory); | ||
} | ||
|
||
$locator = new File\ClassFileLocator($directory); | ||
|
||
/** @var File\PhpClassFile[] $files */ | ||
$files = \iterator_to_array($locator); | ||
|
||
$instance = new self(); | ||
|
||
foreach ($files as $file) { | ||
foreach ($file->getClasses() as $className) { | ||
try { | ||
$reflection = new \ReflectionClass($className); | ||
} catch (\ReflectionException $exception) { | ||
continue; | ||
} | ||
|
||
if (!$reflection->isSubclassOf(Definition::class) || $reflection->isAbstract()) { | ||
continue; | ||
} | ||
|
||
try { | ||
$definition = $reflection->newInstance(); | ||
} catch (\Exception $exception) { | ||
throw Exception\InvalidDefinition::fromClassNameAndException( | ||
$className, | ||
$exception | ||
); | ||
} | ||
|
||
$instance->definitions[] = $definition; | ||
} | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
public function registerWith(FixtureFactory $fixtureFactory) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
$definition->accept($fixtureFactory); | ||
} | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
/** | ||
* 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\Exception; | ||
|
||
final class InvalidDefinition extends \RuntimeException | ||
{ | ||
/** | ||
* @param string $className | ||
* @param \Exception $exception | ||
* | ||
* @return self | ||
*/ | ||
public static function fromClassNameAndException($className, \Exception $exception) | ||
{ | ||
return new self( | ||
\sprintf( | ||
'An exception was thrown while trying to instantiate definition "%s".', | ||
$className | ||
), | ||
null, | ||
$exception | ||
); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
/** | ||
* 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\Exception; | ||
|
||
final class InvalidDirectory extends \InvalidArgumentException | ||
{ | ||
/** | ||
* @param mixed $directory | ||
* | ||
* @return self | ||
*/ | ||
public static function notString($directory) | ||
{ | ||
return new self(\sprintf( | ||
'Directory should be a string, got %s instead.', | ||
\is_object($directory) ? \get_class($directory) : \gettype($directory) | ||
)); | ||
} | ||
|
||
/** | ||
* @param string $directory | ||
* | ||
* @return self | ||
*/ | ||
public static function notDirectory($directory) | ||
{ | ||
return new self(\sprintf( | ||
'Directory should be a directory, but "%s" is not.', | ||
$directory | ||
)); | ||
} | ||
} |
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 | ||
|
||
/** | ||
* 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\Asset\Definition\Acceptable; | ||
|
||
use FactoryGirl\Provider\Doctrine\FixtureFactory; | ||
use Localheinz\FactoryGirl\Definition\Definition; | ||
|
||
/** | ||
* Is acceptable as it implements the ProviderInterface. | ||
*/ | ||
final class UserDefinition implements Definition | ||
{ | ||
public function accept(FixtureFactory $factory) | ||
{ | ||
$factory->defineEntity('Foo'); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/Unit/Asset/Definition/CanNotBeAutoloaded/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 | ||
|
||
/** | ||
* 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\Asset\Definition\CanNotBeAutoloaded; | ||
|
||
use FactoryGirl\Provider\Doctrine\FixtureFactory; | ||
use Localheinz\FactoryGirl\Definition\Definition; | ||
|
||
/** | ||
* Is not acceptable as it can not be autoloaded (class name does not match file name). | ||
*/ | ||
final class MaybeUserDefinition implements Definition | ||
{ | ||
public function accept(FixtureFactory $factory) | ||
{ | ||
$factory->defineEntity('Foo'); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/Unit/Asset/Definition/DoesNotImplementInterface/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,25 @@ | ||
<?php | ||
|
||
/** | ||
* 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\Asset\Definition\DoesNotImplementInterface; | ||
|
||
use FactoryGirl\Provider\Doctrine\FixtureFactory; | ||
|
||
/** | ||
* Is not acceptable as it does not implement the DefinitionInterface. | ||
*/ | ||
final class UserDefinition | ||
{ | ||
public function accept(FixtureFactory $factory) | ||
{ | ||
$factory->defineEntity('Foo'); | ||
} | ||
} |
Oops, something went wrong.