From b650efc6df63b03481aa5b27f31bfdb4498ca3e6 Mon Sep 17 00:00:00 2001 From: Stefan Stutz Date: Wed, 27 Nov 2024 16:12:47 +0100 Subject: [PATCH] fix: load entity factories --- composer.json | 2 +- src/DoctrineServiceProvider.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 55b61197..4a6f20d8 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "doctrine/dbal": "^3.0 || ^4.0", "doctrine/orm": "^3.1", "doctrine/persistence": "^3.3", + "fakerphp/faker": "^1.23", "illuminate/auth": "^10.0|^11.0", "illuminate/console": "^10.0|^11.0", "illuminate/container": "^10.0|^11.0", @@ -45,7 +46,6 @@ "phpstan/phpstan": "^1.9", "phpstan/phpstan-deprecation-rules": "^1.1", "phpunit/phpunit": "^11.4", - "fakerphp/faker": "^1.23", "laravel/framework": "^10.0 || ^11.0", "orchestra/testbench": "^9.5" }, diff --git a/src/DoctrineServiceProvider.php b/src/DoctrineServiceProvider.php index e048bdfd..dd96ad37 100644 --- a/src/DoctrineServiceProvider.php +++ b/src/DoctrineServiceProvider.php @@ -9,6 +9,8 @@ use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Proxy\Autoloader; use Doctrine\Persistence\ManagerRegistry; +use Faker\Factory as FakerFactory; +use Faker\Generator as FakerGenerator; use Illuminate\Contracts\Container\Container; use Illuminate\Notifications\ChannelManager; use Illuminate\Support\ServiceProvider; @@ -31,6 +33,7 @@ use LaravelDoctrine\ORM\Exceptions\ExtensionNotFound; use LaravelDoctrine\ORM\Extensions\ExtensionManager; use LaravelDoctrine\ORM\Notifications\DoctrineChannel; +use LaravelDoctrine\ORM\Testing\Factory as EntityFactory; use LaravelDoctrine\ORM\Validation\PresenceVerifierProvider; use function assert; @@ -68,6 +71,7 @@ public function register(): void $this->registerExtensions(); $this->registerConsoleCommands(); $this->registerCustomTypes(); + $this->registerEntityFactory(); $this->registerProxyAutoloader(); if (! $this->shouldRegisterDoctrinePresenceValidator()) { @@ -270,6 +274,24 @@ public function extendNotificationChannel(): void }); } + /** + * Register the Entity factory instance in the container. + */ + protected function registerEntityFactory(): void + { + $this->app->singleton(FakerGenerator::class, static function ($app) { + return FakerFactory::create($app['config']->get('app.faker_locale', 'en_US')); + }); + + $this->app->singleton(EntityFactory::class, static function ($app) { + return EntityFactory::construct( + $app->make(FakerGenerator::class), + $app->make('registry'), + database_path('factories'), + ); + }); + } + /** * Register proxy autoloader */