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