Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces UseFactory attribute #54065

Open
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Attributes/UseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class UseFactory
{
/**
* Create a new attribute instance.
*
* @param string $factoryClass
christopherarter marked this conversation as resolved.
Show resolved Hide resolved
* @return void
*/
public function __construct(public string $factoryClass) {}
}
23 changes: 23 additions & 0 deletions src/Illuminate/Database/Eloquent/Factories/HasFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Illuminate\Database\Eloquent\Factories;
use Illuminate\Database\Eloquent\Attributes\UseFactory;

/**
* @template TFactory of \Illuminate\Database\Eloquent\Factories\Factory
Expand All @@ -23,6 +24,24 @@ public static function factory($count = null, $state = [])
->state(is_callable($count) || is_array($count) ? $count : $state);
}

/**
* Get the factory from the UseFactory class attribute.
*
* @return TFactory|null
*/
protected static function getUseFactoryAttribute()
{
$attributes = (new \ReflectionClass(static::class))
->getAttributes(UseFactory::class);

if (!empty($attributes)) {
christopherarter marked this conversation as resolved.
Show resolved Hide resolved
$useFactory = $attributes[0]->newInstance();
$factory = new $useFactory->factoryClass;
$factory->guessModelNamesUsing(fn() => static::class);
return $factory;
}
}

/**
* Create a new factory instance for the model.
*
Expand All @@ -34,6 +53,10 @@ protected static function newFactory()
return static::$factory::new();
}

if($useFactory = static::getUseFactoryAttribute()) {
return $useFactory;
}

return null;
christopherarter marked this conversation as resolved.
Show resolved Hide resolved
}
}
31 changes: 31 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\Eloquent\Attributes\CollectedBy;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\ArrayObject;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
Expand All @@ -30,6 +31,8 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Database\Eloquent\MissingAttributeException;
Expand All @@ -51,6 +54,7 @@
use ReflectionClass;
use stdClass;


include_once 'Enums.php';

class DatabaseEloquentModelTest extends TestCase
Expand Down Expand Up @@ -3191,6 +3195,18 @@ public function testCollectedByAttribute()

$this->assertInstanceOf(CustomEloquentCollection::class, $collection);
}

public function testUseFactoryAttribute()
{
$model = new EloquentModelWithUseFactoryAttribute;
$instance = EloquentModelWithUseFactoryAttribute::factory()->make(['name' => 'test name']);
$factory = EloquentModelWithUseFactoryAttribute::factory();
$this->assertInstanceOf(EloquentModelWithUseFactoryAttribute::class, $instance);
$this->assertInstanceOf(EloquentModelWithUseFactoryAttributeFactory::class, $model::factory());
$this->assertInstanceOf(EloquentModelWithUseFactoryAttributeFactory::class, $model::newFactory());
$this->assertEquals(EloquentModelWithUseFactoryAttribute::class, $factory->modelName());
$this->assertEquals('test name', $instance->name); // Small smoke test to ensure the factory is working
}
}

class EloquentTestObserverStub
Expand Down Expand Up @@ -3990,3 +4006,18 @@ class EloquentModelWithCollectedByAttribute extends Model
class CustomEloquentCollection extends Collection
{
}


class EloquentModelWithUseFactoryAttributeFactory extends Factory
{
public function definition()
{
return [];
}
}

#[UseFactory(EloquentModelWithUseFactoryAttributeFactory::class)]
class EloquentModelWithUseFactoryAttribute extends Model
{
use HasFactory;
}
Loading