1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\DtoFactory; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityDataValidatorFactory; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\TestConfigurationException; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\RelationshipHelper; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
use function class_exists; |
20
|
|
|
use function defined; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
24
|
|
|
*/ |
25
|
|
|
class TestEntityGeneratorFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var EntitySaverFactory |
29
|
|
|
*/ |
30
|
|
|
protected $entitySaverFactory; |
31
|
|
|
/** |
32
|
|
|
* @var EntityDataValidatorFactory |
33
|
|
|
*/ |
34
|
|
|
protected $entityValidatorFactory; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $fakerDataProviderClasses; |
39
|
|
|
/** |
40
|
|
|
* @var float|null |
41
|
|
|
*/ |
42
|
|
|
protected $seed; |
43
|
|
|
/** |
44
|
|
|
* @var EntityFactoryInterface|null |
45
|
|
|
*/ |
46
|
|
|
protected $entityFactory; |
47
|
|
|
/** |
48
|
|
|
* @var DtoFactory |
49
|
|
|
*/ |
50
|
|
|
private $dtoFactory; |
51
|
|
|
/** |
52
|
|
|
* @var EntityManagerInterface |
53
|
|
|
*/ |
54
|
|
|
private $entityManager; |
55
|
|
|
/** |
56
|
|
|
* @var NamespaceHelper |
57
|
|
|
*/ |
58
|
|
|
private $namespaceHelper; |
59
|
|
|
/** |
60
|
|
|
* @var FakerDataFillerFactory |
61
|
|
|
*/ |
62
|
|
|
private $fakerDataFillerFactory; |
63
|
|
|
/** |
64
|
|
|
* @var RelationshipHelper |
65
|
|
|
*/ |
66
|
|
|
private $relationshipHelper; |
67
|
|
|
|
68
|
1 |
|
public function __construct( |
69
|
|
|
EntitySaverFactory $entitySaverFactory, |
70
|
|
|
EntityFactoryInterface $entityFactory, |
71
|
|
|
DtoFactory $dtoFactory, |
72
|
|
|
EntityManagerInterface $entityManager, |
73
|
|
|
NamespaceHelper $namespaceHelper, |
74
|
|
|
FakerDataFillerFactory $fakerDataFillerFactory, |
75
|
|
|
RelationshipHelper $relationshipHelper, |
76
|
|
|
array $fakerDataProviderClasses = null, |
77
|
|
|
?float $seed = null |
78
|
|
|
) { |
79
|
1 |
|
$this->entitySaverFactory = $entitySaverFactory; |
80
|
1 |
|
$this->entityFactory = $entityFactory; |
81
|
1 |
|
$this->dtoFactory = $dtoFactory; |
82
|
1 |
|
$this->entityManager = $entityManager; |
83
|
1 |
|
$this->namespaceHelper = $namespaceHelper; |
84
|
1 |
|
$this->fakerDataProviderClasses = $fakerDataProviderClasses; |
85
|
1 |
|
$this->seed = $seed; |
86
|
1 |
|
$this->fakerDataFillerFactory = $fakerDataFillerFactory; |
87
|
1 |
|
$this->relationshipHelper = $relationshipHelper; |
88
|
1 |
|
$this->fakerDataFillerFactory->setSeed($seed); |
89
|
1 |
|
$this->fakerDataFillerFactory->setFakerDataProviders($fakerDataProviderClasses); |
90
|
1 |
|
$this->ensureMetaDataLoaded(); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
1 |
|
private function ensureMetaDataLoaded(): void |
94
|
|
|
{ |
95
|
1 |
|
$this->entityManager->getMetadataFactory()->getAllMetadata(); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
public function createForEntityFqn( |
99
|
|
|
string $entityFqn, |
100
|
|
|
EntityManagerInterface $entityManager = null |
101
|
|
|
): TestEntityGenerator { |
102
|
1 |
|
$this->fakerDataFillerFactory->setFakerDataProviders( |
103
|
1 |
|
$this->fakerDataProviderClasses ?? $this->getFakerDataProvidersFromEntityFqn($entityFqn) |
104
|
|
|
); |
105
|
|
|
|
106
|
1 |
|
return new TestEntityGenerator( |
107
|
1 |
|
$this->getEntityDsm($entityFqn), |
108
|
1 |
|
$this->entityFactory, |
|
|
|
|
109
|
1 |
|
$this->dtoFactory, |
110
|
1 |
|
$this, |
111
|
1 |
|
$this->getFakerDataFillerForEntityFqn($entityFqn), |
112
|
1 |
|
$entityManager ?? $this->entityManager, |
113
|
1 |
|
$this->relationshipHelper |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the list of Faker data providers for the project |
119
|
|
|
* |
120
|
|
|
* By convention this is stored as a constant array on the project level AbstractEntityTest and is generated as |
121
|
|
|
* part of the DSM code generation |
122
|
|
|
* |
123
|
|
|
* This method will throw detailed exceptions if the abstract entity test is not found |
124
|
|
|
* |
125
|
|
|
* @param string $entityFqn |
126
|
|
|
* |
127
|
|
|
* @return array|string[] |
128
|
|
|
* @throws TestConfigurationException |
129
|
|
|
*/ |
130
|
1 |
|
private function getFakerDataProvidersFromEntityFqn(string $entityFqn): array |
131
|
|
|
{ |
132
|
1 |
|
$projectRootNamespace = $this->namespaceHelper->getProjectNamespaceRootFromEntityFqn($entityFqn); |
133
|
1 |
|
$abstractTestFqn = $this->namespaceHelper->tidy( |
134
|
1 |
|
$projectRootNamespace . '\\Entities\\AbstractEntityTest' |
135
|
|
|
); |
136
|
1 |
|
if (!class_exists($abstractTestFqn)) { |
137
|
|
|
throw new TestConfigurationException(<<<TEXT |
138
|
|
|
Failed finding the AbstractEntityTest: $abstractTestFqn |
139
|
|
|
|
140
|
|
|
This could means that your composer configuration is not correct with regards to |
141
|
|
|
including the abstract entity test that has all the definitions for faker data |
142
|
|
|
|
143
|
|
|
You need something that looks like: |
144
|
|
|
|
145
|
|
|
``` |
146
|
|
|
"autoload-dev": { |
147
|
|
|
"psr-4": { |
148
|
|
|
"My\\Project\\": [ |
149
|
|
|
"tests/" |
150
|
|
|
], |
151
|
|
|
"My\\Entities\\Assets\\": [ |
152
|
|
|
"vendor/my/entities/tests/Assets/" |
153
|
|
|
] |
154
|
|
|
}, |
155
|
|
|
"files": [ |
156
|
|
|
"vendor/my/entities/tests/Entities/AbstractEntityTest.php" |
157
|
|
|
] |
158
|
|
|
}, |
159
|
|
|
``` |
160
|
|
|
|
161
|
|
|
TEXT |
162
|
|
|
); |
163
|
|
|
} |
164
|
1 |
|
if (!defined($abstractTestFqn . '::FAKER_DATA_PROVIDERS')) { |
165
|
|
|
throw new TestConfigurationException(<<<TEXT |
166
|
|
|
Your AbstractEntityTest ($abstractTestFqn) does not have the FAKER_DATA_PROVIDERS constant. |
167
|
|
|
|
168
|
|
|
This means that you will not get any custom faker data which is essential to ensure |
169
|
|
|
your generated entities can pass their own validation and be persisted |
170
|
|
|
|
171
|
|
|
TEXT |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
return $abstractTestFqn::FAKER_DATA_PROVIDERS; |
176
|
|
|
} |
177
|
|
|
|
178
|
1 |
|
private function getEntityDsm(string $entityFqn): DoctrineStaticMeta |
179
|
|
|
{ |
180
|
|
|
/** |
181
|
|
|
* @var DoctrineStaticMeta $dsm |
182
|
|
|
*/ |
183
|
1 |
|
$dsm = $entityFqn::getDoctrineStaticMeta(); |
184
|
1 |
|
$metaData = $this->entityManager->getMetadataFactory()->getMetadataFor($entityFqn); |
185
|
1 |
|
if ($metaData instanceof ClassMetadata) { |
186
|
1 |
|
$dsm->setMetaData($metaData); |
187
|
|
|
|
188
|
1 |
|
return $dsm; |
189
|
|
|
} |
190
|
|
|
throw new RuntimeException('$metaData is not an instance of ClassMetadata'); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
private function getFakerDataFillerForEntityFqn(string $entityFqn): FakerDataFillerInterface |
194
|
|
|
{ |
195
|
1 |
|
return $this->fakerDataFillerFactory->getInstanceFromEntityFqn($entityFqn); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param float $seed |
200
|
|
|
* |
201
|
|
|
* @return TestEntityGeneratorFactory |
202
|
|
|
*/ |
203
|
|
|
public function setSeed(float $seed): TestEntityGeneratorFactory |
204
|
|
|
{ |
205
|
|
|
$this->seed = $seed; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $fakerDataProviderClasses |
212
|
|
|
* |
213
|
|
|
* @return TestEntityGeneratorFactory |
214
|
|
|
*/ |
215
|
|
|
public function setFakerDataProviderClasses(array $fakerDataProviderClasses): TestEntityGeneratorFactory |
216
|
|
|
{ |
217
|
|
|
$this->fakerDataProviderClasses = $fakerDataProviderClasses; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|