Skip to content

Commit

Permalink
fix: load entity factories
Browse files Browse the repository at this point in the history
  • Loading branch information
sstutz authored and Stefan Stutz committed Dec 2, 2024
1 parent 04915c1 commit ed46d47
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ composer.lock
/tests/Stubs/storage/doctrine.generated.php
.idea
laravel-doctrine-orm.iml
/workbench/bootstrap/cache/*
!/workbench/bootstrap/cache/.gitkeep
/workbench/storage/logs/*
18 changes: 18 additions & 0 deletions src/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -68,6 +71,7 @@ public function register(): void
$this->registerExtensions();
$this->registerConsoleCommands();
$this->registerCustomTypes();
$this->registerEntityFactory();
$this->registerProxyAutoloader();

if (! $this->shouldRegisterDoctrinePresenceValidator()) {
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 43 additions & 0 deletions tests/Feature/DoctrineServiceProviderEntityFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrineTest\ORM\Feature;

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;
use LaravelDoctrineTest\ORM\TestCase;
use Mockery as m;
use Workbench\App\Entities\User;

use function entity;

class DoctrineServiceProviderEntityFactoryTest extends TestCase
{
public function testEntityFactory(): void
{
$classMetaMock = m::mock(ClassMetadata::class);
$classMetaMock->expects('getAssociationMappings')->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')->andReturn($classMetaMock);
$mrMock = m::mock(ManagerRegistry::class);
$mrMock->expects('getManagers')->andReturn([$emMock]);
$mrMock->expects('getManagerForClass')->andReturn($emMock);

$this->app->bind('registry', static fn () => $mrMock);

$user = entity(User::class)->make();

$this->assertInstanceOf(User::class, $user);
}
}
33 changes: 33 additions & 0 deletions workbench/app/Entities/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Workbench\App\Entities;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'users')]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
protected int|null $id = null;

#[ORM\Column(name: 'name')]
public string $name;

#[ORM\Column(name: 'email')]
public string $email;

#[ORM\Column(name: 'password')]
public string $password;

public function __construct(string $name, string $email, string $password)
{
$this->name = $name;
$this->email = $email;
$this->password = $password;
}
}
Empty file.
16 changes: 16 additions & 0 deletions workbench/database/factories/UserEntityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Workbench\Database\Factories;

use Faker\Generator;
use LaravelDoctrine\ORM\Testing\Factory;
use Workbench\App\Entities\User;

/** @var Factory $factory */
$factory->define(User::class, function (Generator $faker, array $attributes = []) {
return [
'name' => $faker->name(),
'email' => $faker->safeEmail,
'password' => 'password',
];
});

0 comments on commit ed46d47

Please sign in to comment.