Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load entity factories #647

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
sstutz marked this conversation as resolved.
Show resolved Hide resolved
];
});

$factory->defineAs(User::class, 'test', static function (Generator $faker, array $attributes = []) {
return [
'name' => 'test',
'email' => '[email protected]',
'password' => 'password',
];
});
Empty file.
Loading