I was able to manage to work on my own entity factory using eloquent models, and hopefully someday it will also support doctrine. Here is the code.
<?php
declare(strict_types=1);
namespace Tests\Tools\EntityFactories\Interfaces;
use Illuminate\Database\Eloquent\Model;
interface EntityFactoryGeneratorInterface
{
/**
* Persist a model object.
*
* @param string $entityClass
* @param null|mixed[] $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function persistEntity(string $entityClass, ?array $data = null): Model;
/**
* Save model object to database.
*
* @return void
*/
public function resolve(): void;
}
<?php
declare(strict_types=1);
namespace Tests\Tools\EntityFactories\Interfaces;
use Illuminate\Database\Eloquent\Model;
interface EntityFactoryInterface
{
/**
* Create entity model.
*
* @param mixed[] $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $data): Model;
/**
* Get default data for the model.
*
* @return mixed[]
*/
public function getDefaultData(): array;
}
Example Entity
<?php
declare(strict_types=1);
namespace Tests\Tools\EntityFactories;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
class UserEntityGenerator extends AbstractEntityGenerator
{
public function create(array $data): Model
{
return new User($data);
}
public function getDefaultData(): array
{
return [
'first_name' => 'Albert'
];
}
}
Usage
// We use database transaction so after each tests, data will be deleted from the database.
$this->entityFactory = new EntityGeneratorFactory($this->app->make(DatabaseTransactionInterface::class));
// We persist the data in the memory first. Useful for unit tests without saving it in the database.
$this->entityFactory->persistEntity(User::class, ['first_name' => 'David']);
// Then we decide to save it in the database, useful for functional testing.
$this->entityFactory->resolve();