1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\D\Entity\Traits; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\AbstractEntityUpdateDto; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
14
|
|
|
use Ramsey\Uuid\UuidInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @large |
18
|
|
|
*/ |
19
|
|
|
class ImplementNotifyChangeTrackingPolicyTest extends AbstractLargeTest |
20
|
|
|
{ |
21
|
|
|
public const WORK_DIR = self::VAR_PATH . |
22
|
|
|
'/' . |
23
|
|
|
self::TEST_TYPE_LARGE . |
24
|
|
|
'/ImplementNotifyChangeTrackingPolicyTest'; |
25
|
|
|
private const ENTITY_FQN = self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_PERSON; |
26
|
|
|
protected static $buildOnce = true; |
27
|
|
|
private $entity; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $entityFqn; |
32
|
|
|
/** |
33
|
|
|
* @var EntitySaverInterface |
34
|
|
|
*/ |
35
|
|
|
private $saver; |
36
|
|
|
/** |
37
|
|
|
* @var TestEntityGenerator |
38
|
|
|
*/ |
39
|
|
|
private $testEntityGenerator; |
40
|
|
|
|
41
|
|
|
public function setup() |
42
|
|
|
{ |
43
|
|
|
parent::setUp(); |
44
|
|
|
if (false === self::$built) { |
45
|
|
|
$this->getTestCodeGenerator() |
46
|
|
|
->copyTo(self::WORK_DIR); |
47
|
|
|
self::$built = true; |
48
|
|
|
} |
49
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
50
|
|
|
$this->entityFqn = $this->getCopiedFqn(self::ENTITY_FQN); |
51
|
|
|
$this->saver = $this->getEntitySaver(); |
52
|
|
|
$this->testEntityGenerator = $this->getTestEntityGeneratorFactory() |
53
|
|
|
->createForEntityFqn($this->entityFqn); |
54
|
|
|
$this->entity = $this->testEntityGenerator->generateEntity(); |
55
|
|
|
$this->testEntityGenerator->addAssociationEntities($this->entity); |
56
|
|
|
$this->saver->save($this->entity); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
* @throws DoctrineStaticMetaException |
62
|
|
|
* @throws \ReflectionException |
63
|
|
|
*/ |
64
|
|
|
public function youCanRemoveItemsFromACollection(): void |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* @var Collection $attributesEmails |
68
|
|
|
*/ |
69
|
|
|
$attributesEmails = $this->entity->getAttributesEmails(); |
70
|
|
|
$attributesEmails->removeElement($attributesEmails->last()); |
71
|
|
|
$dto = $this->getDto($attributesEmails); |
72
|
|
|
$this->entity->update($dto); |
73
|
|
|
$this->saver->save($this->entity); |
74
|
|
|
$this->getEntityManager()->clear(); |
75
|
|
|
$loaded = $this->getRepositoryFactory()->getRepository($this->entityFqn)->find($this->entity->getId()); |
76
|
|
|
$expected = $attributesEmails->count(); |
77
|
|
|
$actual = $loaded->getAttributesEmails()->count(); |
|
|
|
|
78
|
|
|
self::assertSame($expected, $actual); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getDto(Collection $attributesEmails) |
82
|
|
|
{ |
83
|
|
|
return new class($this->entityFqn, $this->entity->getId(), $attributesEmails) extends AbstractEntityUpdateDto |
84
|
|
|
{ |
85
|
|
|
private $attributesEmails; |
86
|
|
|
|
87
|
|
|
public function __construct(string $entityFqn, UuidInterface $id, Collection $attributesEmails) |
88
|
|
|
{ |
89
|
|
|
$this->attributesEmails = $attributesEmails; |
90
|
|
|
parent::__construct($entityFqn, $id); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getAttributesEmails(): Collection |
94
|
|
|
{ |
95
|
|
|
return $this->attributesEmails; |
96
|
|
|
} |
97
|
|
|
}; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
* @throws DoctrineStaticMetaException |
103
|
|
|
* @throws \ReflectionException |
104
|
|
|
*/ |
105
|
|
|
public function youCanAddItemsToACollection(): void |
106
|
|
|
{ |
107
|
|
|
/** |
108
|
|
|
* @var Collection $attributesEmails |
109
|
|
|
*/ |
110
|
|
|
$attributesEmails = $this->entity->getAttributesEmails(); |
111
|
|
|
$attributesEmails->add($this->getNewAttributeEmail()); |
112
|
|
|
$dto = $this->getDto($attributesEmails); |
113
|
|
|
$this->entity->update($dto); |
114
|
|
|
$this->saver->save($this->entity); |
115
|
|
|
$this->getEntityManager()->clear(); |
116
|
|
|
$loaded = $this->getRepositoryFactory()->getRepository($this->entityFqn)->find($this->entity->getId()); |
117
|
|
|
$expected = $attributesEmails->count(); |
118
|
|
|
$actual = $loaded->getAttributesEmails()->count(); |
119
|
|
|
self::assertSame($expected, $actual); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getNewAttributeEmail(): EntityInterface |
123
|
|
|
{ |
124
|
|
|
$attributesEmailsFqn = $this->getCopiedFqn( |
125
|
|
|
self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_EMAIL |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return $this->getTestEntityGeneratorFactory()->createForEntityFqn($attributesEmailsFqn)->generateEntity(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @test |
133
|
|
|
* @throws DoctrineStaticMetaException |
134
|
|
|
* @throws \ReflectionException |
135
|
|
|
*/ |
136
|
|
|
public function youCanUpdateWithAnEmptyCollection(): void |
137
|
|
|
{ |
138
|
|
|
$dto = $this->getDto(new ArrayCollection()); |
139
|
|
|
$this->entity->update($dto); |
140
|
|
|
$this->saver->save($this->entity); |
141
|
|
|
$this->getEntityManager()->clear(); |
142
|
|
|
$loaded = $this->getRepositoryFactory()->getRepository($this->entityFqn)->find($this->entity->getId()); |
143
|
|
|
$expected = 0; |
144
|
|
|
$actual = $loaded->getAttributesEmails()->count(); |
145
|
|
|
self::assertSame($expected, $actual); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.