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\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\TestEntityGenerator; |
11
|
|
|
|
12
|
|
|
abstract class AbstractEntityFixtureLoader extends AbstractFixture |
13
|
|
|
{ |
14
|
|
|
public const BULK_AMOUNT_TO_GENERATE = 100; |
15
|
|
|
/** |
16
|
|
|
* @var TestEntityGenerator |
17
|
|
|
*/ |
18
|
|
|
protected $testEntityGenerator; |
19
|
|
|
/** |
20
|
|
|
* @var EntitySaverFactory |
21
|
|
|
*/ |
22
|
|
|
protected $saverFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var null|FixtureEntitiesModifierInterface |
26
|
|
|
*/ |
27
|
|
|
protected $modifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $entityFqn; |
33
|
|
|
|
34
|
3 |
|
public function __construct( |
35
|
|
|
TestEntityGenerator $testEntityGenerator, |
36
|
|
|
EntitySaverFactory $saverFactory, |
37
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
38
|
|
|
) { |
39
|
3 |
|
$this->testEntityGenerator = $testEntityGenerator; |
40
|
3 |
|
$this->saverFactory = $saverFactory; |
41
|
3 |
|
$this->entityFqn = $this->getEntityFqn(); |
42
|
3 |
|
if (null !== $modifier) { |
43
|
1 |
|
$this->setModifier($modifier); |
44
|
|
|
} |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Use this method to inject your own modifier that will receive the array of generated entities and can then |
49
|
|
|
* update them as you see fit |
50
|
|
|
* |
51
|
|
|
* @param FixtureEntitiesModifierInterface $modifier |
52
|
|
|
*/ |
53
|
1 |
|
public function setModifier(FixtureEntitiesModifierInterface $modifier): void |
54
|
|
|
{ |
55
|
1 |
|
$this->modifier = $modifier; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Load data fixtures with the passed EntityManager |
61
|
|
|
* |
62
|
|
|
* @param ObjectManager $manager |
63
|
|
|
* |
64
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
65
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
66
|
|
|
* @throws \ErrorException |
67
|
|
|
* @throws \ReflectionException |
68
|
|
|
*/ |
69
|
2 |
|
public function load(ObjectManager $manager) |
70
|
|
|
{ |
71
|
2 |
|
if (!$manager instanceof EntityManagerInterface) { |
72
|
|
|
throw new \RuntimeException( |
73
|
|
|
'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager) |
74
|
|
|
); |
75
|
|
|
} |
76
|
2 |
|
$entities = $this->loadBulk($manager); |
77
|
2 |
|
$this->updateGenerated($entities); |
78
|
2 |
|
$this->saverFactory->getSaverForEntityFqn($this->entityFqn)->saveAll($entities); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
protected function updateGenerated(array &$entities) |
82
|
|
|
{ |
83
|
2 |
|
if (null === $this->modifier) { |
84
|
1 |
|
return; |
85
|
|
|
} |
86
|
1 |
|
$this->modifier->modifyEntities($entities); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param EntityManagerInterface $entityManager |
91
|
|
|
* |
92
|
|
|
* @return array|EntityInterface[] |
93
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
94
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
95
|
|
|
* @throws \ErrorException |
96
|
|
|
* @throws \ReflectionException |
97
|
|
|
*/ |
98
|
2 |
|
protected function loadBulk(EntityManagerInterface $entityManager): array |
99
|
|
|
{ |
100
|
2 |
|
$entities = $this->testEntityGenerator->generateEntities( |
101
|
2 |
|
$entityManager, |
102
|
2 |
|
$this->entityFqn, |
103
|
2 |
|
static::BULK_AMOUNT_TO_GENERATE |
104
|
|
|
); |
105
|
2 |
|
foreach ($entities as $generated) { |
106
|
2 |
|
$this->testEntityGenerator->addAssociationEntities($entityManager, $generated); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return $entities; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the fully qualified name of the Entity we are testing, |
114
|
|
|
* assumes EntityNameTest as the entity class short name |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
3 |
|
protected function getEntityFqn(): string |
119
|
|
|
{ |
120
|
3 |
|
if (null === $this->entityFqn) { |
121
|
3 |
|
$this->entityFqn = \str_replace( |
122
|
3 |
|
'\\Assets\\EntityFixtures\\', |
123
|
3 |
|
'\\Entities\\', |
124
|
3 |
|
\substr(static::class, 0, -7) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
return $this->entityFqn; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|