-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #647 from sstutz/fix/645
fix: load entity factories
- Loading branch information
Showing
8 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/Feature/DoctrineServiceProviderEntityFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => '[email protected]', | ||
'password' => 'password', | ||
]; | ||
}); |
Empty file.