test(factories): add abstract Factory base class
This commit is contained in:
55
tests/Factories/Factory.php
Normal file
55
tests/Factories/Factory.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Factories;
|
||||
|
||||
use Faker\Factory as FakerFactory;
|
||||
use Faker\Generator;
|
||||
|
||||
/**
|
||||
* Factory - DuckBrain testing
|
||||
*
|
||||
* Clase base para crear fábricas de datos de prueba. Cada subclase
|
||||
* define los atributos por defecto de un modelo y construye su
|
||||
* instancia.
|
||||
*
|
||||
* @author KJ
|
||||
* @website https://kj2.me
|
||||
* @license MIT
|
||||
*/
|
||||
abstract class Factory
|
||||
{
|
||||
protected static ?Generator $fake = null;
|
||||
|
||||
/**
|
||||
* Devuelve una instancia de Faker (singleton por fábrica).
|
||||
*/
|
||||
protected static function faker(): Generator
|
||||
{
|
||||
return static::$fake ??= FakerFactory::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fusiona los atributos por defecto con los indicados.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected static function attributes(array $attributes = []): array
|
||||
{
|
||||
return array_merge(static::definition(), $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Atributos por defecto de la fábrica.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
abstract protected static function definition(): array;
|
||||
|
||||
/**
|
||||
* Crea una instancia del modelo con los atributos dados.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
*/
|
||||
abstract public static function create(array $attributes = []): object;
|
||||
}
|
||||
Reference in New Issue
Block a user