-
-
Notifications
You must be signed in to change notification settings - Fork 35
Improve support for Factories #201
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9fc3c47
override Database Factory for Winter Storm
mjauvin 8ca4afc
improve resolveFactoryName; add unit test
mjauvin 53a7dc3
add one more test case
mjauvin 61255f3
use winter storm model and factory
mjauvin e871138
Add test case for modules
LukeTowers ee5d407
Apply suggestions from code review
LukeTowers b685a09
Update Factory.php
LukeTowers e4f6458
Update Factory.php
LukeTowers 14c27b1
Appease the stan
LukeTowers 94982e2
Appease the stan pt 2
LukeTowers dc4d60f
I've had ENOUGH
LukeTowers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?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\\')) { | ||
| $baseNamespace = trim(Str::before($modelName, 'Models'), '\\'); | ||
| $modelClassName = trim(Str::after($modelName, 'Models'), '\\'); | ||
| } else { | ||
| $baseNamespace = ''; | ||
| $modelClassName = $modelName; | ||
| } | ||
|
|
||
| return trim(implode('\\', [ | ||
| $baseNamespace, | ||
| trim(static::$namespace, '\\'), | ||
| $modelClassName.'Factory' | ||
| ]), '\\'); | ||
| } | ||
|
|
||
| /** | ||
| * Get the name of the model that is generated by the factory. | ||
| * | ||
| * @return class-string<\Illuminate\Database\Eloquent\Model|TModel> | ||
| */ | ||
| public function modelName() | ||
| { | ||
| // @phpstan-ignore-next-line | ||
| $resolver = static::$modelNameResolver ?? function (self $factory) { | ||
| $baseNamespace = join("\\", array_slice( | ||
| explode("\\", Str::replaceFirst(static::$namespace, '', get_class($factory))), | ||
| 0, | ||
| -1 | ||
| )); | ||
| $factoryBasename = Str::replaceLast('Factory', '', class_basename($factory)); | ||
|
|
||
| $guessedClass = $baseNamespace . '\\Models\\' . $factoryBasename; | ||
|
|
||
| return class_exists($guessedClass) ? $guessedClass : parent::modelName(); | ||
| }; | ||
|
|
||
| // @phpstan-ignore-next-line | ||
| return $this->model ?? $resolver($this); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace Winter\Storm\Database\Factories; | ||
|
|
||
| trait HasFactory | ||
| { | ||
| /** | ||
| * Get a new factory instance for the model. | ||
| */ | ||
| public static function factory(callable|array|int|null $count = null, callable|array $state = []): Factory | ||
| { | ||
| $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() | ||
| { | ||
| // | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| use Winter\Storm\Database\Factories\Factory; | ||
|
|
||
| class FactoryTest extends TestCase | ||
| { | ||
| public function testResolveFactoryName() | ||
| { | ||
| $factoryClass = Factory::resolveFactoryName('Module\Models\TestModel'); | ||
| $this->assertEquals($factoryClass, 'Module\Database\Factories\TestModelFactory'); | ||
|
|
||
| $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'); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.