-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CreatesObjects concern to create objects using factory muffing (#38)
- Loading branch information
Showing
4 changed files
with
91 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Gricob\FunctionalTestBundle\Concerns; | ||
|
||
use League\FactoryMuffin\FactoryMuffin; | ||
use League\FactoryMuffin\Stores\RepositoryStore; | ||
use Symfony\Component\DependencyInjection\Container; | ||
|
||
/** | ||
* @author Gerard Rico <[email protected]> | ||
*/ | ||
trait CreatesObjects | ||
{ | ||
/** | ||
* @var FactoryMuffin | ||
*/ | ||
protected static $factory = null; | ||
|
||
protected function instance(string $entityClass, array $attributes = []) | ||
{ | ||
return $this->getFactory()->instance($entityClass, $attributes); | ||
} | ||
|
||
protected function create(string $entityClass, array $attributes = []) | ||
{ | ||
return $this->getFactory()->create($entityClass, $attributes); | ||
} | ||
|
||
protected function seed(string $entityClass, int $times, array $attributes = []) | ||
{ | ||
return $this->getFactory()->seed($times, $entityClass, $attributes); | ||
} | ||
|
||
protected function getFactory(): FactoryMuffin | ||
{ | ||
if (is_null(static::$factory)) { | ||
$container = $this->getContainer(); | ||
$doctrine = $container->get('doctrine'); | ||
|
||
if (!$doctrine) { | ||
throw new \LogicException('Doctrine is required to create objects.'); | ||
} | ||
|
||
self::$factory = new FactoryMuffin(new RepositoryStore($doctrine->getManager())); | ||
|
||
self::$factory->loadFactories($container->getParameter('kernel.root_dir').'/factories'); | ||
} | ||
|
||
return self::$factory; | ||
} | ||
|
||
abstract function getContainer(): Container; | ||
} |
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,9 @@ | ||
<?php | ||
use League\FactoryMuffin\FactoryMuffin; | ||
use Tests\App\Entity\User; | ||
|
||
/** @var FactoryMuffin $fm */ | ||
$fm->define(User::class)->setDefinitions([ | ||
'id' => rand(1,10), | ||
'username' => 'test-username' | ||
]); |
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