From db2d3ed424657df21726654fc6351808c2926787 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 --- .gitignore | 4 ++ src/DoctrineServiceProvider.php | 18 +++++++ testbench.yaml | 3 +- ...ctrineServiceProviderEntityFactoryTest.php | 47 +++++++++++++++++++ workbench/app/Entities/User.php | 33 +++++++++++++ workbench/bootstrap/cache/.gitkeep | 0 .../database/factories/UserEntityFactory.php | 24 ++++++++++ workbench/storage/framework/views/.gitkeep | 0 8 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/DoctrineServiceProviderEntityFactoryTest.php create mode 100644 workbench/app/Entities/User.php create mode 100644 workbench/bootstrap/cache/.gitkeep create mode 100644 workbench/database/factories/UserEntityFactory.php create mode 100644 workbench/storage/framework/views/.gitkeep diff --git a/.gitignore b/.gitignore index ba8e64dc..83172d0d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,7 @@ composer.lock /tests/Stubs/storage/doctrine.generated.php .idea laravel-doctrine-orm.iml +/workbench/bootstrap/cache/* +!/workbench/bootstrap/cache/.gitkeep +/workbench/storage/logs/* +/workbench/vendor diff --git a/src/DoctrineServiceProvider.php b/src/DoctrineServiceProvider.php index e048bdfd..ef52a090 100644 --- a/src/DoctrineServiceProvider.php +++ b/src/DoctrineServiceProvider.php @@ -31,12 +31,15 @@ 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; use function class_exists; use function config; use function config_path; +use function database_path; +use function fake; class DoctrineServiceProvider extends ServiceProvider { @@ -68,6 +71,7 @@ public function register(): void $this->registerExtensions(); $this->registerConsoleCommands(); $this->registerCustomTypes(); + $this->registerEntityFactory(); $this->registerProxyAutoloader(); if (! $this->shouldRegisterDoctrinePresenceValidator()) { @@ -270,6 +274,20 @@ public function extendNotificationChannel(): void }); } + /** + * Register the Entity factory instance in the container. + */ + protected function registerEntityFactory(): void + { + $this->app->singleton(EntityFactory::class, static function ($app) { + return EntityFactory::construct( + fake(), + $app->make('registry'), + database_path('factories'), + ); + }); + } + /** * Register proxy autoloader */ diff --git a/testbench.yaml b/testbench.yaml index d5e9cf31..7878203b 100644 --- a/testbench.yaml +++ b/testbench.yaml @@ -1,12 +1,13 @@ providers: - LaravelDoctrine\ORM\DoctrineServiceProvider +laravel: ./workbench workbench: start: '/' install: true health: false discovers: - web: fale + web: false api: false commands: false components: false diff --git a/tests/Feature/DoctrineServiceProviderEntityFactoryTest.php b/tests/Feature/DoctrineServiceProviderEntityFactoryTest.php new file mode 100644 index 00000000..6b3144fc --- /dev/null +++ b/tests/Feature/DoctrineServiceProviderEntityFactoryTest.php @@ -0,0 +1,47 @@ +expects('getAssociationMappings')->twice()->andReturn([]); + + $emMock = m::mock(EntityManagerInterface::class); + $config = new Configuration(); + + $config->setProxyDir('tmp'); + $config->setProxyNamespace(''); + + $config->setAutoGenerateProxyClasses(true); + $emMock->expects('getConfiguration')->twice()->andReturn($config); + $emMock->expects('getClassMetadata')->twice()->andReturn($cmMock); + $mrMock = m::mock(ManagerRegistry::class); + $mrMock->expects('getManagers')->andReturn([$emMock]); + $mrMock->expects('getManagerForClass')->twice()->andReturn($emMock); + + $this->app->bind('registry', static fn () => $mrMock); + + $user = entity(User::class)->make(['password' => 'abc']); + + $this->assertInstanceOf(User::class, $user); + $this->assertEquals('abc', $user->password); + + $user = entity(User::class, 'test')->make(); + $this->assertEquals('test', $user->name); + } +} diff --git a/workbench/app/Entities/User.php b/workbench/app/Entities/User.php new file mode 100644 index 00000000..974f3483 --- /dev/null +++ b/workbench/app/Entities/User.php @@ -0,0 +1,33 @@ +name = $name; + $this->email = $email; + $this->password = $password; + } +} diff --git a/workbench/bootstrap/cache/.gitkeep b/workbench/bootstrap/cache/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/workbench/database/factories/UserEntityFactory.php b/workbench/database/factories/UserEntityFactory.php new file mode 100644 index 00000000..11a26bf2 --- /dev/null +++ b/workbench/database/factories/UserEntityFactory.php @@ -0,0 +1,24 @@ +define(User::class, static function (Generator $faker, array $attributes = []) { + return [ + 'name' => $faker->name(), + 'email' => $faker->safeEmail, + 'password' => 'password', + ]; +}); + +$factory->defineAs(User::class, 'test', static function (Generator $faker, array $attributes = []) { + return [ + 'name' => 'test', + 'email' => 'test@test.tld', + 'password' => 'password', + ]; +}); diff --git a/workbench/storage/framework/views/.gitkeep b/workbench/storage/framework/views/.gitkeep new file mode 100644 index 00000000..e69de29b