|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractFunctionalTest; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\FullProjectBuildFunctionalTest; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
10
|
|
|
|
|
11
|
|
|
class TestEntityGeneratorFunctionalTest extends AbstractFunctionalTest |
|
12
|
|
|
{ |
|
13
|
|
|
public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'.self::TEST_TYPE.'/TestEntityGeneratorFunctionalTest'; |
|
14
|
|
|
|
|
15
|
|
|
private const TEST_ENTITIES = FullProjectBuildFunctionalTest::TEST_ENTITIES; |
|
16
|
|
|
|
|
17
|
|
|
private const TEST_RELATIONS = FullProjectBuildFunctionalTest::TEST_RELATIONS; |
|
18
|
|
|
|
|
19
|
|
|
private const TEST_FIELD_FQN_BASE = FullProjectBuildFunctionalTest::TEST_FIELD_NAMESPACE_BASE.'\\Traits'; |
|
20
|
|
|
|
|
21
|
|
|
private $built = false; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
protected function buildFullSuiteOfEntities() |
|
25
|
|
|
{ |
|
26
|
|
|
if (false === $this->built) { |
|
27
|
|
|
$entityGenerator = $this->getEntityGenerator(); |
|
28
|
|
|
$fieldGenerator = $this->getFieldGenerator(); |
|
29
|
|
|
$relationsGenerator = $this->getRelationsGenerator(); |
|
30
|
|
|
$fields = []; |
|
31
|
|
|
foreach (MappingHelper::COMMON_TYPES as $type) { |
|
32
|
|
|
$fields[] = $fieldGenerator->generateField( |
|
33
|
|
|
self::TEST_FIELD_FQN_BASE.'\\'.ucwords($type), |
|
34
|
|
|
$type |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
|
38
|
|
|
$entityGenerator->generateEntity($entityFqn); |
|
39
|
|
|
foreach ($fields as $fieldFqn) { |
|
40
|
|
|
$fieldGenerator->setEntityHasField($entityFqn, $fieldFqn); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
foreach (self::TEST_RELATIONS as $relation) { |
|
44
|
|
|
$relationsGenerator->setEntityHasRelationToEntity(...$relation); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function getTestEntityGenerator(string $entityFqn): TestEntityGenerator |
|
51
|
|
|
{ |
|
52
|
|
|
$testedEntityReflectionClass = new \ReflectionClass($entityFqn); |
|
53
|
|
|
|
|
54
|
|
|
return new TestEntityGenerator( |
|
55
|
|
|
AbstractEntityTest::SEED, |
|
56
|
|
|
[], |
|
57
|
|
|
$testedEntityReflectionClass, |
|
58
|
|
|
$this->container->get(EntitySaverFactory::class) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testItCanGenerateASingleEntity(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$entityFqn = current(self::TEST_ENTITIES); |
|
65
|
|
|
$this->getEntityGenerator()->generateEntity($entityFqn); |
|
66
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
|
67
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
68
|
|
|
$testEntityGenerator = $this->getTestEntityGenerator($entityFqn); |
|
69
|
|
|
$entityManager = $this->getEntityManager(); |
|
70
|
|
|
$entity = $testEntityGenerator->generateEntity($entityManager, $entityFqn); |
|
71
|
|
|
$entityManager->persist($entity); |
|
72
|
|
|
$entityManager->flush($entity); |
|
73
|
|
|
$this->assertTrue(true); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testItGeneratesEntitiesAndAssociatedEntities(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->buildFullSuiteOfEntities(); |
|
80
|
|
|
$entities = []; |
|
81
|
|
|
$entityManager = $this->getEntityManager(); |
|
82
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
|
83
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
84
|
|
|
$testEntityGenerator = $this->getTestEntityGenerator($entityFqn); |
|
85
|
|
|
$entity = $testEntityGenerator->generateEntity($entityManager, $entityFqn); |
|
86
|
|
|
$this->assertInstanceOf($entityFqn, $entity); |
|
87
|
|
|
$testEntityGenerator->addAssociationEntities($entityManager, $entity); |
|
88
|
|
|
$entities[] = $entity; |
|
89
|
|
|
} |
|
90
|
|
|
$this->getEntitySaver()->saveAll($entities); |
|
91
|
|
|
$this->assertTrue(true); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testItCanGenerateMultipleEntities(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->buildFullSuiteOfEntities(); |
|
97
|
|
|
$entityFqn = $this->getCopiedFqn(current(self::TEST_ENTITIES)); |
|
98
|
|
|
$actual = $this->getTestEntityGenerator($entityFqn)->generateEntities( |
|
99
|
|
|
$this->getEntityManager(), |
|
100
|
|
|
$entityFqn, |
|
101
|
|
|
100 |
|
102
|
|
|
); |
|
103
|
|
|
$this->assertCount(100, $actual); |
|
104
|
|
|
$this->assertInstanceOf($entityFqn, current($actual)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|