|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
6
|
|
|
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverInterface; |
|
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator; |
|
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractEntityFixtureLoader extends AbstractFixture implements OrderedFixtureInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public const ORDER_FIRST = 1000; |
|
21
|
|
|
|
|
22
|
|
|
public const ORDER_DEFAULT = 2000; |
|
23
|
|
|
|
|
24
|
|
|
public const ORDER_LAST = 3000; |
|
25
|
|
|
|
|
26
|
|
|
public const BULK_AMOUNT_TO_GENERATE = 100; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var TestEntityGenerator |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $testEntityGenerator; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var EntitySaverInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $saver; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var null|FixtureEntitiesModifierInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $modifier; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $entityFqn; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var int |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $order = self::ORDER_DEFAULT; |
|
50
|
|
|
|
|
51
|
4 |
|
public function __construct( |
|
52
|
|
|
TestEntityGeneratorFactory $testEntityGeneratorFactory, |
|
53
|
|
|
EntitySaverFactory $saverFactory, |
|
54
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
|
55
|
|
|
) { |
|
56
|
4 |
|
$this->entityFqn = $this->getEntityFqn(); |
|
57
|
4 |
|
$this->saver = $saverFactory->getSaverForEntityFqn($this->entityFqn); |
|
58
|
4 |
|
$this->testEntityGenerator = $testEntityGeneratorFactory->createForEntityFqn($this->entityFqn); |
|
59
|
4 |
|
if (null !== $modifier) { |
|
60
|
2 |
|
$this->setModifier($modifier); |
|
61
|
|
|
} |
|
62
|
4 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return int |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getOrder(): int |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->order; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $order |
|
74
|
|
|
* |
|
75
|
|
|
* @return AbstractEntityFixtureLoader |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function setOrder(int $order): self |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->order = $order; |
|
80
|
|
|
|
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Use this method to inject your own modifier that will receive the array of generated entities and can then |
|
86
|
|
|
* update them as you see fit |
|
87
|
|
|
* |
|
88
|
|
|
* @param FixtureEntitiesModifierInterface $modifier |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function setModifier(FixtureEntitiesModifierInterface $modifier): void |
|
91
|
|
|
{ |
|
92
|
2 |
|
$this->modifier = $modifier; |
|
93
|
2 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Load data fixtures with the passed EntityManager |
|
98
|
|
|
* |
|
99
|
|
|
* @param ObjectManager $manager |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
102
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
103
|
|
|
* @throws \ErrorException |
|
104
|
|
|
* @throws \ReflectionException |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function load(ObjectManager $manager) |
|
107
|
|
|
{ |
|
108
|
2 |
|
if (!$manager instanceof EntityManagerInterface) { |
|
109
|
|
|
throw new \RuntimeException( |
|
110
|
|
|
'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
2 |
|
$entities = $this->loadBulk($manager); |
|
114
|
2 |
|
$this->updateGenerated($entities); |
|
115
|
2 |
|
$this->saver->saveAll($entities); |
|
116
|
2 |
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
protected function updateGenerated(array &$entities) |
|
119
|
|
|
{ |
|
120
|
2 |
|
if (null === $this->modifier) { |
|
121
|
1 |
|
return; |
|
122
|
|
|
} |
|
123
|
1 |
|
$this->modifier->modifyEntities($entities); |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param EntityManagerInterface $entityManager |
|
128
|
|
|
* |
|
129
|
|
|
* @return array|EntityInterface[] |
|
130
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
131
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
132
|
|
|
* @throws \ErrorException |
|
133
|
|
|
* @throws \ReflectionException |
|
134
|
|
|
*/ |
|
135
|
2 |
|
protected function loadBulk(EntityManagerInterface $entityManager): array |
|
136
|
|
|
{ |
|
137
|
2 |
|
$entities = $this->testEntityGenerator->generateEntities( |
|
138
|
2 |
|
$entityManager, |
|
139
|
2 |
|
$this->entityFqn, |
|
140
|
2 |
|
static::BULK_AMOUNT_TO_GENERATE |
|
141
|
|
|
); |
|
142
|
2 |
|
foreach ($entities as $generated) { |
|
143
|
2 |
|
$this->testEntityGenerator->addAssociationEntities($entityManager, $generated); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
return $entities; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the fully qualified name of the Entity we are testing, |
|
151
|
|
|
* assumes EntityNameTest as the entity class short name |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
4 |
|
protected function getEntityFqn(): string |
|
156
|
|
|
{ |
|
157
|
4 |
|
if (null === $this->entityFqn) { |
|
158
|
4 |
|
$this->entityFqn = \str_replace( |
|
159
|
4 |
|
'\\Assets\\EntityFixtures\\', |
|
160
|
4 |
|
'\\Entities\\', |
|
161
|
4 |
|
\substr(static::class, 0, -7) |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
4 |
|
return $this->entityFqn; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|