1. Go to this page and download the library: Download ergebnis/factory-bot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ergebnis / factory-bot example snippets
declare(strict_types=1);
use Doctrine\ORM;
use Ergebnis\FactoryBot;
use Faker\Factory;
$entityManager = ORM\EntityManager::create(...);
$faker = Factory::create(...);
$fixtureFactory = new FactoryBot\FixtureFactory(
$entityManager,
$faker
);
declare(strict_types=1);
namespace App\Test\Functional;
use Doctrine\ORM;
use Ergebnis\FactoryBot;
use Faker\Generator;
use PHPUnit\Framework;
abstract class AbstractTestCase extends Framework\TestCase
{
final protected static function entityManager(): ORM\EntityManagerInterface
{
// create entity manager from configuration or fetch it from container
return $entityManager;
}
final protected static function faker(): Generator
{
$faker = Factory::create();
$faker->seed(9001);
return $faker;
}
final protected static function fixtureFactory(): FactoryBot\FixtureFactory
{
$fixtureFactory = new FactoryBot\FixtureFactory(
static::entityManager(),
static::faker()
);
// create or load entity definitions
return $fixtureFactory;
}
}
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class);
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'avatar' => FactoryBot\FieldDefinition::reference(Entity\Avatar::class),
'id' => FactoryBot\FieldDefinition::closure(static function (Generator $faker): string {
return $faker->uuid;
}),
'location' => FactoryBot\FieldDefinition::optionalClosure(static function (Generator $faker): string {
return $faker->city;
}),
'login' => FactoryBot\FieldDefinition::closure(static function (Generator $faker): string {
return $faker->userName;
}),
]);
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(
Entity\User::class,
[
'avatar' => FactoryBot\FieldDefinition::reference(Entity\Avatar::class),
'id' => FactoryBot\FieldDefinition::closure(static function (Generator $faker): string {
return $faker->uuid;
}),
'location' => FactoryBot\FieldDefinition::optionalClosure(static function (Generator $faker): string {
return $faker->city;
}),
'login' => FactoryBot\FieldDefinition::closure(static function (Generator $faker): string {
return $faker->userName;
}),
],
static function (Entity\User $user, array $fieldValues): void {
if (is_string($fieldValues['location')) {
// ...
}
}
);
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Faker\Generator;
$closure = static function (Generator $faker, FactoryBot\FixtureFactory $fixtureFactory) {
// return whatever makes sense
};
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'id' => FactoryBot\FieldDefinition::closure(static function (Generator $faker): string {
return $faker->uuid;
}),
'organizations' => FactoryBot\FieldDefinition::closure(static function (Generator $faker, FactoryBot\FixtureFactory $fixtureFactory): array {
return $fixtureFactory->createMany(
Entity\Organization::class,
FactoryBot\Count::exact($faker->numberBetween(
1,
5
))
);
}),
]);
/** @var Entity\User $user */
$user = $fixtureFactory->createOne(Entity\User::class);
var_dump($user->id()); // string
var_dump($user->organizations()); // array with 1-5 instances of Entity\Organization
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'id' => static function (Generator $faker): string {
return $faker->uuid;
},
'organizations' => static function (Generator $faker, FactoryBot\FixtureFactory $fixtureFactory): array {
return $fixtureFactory->createMany(
Entity\Organization::class,
FactoryBot\Count::exact($faker->numberBetween(
1,
5
))
);
},
]);
/** @var Entity\User $user */
$user = $fixtureFactory->createOne(Entity\User::class);
var_dump($user->id()); // string
var_dump($user->organizations()); // array with 1-5 instances of Entity\Organization
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Faker\Generator;
$closure = static function (Generator $faker, FactoryBot\FixtureFactory $fixtureFactory) {
// return whatever makes sense
};
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'location' => FactoryBot\FieldDefinition::optionalClosure(static function (Generator $faker): string {
return $faker->city;
}),
]);
/** @var Entity\User $user */
$user = $fixtureFactory->createOne(Entity\User::class);
var_dump($user->location()); // null or a random city
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'location' => FactoryBot\FieldDefinition::optionalClosure(static function (Generator $faker): string {
return $faker->city;
}),
]);
$withOptionalFixtureFactory = $fixtureFactory->withOptional();
/** @var Entity\User $user */
$user = $withOptionalFixtureFactory->createOne(Entity\User::class);
var_dump($user->location()); // a random city
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
use Faker\Generator;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'location' => FactoryBot\FieldDefinition::optionalClosure(static function (Generator $faker): string {
return $faker->city;
}),
]);
$withoutOptionalFixtureFactory = $fixtureFactory->withoutOptional();
/** @var Entity\User $user */
$user = $withoutOptionalFixtureFactory->createOne(Entity\User::class);
var_dump($user->location()); // null
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'avatar' => FactoryBot\FieldDefinition::reference(Entity\Avatar::class),
]);
/** @var Entity\User $user */
$user = $fixtureFactory->createOne(Entity\User::class);
var_dump($user->avatar()); // an instance of Entity\Avatar
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\User::class, [
'avatar' => FactoryBot\FieldDefinition::reference(
Entity\Avatar::class,
[
'height' => 300,
'width' => 200,
]
),
]);
/** @var Entity\User $user */
$user = $fixtureFactory->createOne(Entity\User::class);
var_dump($user->avatar()); // an instance of Entity\Avatar with height set to 300 and width set to 200
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\Repository::class, [
'template' => FactoryBot\FieldDefinition::optionalReference(Entity\Repository::class),
]);
/** @var Entity\Repository $repository */
$repository = $fixtureFactory->createOne(Entity\Repository::class);
var_dump($repository->template()); // null or an instance of Entity\Repository
declare(strict_types=1);
use Ergebnis\FactoryBot;
use Example\Entity;
/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->define(Entity\Repository::class, [
'template' => FactoryBot\FieldDefinition::optionalReference(Entity\Repository::class),
]);
$withOptionalFixtureFactory = $fixtureFactory->withOptional();
/** @var Entity\Repository $repository */
$repository = $withOptionalFixtureFactory->createOne(Entity\Repository::class);
var_dump($repository->template()); // an instance of Entity\Repository
declare(strict_types=1);
namespace Example\Test\Fixture\Entity;
use Ergebnis\FactoryBot;
use Example\Entity;
final class UserDefinitionProvider implements FactoryBot\EntityDefinitionProvider
{
public function accept(FactoryBot\FixtureFactory $fixtureFactory): void
{
$fixtureFactory->define(Entity\User::class, [
// ...
]);
}
}
declare(strict_types=1);
namespace App\Test\Functional;
use Ergebnis\FactoryBot;
use PHPUnit\Framework;
abstract class AbstractTestCase extends Framework\TestCase
{
// ...
final protected static function fixtureFactory(): FactoryBot\FixtureFactory
{
$fixtureFactory = new FactoryBot\FixtureFactory(
static::entityManager(),
static::faker()
);
$fixtureFactory->load(__DIR__ . '/../Fixture');
return $fixtureFactory;
}
// ...
}
declare(strict_types=1);
namespace App\Test\Functional;
use Ergebnis\FactoryBot;
use Example\Test\Fixture;
use PHPUnit\Framework;
abstract class AbstractTestCase extends Framework\TestCase
{
// ...
final protected static function fixtureFactory(): FactoryBot\FixtureFactory
{
$fixtureFactory = new FactoryBot\FixtureFactory(
static::entityManager(),
static::faker()
);
$fixtureFactory->register(new Fixture\UserDefinitionProvider());
return $fixtureFactory;
}
// ...
}