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

Improve support for Factories #201

Merged
merged 11 commits into from
Feb 1, 2025
37 changes: 37 additions & 0 deletions src/Database/Factories/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Winter\Storm\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory;
use Illuminate\Support\Str;

/**
* @template TModel of \Winter\Storm\Database\Model
*
* @method $this trashed()
*/
abstract class Factory extends BaseFactory
{
/**
* Get the factory name for the given model name.
*
* @param class-string<\Winter\Storm\Database\Model> $modelName
* @return class-string<\Winter\Storm\Database\Factories\Factory>
*/
public static function resolveFactoryName(string $modelName)
{
if (Str::contains($modelName, 'Models\\')) {
$pluginNamespace = trim(Str::before($modelName, 'Models'), '\\');
$modelClassName = trim(Str::after($modelName, 'Models'), '\\');
} else {
$pluginNamespace = '';
$modelClassName = $modelName;
}

return trim(implode('\\', [
$pluginNamespace,
trim(static::$namespace, '\\'),
$modelClassName.'Factory'
]), '\\');
}
}
30 changes: 30 additions & 0 deletions src/Database/Factories/HasFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Winter\Storm\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory;

LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
trait HasFactory
mjauvin marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* Get a new factory instance for the model.
*/
public static function factory(callable|array|int|null $count = null, callable|array $state = []): BaseFactory
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
{
$factory = static::newFactory() ?: Factory::factoryForModel(get_called_class());

return $factory
->count(is_numeric($count) ? $count : null)
->state(is_callable($count) || is_array($count) ? $count : $state);
}

/**
* Create a new factory instance for the model.
*
* @return \Winter\Storm\Database\Factories\Factory<static>
*/
protected static function newFactory()
{
//
}
}
18 changes: 18 additions & 0 deletions tests/Database/Factories/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Winter\Storm\Database\Factories\Factory;

class FactoryTest extends TestCase
{
public function testResolveFactoryName()
{
$factoryClass = Factory::resolveFactoryName('Plugin\Author\Models\TestModel');
$this->assertEquals($factoryClass, 'Plugin\Author\Database\Factories\TestModelFactory');
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved

$factoryClass = Factory::resolveFactoryName('Models\TestModel');
$this->assertEquals($factoryClass, 'Database\Factories\TestModelFactory');

$factoryClass = Factory::resolveFactoryName('TestModel');
$this->assertEquals($factoryClass, 'Database\Factories\TestModelFactory');
}
}
Loading