diff --git a/composer.json b/composer.json index bf585d6..253383c 100644 --- a/composer.json +++ b/composer.json @@ -26,11 +26,13 @@ "symfony/var-dumper": "^4.4", "symfony/dom-crawler": "^4.0", "symfony/translation-contracts": "^1.0", - "symfony/monolog-bundle": "^3.5" + "symfony/monolog-bundle": "^3.5", + "league/factory-muffin": "^3.1" }, "suggest": { - "doctrine/doctrine-fixtures-bundle": "Required to perform loadFixtures", - "twig/twig": "Required to perform twig templates related assertion" + "doctrine/doctrine-fixtures-bundle": "Required to load fixtures", + "twig/twig": "Required to perform twig templates related assertions", + "league/factory-muffin": "Required to create objects using the factory" }, "autoload": { "psr-4": { @@ -41,5 +43,8 @@ "psr-4": { "Tests\\": "tests/" } + }, + "conflict": { + "doctrine/lexer": ">1.0.2" } } diff --git a/src/Concerns/CreatesObjects.php b/src/Concerns/CreatesObjects.php new file mode 100644 index 0000000..c6770cf --- /dev/null +++ b/src/Concerns/CreatesObjects.php @@ -0,0 +1,53 @@ + + */ +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; +} \ No newline at end of file diff --git a/tests/App/factories/UserFactory.php b/tests/App/factories/UserFactory.php new file mode 100644 index 0000000..fd82ccf --- /dev/null +++ b/tests/App/factories/UserFactory.php @@ -0,0 +1,9 @@ +define(User::class)->setDefinitions([ + 'id' => rand(1,10), + 'username' => 'test-username' +]); \ No newline at end of file diff --git a/tests/FunctionalTestCaseTest.php b/tests/FunctionalTestCaseTest.php index d118c61..e9096ea 100644 --- a/tests/FunctionalTestCaseTest.php +++ b/tests/FunctionalTestCaseTest.php @@ -3,6 +3,7 @@ namespace Tests; use Exception; +use Gricob\FunctionalTestBundle\Concerns\CreatesObjects; use Gricob\FunctionalTestBundle\Testing\RefreshDatabase; use Gricob\FunctionalTestBundle\Testing\FunctionalTestCase; use Gricob\FunctionalTestBundle\Testing\TestResponse; @@ -22,7 +23,8 @@ class FunctionalTestCaseTest extends FunctionalTestCase { - use RefreshDatabase; + use RefreshDatabase, + CreatesObjects; protected static function getKernelClass() { @@ -246,6 +248,24 @@ public function testContainerCanGetUnusedServiceWhenUsingPreventRemoveUnusedDefi $this->assertInstanceOf(UnusedService::class, $container->get('test.unused_private_service')); } + public function testInstanceEntity() + { + $user = $this->instance(User::class); + + $this->assertNotNull($user); + $this->assertInstanceOf(User::class, $user); + } + + public function testCreateEntity() + { + $user = $this->create(User::class); + + $this->assertNotNull($user); + $this->assertInstanceOf(User::class, $user); + $this->assertEquals('test-username', $user->getUsername()); + $this->assertDatabaseHas(User::class, ['username' => 'test-username']); + } + private function getTestFile() { return new UploadedFile(