Skip to content
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
18 changes: 17 additions & 1 deletion src/Testing/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace LaravelDoctrine\ORM\Testing;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadata;
use Faker\Generator as Faker;
use Illuminate\Support\Collection;
use InvalidArgumentException;
Expand Down Expand Up @@ -147,9 +149,23 @@ protected function makeInstance(array $attributes = [])
return $definition;
}

/** @var ClassMetadata $metadata */
$metadata = $this->registry
->getManagerForClass($this->class)
->getClassMetadata($this->class);

$toManyRelations = (new Collection($metadata->getAssociationMappings()))
->keys()
->filter(function ($association) use ($metadata) {
return $metadata->isCollectionValuedAssociation($association);
})
->mapWithKeys(function ($association) {
return [$association => new ArrayCollection];
});

return SimpleHydrator::hydrate(
$this->class,
$this->callClosureAttributes(array_merge($definition, $attributes))
$this->callClosureAttributes(array_merge($toManyRelations->all(), $definition, $attributes))
);
}

Expand Down
169 changes: 169 additions & 0 deletions tests/Testing/FactoryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace {

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup;
use LaravelDoctrine\ORM\Testing\FactoryBuilder;
use Mockery\Adapter\Phpunit\MockeryTestCase;

class FactoryBuilderTest extends MockeryTestCase
{
/**
* @var ManagerRegistry|\Mockery\Mock
*/
private $aRegistry;

/**
* @var string
*/
private $aClass;

/**
* @var string
*/
private $aName;

/**
* @var callable[]|\Mockery\Mock[]
*/
private $definitions;

/**
* @var \Faker\Generator|\Mockery\Mock
*/
private $faker;

/**
* @var EntityManagerInterface|\Mockery\Mock
*/
private $entityManager;

protected function setUp()
{
$this->aRegistry = \Mockery::mock(ManagerRegistry::class);
$this->aClass = EntityStub::class;
$this->aName = 'default';
$this->faker = \Mockery::mock(Faker\Generator::class);
$this->definitions = [
EntityStub::class => [
$this->aName => function () {
return [
'id' => random_int(1, 9),
'name' => 'A Name',
];
}
]
];

$this->aRegistry
->shouldReceive('getManagerForClass')
->with(EntityStub::class)
->andReturn($this->entityManager = \Mockery::mock(EntityManagerInterface::class));

$classMetadata = $this->getEntityManager()->getClassMetadata(EntityStub::class);

$this->entityManager->shouldReceive('getClassMetadata')
->with(EntityStub::class)
->andReturn($classMetadata);

$this->entityManager->shouldReceive('persist');
$this->entityManager->shouldReceive('flush');
}

protected function getFactoryBuilder(array $definitions = []): FactoryBuilder
{
return new FactoryBuilder(
$this->aRegistry,
$this->aClass,
$this->aName,
array_merge($this->definitions, $definitions),
$this->faker
);
}

protected function getEntityManager()
{
$conn = [
'driver' => 'pdo_sqlite',
'database' => ':memory:',
];

$config = Setup::createAnnotationMetadataConfiguration([__DIR__], true);

return EntityManager::create($conn, $config);
}

public function test_it_makes_instances_of_the_class()
{
$instance = $this->getFactoryBuilder()->make();

$this->assertInstanceOf(EntityStub::class, $instance);
}

public function test_it_creates_instances_of_the_class()
{
$instance = $this->getFactoryBuilder()->create();

$this->entityManager->shouldHaveReceived('persist')->with($instance)->once();
$this->entityManager->shouldHaveReceived('flush')->once();
}

public function test_it_fills_to_many_relations_with_array_collections()
{
$instance = $this->getFactoryBuilder()->make();

$this->assertInstanceOf(ArrayCollection::class, $instance->others);
}

public function test_it_shouldnt_override_predefined_relations()
{
$instance = $this->getFactoryBuilder([
EntityStub::class => [
'default' => function () {
return [
'id' => 1,
'name' => 'a name',
'others' => ['Foo'],
];
}
]
])->make();

$this->assertEquals(['Foo'], $instance->others);
}
}

/**
* @Entity
*/
class EntityStub
{
/**
* @Id @GeneratedValue @Column(type="integer")
*/
public $id;

/**
* @Column(type="string")
*/
public $name;

/**
* @ManyToMany(targetEntity="EntityStub")
* @JoinTable(name="stub_stubs",
* joinColumns={@JoinColumn(name="owner_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="owned_id", referencedColumnName="id")}
* )
*/
public $others;
}
}
namespace Faker {
interface Generator
{
}
}