File tree Expand file tree Collapse file tree 2 files changed +36
-5
lines changed
src/Illuminate/Database/Eloquent/Factories Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -618,11 +618,7 @@ public static function useNamespace(string $namespace)
618
618
*/
619
619
public static function factoryForModel (string $ modelName )
620
620
{
621
- $ resolver = static ::$ factoryNameResolver ?: function ($ modelName ) {
622
- return static ::$ namespace .Str::singular (class_basename ($ modelName )).'Factory ' ;
623
- };
624
-
625
- $ factory = $ resolver ($ modelName );
621
+ $ factory = static ::resolveFactoryName ($ modelName );
626
622
627
623
return $ factory ::new ();
628
624
}
@@ -648,6 +644,25 @@ protected function withFaker()
648
644
return Container::getInstance ()->make (Generator::class);
649
645
}
650
646
647
+ /**
648
+ * Get the factory name for the given model name.
649
+ *
650
+ * @param string $modelName
651
+ * @return string
652
+ */
653
+ public static function resolveFactoryName (string $ modelName )
654
+ {
655
+ $ resolver = static ::$ factoryNameResolver ?: function (string $ modelName ) {
656
+ $ modelName = Str::startsWith ($ modelName , 'App \\Models \\' )
657
+ ? Str::after ($ modelName , 'App \\Models \\' )
658
+ : Str::after ($ modelName , 'App \\' );
659
+
660
+ return static ::$ namespace .$ modelName .'Factory ' ;
661
+ };
662
+
663
+ return $ resolver ($ modelName );
664
+ }
665
+
651
666
/**
652
667
* Proxy dynamic factory methods onto their proper methods.
653
668
*
Original file line number Diff line number Diff line change @@ -259,6 +259,22 @@ public function test_sequences()
259
259
}));
260
260
}
261
261
262
+ public function test_resolve_nested_model_factories ()
263
+ {
264
+ Factory::useNamespace ('Factories \\' );
265
+
266
+ $ resolves = [
267
+ 'App \\Foo ' => 'Factories \\FooFactory ' ,
268
+ 'App \\Models \\Foo ' => 'Factories \\FooFactory ' ,
269
+ 'App \\Models \\Nested \\Foo ' => 'Factories \\Nested \\FooFactory ' ,
270
+ 'App \\Models \\Really \\Nested \\Foo ' => 'Factories \\Really \\Nested \\FooFactory ' ,
271
+ ];
272
+
273
+ foreach ($ resolves as $ model => $ factory ) {
274
+ $ this ->assertEquals ($ factory , Factory::resolveFactoryName ($ model ));
275
+ }
276
+ }
277
+
262
278
public function test_model_has_factory ()
263
279
{
264
280
Factory::guessFactoryNamesUsing (function ($ model ) {
You can’t perform that action at this time.
0 commit comments