|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\AbstractEntityTest; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
|
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
|
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @large |
|
17
|
|
|
* @coversDefaultClass \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator |
|
18
|
|
|
*/ |
|
19
|
|
|
class TestEntityGeneratorLargeTest extends AbstractLargeTest |
|
20
|
|
|
{ |
|
21
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . |
|
22
|
|
|
self::TEST_TYPE_LARGE . |
|
23
|
|
|
'/TestEntityGeneratorLargeTest'; |
|
24
|
|
|
|
|
25
|
|
|
public const TEST_ENTITY_NAMESPACE_BASE = self::TEST_PROJECT_ROOT_NAMESPACE |
|
26
|
|
|
. '\\' . AbstractGenerator::ENTITIES_FOLDER_NAME; |
|
27
|
|
|
|
|
28
|
|
|
private const TEST_ENTITY = self::TEST_ENTITY_NAMESPACE_BASE . '\\Person'; |
|
29
|
|
|
|
|
30
|
|
|
protected static $buildOnce = true; |
|
31
|
|
|
|
|
32
|
|
|
public function setup(): void |
|
33
|
|
|
{ |
|
34
|
|
|
parent::setUp(); |
|
35
|
|
|
if (false === self::$built) { |
|
36
|
|
|
$this->getTestCodeGenerator() |
|
37
|
|
|
->copyTo(self::WORK_DIR); |
|
38
|
|
|
} |
|
39
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @test |
|
44
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator |
|
45
|
|
|
* @return EntityInterface |
|
46
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
47
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
48
|
|
|
* @throws \ReflectionException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function itCanGenerateASingleEntity(): EntityInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$entityFqn = self::TEST_ENTITY; |
|
53
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
54
|
|
|
$testEntityGenerator = $this->getTestEntityGenerator($entityFqn); |
|
55
|
|
|
$entityManager = $this->getEntityManager(); |
|
56
|
|
|
$entity = $testEntityGenerator->generateEntity($entityManager, $entityFqn); |
|
57
|
|
|
$entityManager->persist($entity); |
|
58
|
|
|
$entityManager->flush(); |
|
59
|
|
|
self::assertTrue(true); |
|
60
|
|
|
|
|
61
|
|
|
return $entity; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getTestEntityGenerator(string $entityFqn): TestEntityGenerator |
|
65
|
|
|
{ |
|
66
|
|
|
$testedEntityReflectionClass = new \ts\Reflection\ReflectionClass($entityFqn); |
|
67
|
|
|
|
|
68
|
|
|
return new \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator( |
|
69
|
|
|
[], |
|
70
|
|
|
$testedEntityReflectionClass, |
|
71
|
|
|
$this->container->get(EntitySaverFactory::class), |
|
72
|
|
|
$this->container->get(EntityValidatorFactory::class), |
|
73
|
|
|
AbstractEntityTest::SEED |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @test |
|
79
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator |
|
80
|
|
|
* |
|
81
|
|
|
* @param EntityInterface $originalEntity |
|
82
|
|
|
* |
|
83
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
84
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
85
|
|
|
* @throws \ReflectionException |
|
86
|
|
|
* @depends itCanGenerateASingleEntity |
|
87
|
|
|
*/ |
|
88
|
|
|
public function itCanGenerateAnOffsetEntity(EntityInterface $originalEntity): void |
|
89
|
|
|
{ |
|
90
|
|
|
$entityFqn = \get_class($originalEntity); |
|
91
|
|
|
$testEntityGenerator = $this->getTestEntityGenerator($entityFqn); |
|
92
|
|
|
$entityManager = $this->getEntityManager(); |
|
93
|
|
|
$newEntity = $testEntityGenerator->generateEntity($entityManager, $entityFqn, 1); |
|
94
|
|
|
self::assertNotEquals($this->dump($newEntity), $this->dump($originalEntity)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @test |
|
99
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator |
|
100
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
101
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
102
|
|
|
* @throws \ErrorException |
|
103
|
|
|
* @throws \ReflectionException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function itGeneratesEntitiesAndAssociatedEntities(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$entities = []; |
|
108
|
|
|
$entityManager = $this->getEntityManager(); |
|
109
|
|
|
$limit = ($this->isQuickTests() ? 2 : null); |
|
110
|
|
|
foreach (TestCodeGenerator::TEST_ENTITIES as $key => $entityFqn) { |
|
111
|
|
|
if ($limit !== null && $key === $limit) { |
|
112
|
|
|
break; |
|
113
|
|
|
} |
|
114
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
115
|
|
|
$testEntityGenerator = $this->getTestEntityGenerator($entityFqn); |
|
116
|
|
|
$entity = $testEntityGenerator->generateEntity($entityManager, $entityFqn); |
|
117
|
|
|
self::assertInstanceOf($entityFqn, $entity); |
|
118
|
|
|
$testEntityGenerator->addAssociationEntities($entityManager, $entity); |
|
119
|
|
|
$entities[] = $entity; |
|
120
|
|
|
} |
|
121
|
|
|
$this->getEntitySaver()->saveAll($entities); |
|
122
|
|
|
self::assertTrue(true); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @test |
|
128
|
|
|
* @covers ::generateEntities |
|
129
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
130
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
131
|
|
|
* @throws \ReflectionException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function itCanGenerateMultipleEntities(): void |
|
134
|
|
|
{ |
|
135
|
|
|
$entityFqn = $this->getCopiedFqn(self::TEST_ENTITY); |
|
136
|
|
|
$count = $this->isQuickTests() ? 2 : 100; |
|
137
|
|
|
$actual = $this->getTestEntityGenerator($entityFqn)->generateEntities( |
|
138
|
|
|
$this->getEntityManager(), |
|
139
|
|
|
$entityFqn, |
|
140
|
|
|
$count |
|
141
|
|
|
); |
|
142
|
|
|
self::assertCount($count, $actual); |
|
143
|
|
|
self::assertInstanceOf($entityFqn, current($actual)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @test |
|
148
|
|
|
* @covers ::create |
|
149
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
150
|
|
|
* @throws \ReflectionException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function itCanCreateAnEmptyEntityUsingTheFactory(): void |
|
153
|
|
|
{ |
|
154
|
|
|
$entityFqn = $this->getCopiedFqn(self::TEST_ENTITY); |
|
155
|
|
|
$entity = $this->getTestEntityGenerator($entityFqn)->create($this->getEntityManager()); |
|
156
|
|
|
self::assertInstanceOf($entityFqn, $entity); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @test |
|
161
|
|
|
* @covers ::create |
|
162
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
163
|
|
|
* @throws \ReflectionException |
|
164
|
|
|
*/ |
|
165
|
|
|
public function itCanCreateAnEntityWithValuesSet(): void |
|
166
|
|
|
{ |
|
167
|
|
|
$entityFqn = $this->getCopiedFqn(self::TEST_ENTITY); |
|
168
|
|
|
$values = [ |
|
169
|
|
|
'string' => 'this has been set', |
|
170
|
|
|
]; |
|
171
|
|
|
$entity = $this->getTestEntityGenerator($entityFqn)->create($this->getEntityManager(), $values); |
|
172
|
|
|
self::assertSame($values['string'], $entity->getString()); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @test |
|
177
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
178
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
179
|
|
|
* @throws \ReflectionException |
|
180
|
|
|
*/ |
|
181
|
|
|
public function itCanYieldUnsavedEntities() |
|
182
|
|
|
{ |
|
183
|
|
|
$entityFqn = $this->getCopiedFqn(self::TEST_ENTITY); |
|
184
|
|
|
$testEntityGenerator = $this->getTestEntityGenerator($entityFqn); |
|
185
|
|
|
$generator = $testEntityGenerator->getGenerator($this->getEntityManager(), $entityFqn); |
|
186
|
|
|
$entity1 = null; |
|
187
|
|
|
foreach ($generator as $entity) { |
|
188
|
|
|
$entity1 = $entity; |
|
189
|
|
|
break; |
|
190
|
|
|
} |
|
191
|
|
|
$generator->next(); |
|
192
|
|
|
$entity3 = $generator->current(); |
|
193
|
|
|
$generator->next(); |
|
194
|
|
|
$entity2 = $generator->current(); |
|
195
|
|
|
|
|
196
|
|
|
self::assertInstanceOf($entityFqn, $entity1); |
|
197
|
|
|
self::assertInstanceOf($entityFqn, $entity2); |
|
198
|
|
|
self::assertInstanceOf($entityFqn, $entity3); |
|
199
|
|
|
self::assertNotSame($entity1, $entity2); |
|
200
|
|
|
self::assertNotSame($entity1, $entity3); |
|
201
|
|
|
self::assertNotSame($entity2, $entity3); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.