1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
8
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
9
|
|
|
use Doctrine\Common\DataFixtures\FixtureInterface; |
10
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema; |
18
|
|
|
use Psr\Container\ContainerInterface; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* To be used in your Test classes. This provides you with the methods to use in your setup method to create the |
23
|
|
|
* fixtures are required |
24
|
|
|
* |
25
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
26
|
|
|
*/ |
27
|
|
|
class FixturesHelper |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Database |
31
|
|
|
*/ |
32
|
|
|
protected $database; |
33
|
|
|
/** |
34
|
|
|
* @var Schema |
35
|
|
|
*/ |
36
|
|
|
protected $schema; |
37
|
|
|
/** |
38
|
|
|
* @var EntityManagerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $entityManager; |
41
|
|
|
/** |
42
|
|
|
* @var FilesystemCache |
43
|
|
|
*/ |
44
|
|
|
protected $cache; |
45
|
|
|
/** |
46
|
|
|
* @var null|string |
47
|
|
|
*/ |
48
|
|
|
protected $cacheKey; |
49
|
|
|
/** |
50
|
|
|
* @var ORMExecutor |
51
|
|
|
*/ |
52
|
|
|
private $fixtureExecutor; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Loader |
56
|
|
|
*/ |
57
|
|
|
private $fixtureLoader; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Did we load cached Fixture SQL? |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
private $loadedFromCache = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Should we load cached Fixture SQL if available? |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
private $loadFromCache = true; |
72
|
|
|
/** |
73
|
|
|
* @var EntitySaverFactory |
74
|
|
|
*/ |
75
|
|
|
private $entitySaverFactory; |
76
|
|
|
/** |
77
|
|
|
* @var NamespaceHelper |
78
|
|
|
*/ |
79
|
|
|
private $namespaceHelper; |
80
|
|
|
/** |
81
|
|
|
* @var TestEntityGeneratorFactory |
82
|
|
|
*/ |
83
|
|
|
private $testEntityGeneratorFactory; |
84
|
|
|
/** |
85
|
|
|
* @var ContainerInterface |
86
|
|
|
*/ |
87
|
|
|
private $container; |
88
|
|
|
|
89
|
4 |
|
public function __construct( |
90
|
|
|
EntityManagerInterface $entityManager, |
91
|
|
|
Database $database, |
92
|
|
|
Schema $schema, |
93
|
|
|
FilesystemCache $cache, |
94
|
|
|
EntitySaverFactory $entitySaverFactory, |
95
|
|
|
NamespaceHelper $namespaceHelper, |
96
|
|
|
TestEntityGeneratorFactory $testEntityGeneratorFactory, |
97
|
|
|
ContainerInterface $container, |
98
|
|
|
?string $cacheKey = null |
99
|
|
|
) { |
100
|
4 |
|
$purger = null; |
101
|
4 |
|
$this->fixtureExecutor = new ORMExecutor($entityManager, $purger); |
102
|
4 |
|
$this->fixtureLoader = new Loader(); |
103
|
4 |
|
$this->database = $database; |
104
|
4 |
|
$this->schema = $schema; |
105
|
4 |
|
$this->entityManager = $entityManager; |
106
|
4 |
|
$this->cache = $cache; |
107
|
4 |
|
$this->entitySaverFactory = $entitySaverFactory; |
108
|
4 |
|
$this->namespaceHelper = $namespaceHelper; |
109
|
4 |
|
$this->testEntityGeneratorFactory = $testEntityGeneratorFactory; |
110
|
4 |
|
$this->cacheKey = $cacheKey; |
111
|
4 |
|
$this->container = $container; |
112
|
|
|
|
113
|
4 |
|
$this->fixtureLoader->setFixturesHelper($this); |
114
|
4 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param null|string $cacheKey |
118
|
|
|
*/ |
119
|
4 |
|
public function setCacheKey(?string $cacheKey): void |
120
|
|
|
{ |
121
|
4 |
|
$this->cacheKey = $cacheKey; |
122
|
4 |
|
} |
123
|
|
|
|
124
|
4 |
|
public function createFixtureInstanceForEntityFqn( |
125
|
|
|
string $entityFqn, |
126
|
|
|
FixtureEntitiesModifierInterface $modifier = null |
127
|
|
|
): AbstractEntityFixtureLoader { |
128
|
4 |
|
$fixtureFqn = $this->namespaceHelper->getFixtureFqnFromEntityFqn($entityFqn); |
129
|
|
|
|
130
|
4 |
|
return $this->createFixture($fixtureFqn, $modifier); |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
public function createFixture( |
134
|
|
|
string $fixtureFqn, |
135
|
|
|
FixtureEntitiesModifierInterface $modifier = null |
136
|
|
|
): AbstractEntityFixtureLoader { |
137
|
4 |
|
return new $fixtureFqn( |
138
|
4 |
|
$this->testEntityGeneratorFactory, |
139
|
4 |
|
$this->entitySaverFactory, |
140
|
4 |
|
$this->namespaceHelper, |
141
|
4 |
|
$this->entityManager, |
142
|
4 |
|
$this->container, |
143
|
4 |
|
$modifier |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Clean the DB and insert fixtures |
149
|
|
|
* |
150
|
|
|
* You can pass in a fixture loader directly which will be appended to the main fixtureLoader, or you can add |
151
|
|
|
* fixtures prior to calling this method |
152
|
|
|
* |
153
|
|
|
* We do not use the purge functionality of the `doctrine/data-fixtures` module, instead we fully drop and create |
154
|
|
|
* the database. |
155
|
|
|
* |
156
|
|
|
* @param FixtureInterface $fixture |
157
|
|
|
* |
158
|
|
|
* @throws DoctrineStaticMetaException |
159
|
|
|
*/ |
160
|
4 |
|
public function createDb(?FixtureInterface $fixture = null): void |
161
|
|
|
{ |
162
|
4 |
|
if (null !== $fixture) { |
163
|
|
|
$this->addFixture($fixture); |
164
|
4 |
|
} elseif ([] === $this->fixtureLoader->getFixtures()) { |
165
|
|
|
throw new RuntimeException( |
166
|
|
|
'No fixtures have been set.' |
167
|
|
|
. 'You need to either pass in a Fixture to this method, or have called `addFixture` at least once ' |
168
|
|
|
. 'before calling this method' |
169
|
|
|
); |
170
|
|
|
} |
171
|
4 |
|
$this->resetDb(); |
172
|
4 |
|
$this->run(); |
173
|
4 |
|
} |
174
|
|
|
|
175
|
4 |
|
public function addFixture(FixtureInterface $fixture): void |
176
|
|
|
{ |
177
|
4 |
|
$this->fixtureLoader->addFixture($fixture); |
178
|
4 |
|
} |
179
|
|
|
|
180
|
4 |
|
public function resetDb(): void |
181
|
|
|
{ |
182
|
4 |
|
$this->database->drop(true)->create(true); |
183
|
4 |
|
$this->schema->create(); |
184
|
4 |
|
} |
185
|
|
|
|
186
|
4 |
|
public function run(): void |
187
|
|
|
{ |
188
|
4 |
|
$cacheKey = $this->getCacheKey(); |
189
|
4 |
|
$connection = $this->entityManager->getConnection(); |
190
|
4 |
|
if ($this->loadFromCache && $this->cache->contains($cacheKey)) { |
191
|
1 |
|
$logger = $this->cache->fetch($cacheKey); |
192
|
1 |
|
$logger->run($this->entityManager->getConnection()); |
193
|
1 |
|
$this->loadedFromCache = true; |
194
|
|
|
|
195
|
1 |
|
return; |
196
|
|
|
} |
197
|
|
|
|
198
|
3 |
|
$logger = $this->getLogger(); |
199
|
3 |
|
$connectionLogger = $connection->getConfiguration()->getSQLLogger(); |
200
|
3 |
|
$configurationLogger = $this->entityManager->getConfiguration()->getSQLLogger(); |
201
|
|
|
|
202
|
3 |
|
$connection->getConfiguration()->setSQLLogger($logger); |
203
|
3 |
|
$this->entityManager->getConfiguration()->setSQLLogger($logger); |
204
|
|
|
|
205
|
3 |
|
$this->fixtureExecutor->execute($this->fixtureLoader->getFixtures(), true); |
206
|
|
|
|
207
|
3 |
|
$this->entityManager->getConfiguration()->setSQLLogger($configurationLogger); |
208
|
3 |
|
$connection->getConfiguration()->setSQLLogger($connectionLogger); |
209
|
|
|
|
210
|
3 |
|
$this->cache->save($cacheKey, $logger); |
211
|
3 |
|
} |
212
|
|
|
|
213
|
4 |
|
private function getCacheKey(): string |
214
|
|
|
{ |
215
|
4 |
|
if (null !== $this->cacheKey) { |
216
|
4 |
|
return $this->cacheKey; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$fixtureFqns = []; |
220
|
|
|
foreach ($this->fixtureLoader->getFixtures() as $fixture) { |
221
|
|
|
$fixtureFqns[] = get_class($fixture); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return md5(print_r($fixtureFqns, true)); |
225
|
|
|
} |
226
|
|
|
|
227
|
3 |
|
private function getLogger(): SQLLogger |
228
|
|
|
{ |
229
|
3 |
|
return new QueryCachingLogger(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function clearFixtures(): void |
233
|
|
|
{ |
234
|
|
|
$this->fixtureLoader = new Loader(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
2 |
|
public function isLoadedFromCache(): bool |
241
|
|
|
{ |
242
|
2 |
|
return $this->loadedFromCache; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param bool $loadFromCache |
247
|
|
|
* |
248
|
|
|
* @return FixturesHelper |
249
|
|
|
*/ |
250
|
1 |
|
public function setLoadFromCache(bool $loadFromCache): FixturesHelper |
251
|
|
|
{ |
252
|
1 |
|
$this->loadFromCache = $loadFromCache; |
253
|
|
|
|
254
|
1 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|