Skip to content

Commit

Permalink
Add CreatesObjects concern to create objects using factory muffing (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
gricob authored Feb 8, 2020
1 parent 90eebd5 commit efc9cda
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -41,5 +43,8 @@
"psr-4": {
"Tests\\": "tests/"
}
},
"conflict": {
"doctrine/lexer": ">1.0.2"
}
}
53 changes: 53 additions & 0 deletions src/Concerns/CreatesObjects.php
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;
}
9 changes: 9 additions & 0 deletions tests/App/factories/UserFactory.php
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'
]);
22 changes: 21 additions & 1 deletion tests/FunctionalTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,8 @@

class FunctionalTestCaseTest extends FunctionalTestCase
{
use RefreshDatabase;
use RefreshDatabase,
CreatesObjects;

protected static function getKernelClass()
{
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit efc9cda

Please sign in to comment.