Skip to content

Commit

Permalink
Merge pull request #647 from sstutz/fix/645
Browse files Browse the repository at this point in the history
fix: load entity factories
  • Loading branch information
TomHAnderson authored Dec 9, 2024
2 parents 04915c1 + db2d3ed commit 149dc8b
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
47 changes: 47 additions & 0 deletions tests/Feature/DoctrineServiceProviderEntityFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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
{
$cmMock = m::mock(ClassMetadata::class);
$cmMock->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);
}
}
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.
24 changes: 24 additions & 0 deletions workbench/database/factories/UserEntityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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, 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',
];
});
Empty file.

0 comments on commit 149dc8b

Please sign in to comment.