|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractFunctionalTest; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
8
|
|
|
|
|
9
|
|
|
class EntitySaverFunctionalTest extends AbstractFunctionalTest |
|
10
|
|
|
{ |
|
11
|
|
|
public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'.self::TEST_TYPE.'/EntitySaverFunctionalTest'; |
|
12
|
|
|
|
|
13
|
|
|
private const TEST_ENTITIES = [ |
|
14
|
|
|
self::TEST_PROJECT_ROOT_NAMESPACE.'\\Entities\\TestEntityOne', |
|
15
|
|
|
self::TEST_PROJECT_ROOT_NAMESPACE.'\\Entities\\Deeply\\Nested\\TestEntityTwo', |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
private const TEST_FIELDS = [ |
|
19
|
|
|
self::TEST_PROJECT_ROOT_NAMESPACE.'\\Entity\\Fields\\Traits\\NameFieldTrait', |
|
20
|
|
|
self::TEST_PROJECT_ROOT_NAMESPACE.'\\Entity\\Fields\\Traits\\FooFieldTrait', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
public function setup() |
|
24
|
|
|
{ |
|
25
|
|
|
parent::setup(); |
|
26
|
|
|
$fieldGenerator = $this->getFieldGenerator(); |
|
27
|
|
|
foreach (self::TEST_FIELDS as $fieldFqn) { |
|
28
|
|
|
$fieldGenerator->generateField($fieldFqn, MappingHelper::TYPE_STRING); |
|
29
|
|
|
} |
|
30
|
|
|
$entityGenerator = $this->getEntityGenerator(); |
|
31
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
|
32
|
|
|
$entityGenerator->generateEntity($entityFqn); |
|
33
|
|
|
foreach (self::TEST_FIELDS as $fieldFqn) { |
|
34
|
|
|
$fieldGenerator->setEntityHasField($entityFqn, $fieldFqn); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return EntitySaverInterface |
|
42
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
43
|
|
|
*/ |
|
44
|
|
|
private function getEntitySaver(): EntitySaverInterface |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->container->get(EntitySaver::class); |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function findAllEntity(string $entityFqn) |
|
51
|
|
|
{ |
|
52
|
|
|
$entityManager = $this->getEntityManager(); |
|
53
|
|
|
|
|
54
|
|
|
return $entityManager->getRepository($entityFqn)->findAll(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
public function testItCanSaveAndRemoveASingleEntity() |
|
59
|
|
|
{ |
|
60
|
|
|
$entityFqn = $this->getCopiedFqn(current(self::TEST_ENTITIES)); |
|
61
|
|
|
$entity = new $entityFqn(); |
|
62
|
|
|
$entity->setName('blah'); |
|
63
|
|
|
$entity->setfoo('bar'); |
|
64
|
|
|
$saver = $this->getEntitySaver(); |
|
65
|
|
|
$saver->save($entity); |
|
66
|
|
|
$loaded = $this->findAllEntity($entityFqn)[0]; |
|
67
|
|
|
$this->assertSame($entity->getName(), $loaded->getName()); |
|
68
|
|
|
$this->assertSame($entity->getFoo(), $loaded->getFoo()); |
|
69
|
|
|
$saver->remove($loaded); |
|
70
|
|
|
$this->assertSame([], $this->findAllEntity($entityFqn)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testItCanSaveAndRemoveMultipleEntities() |
|
74
|
|
|
{ |
|
75
|
|
|
$entities = []; |
|
76
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
|
77
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
78
|
|
|
foreach (range(0, 9) as $num) { |
|
79
|
|
|
$entities[$entityFqn.$num] = new $entityFqn(); |
|
80
|
|
|
$entities[$entityFqn.$num]->setName('blah'); |
|
81
|
|
|
$entities[$entityFqn.$num]->setfoo('bar'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
$saver = $this->getEntitySaver(); |
|
85
|
|
|
$saver->saveAll($entities); |
|
86
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
|
87
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
88
|
|
|
$loaded = $this->findAllEntity($entityFqn); |
|
89
|
|
|
$this->assertCount(10, $loaded); |
|
90
|
|
|
foreach (range(0, 9) as $num) { |
|
91
|
|
|
$this->assertSame($entities[$entityFqn.$num]->getName(), $loaded[$num]->getName()); |
|
92
|
|
|
$this->assertSame($entities[$entityFqn.$num]->getFoo(), $loaded[$num]->getFoo()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$saver->removeAll($entities); |
|
97
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
|
98
|
|
|
$entityFqn = $this->getCopiedFqn($entityFqn); |
|
99
|
|
|
$this->assertSame([], $this->findAllEntity($entityFqn)); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|