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