|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Filesystem\File; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\FindReplace; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
//phpcs:disable Generic.Files.LineLength.TooLong |
|
9
|
|
|
/** |
|
10
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\FindReplace |
|
11
|
|
|
*/ |
|
12
|
|
|
class FindReplaceTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/* ************************************************************** |
|
16
|
|
|
* TEST CONTENT STARTS |
|
17
|
|
|
* **************************************************************/ |
|
18
|
|
|
|
|
19
|
|
|
private const TEST_CONTENTS = <<<'PHP' |
|
20
|
|
|
<?php declare(strict_types=1); |
|
21
|
|
|
|
|
22
|
|
|
namespace Test\Code\Generator\Entities; |
|
23
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
|
24
|
|
|
|
|
25
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
26
|
|
|
use Test\Code\Generator\Entity\Fields\Traits\TextFieldTrait; |
|
27
|
|
|
use Test\Code\Generator\Entity\Interfaces\CompanyInterface; |
|
28
|
|
|
use Test\Code\Generator\Entity\Relations\Company\Director\Traits\HasCompanyDirectors\HasCompanyDirectorsOwningManyToMany; |
|
29
|
|
|
use Test\Code\Generator\Entity\Relations\Some\Client\Traits\HasSomeClient\HasSomeClientOwningOneToOne; |
|
30
|
|
|
use Test\Code\Generator\Entity\Repositories\CompanyRepository; |
|
31
|
|
|
|
|
32
|
|
|
// phpcs:enable |
|
33
|
|
|
class Company implements |
|
34
|
|
|
CompanyInterface |
|
35
|
|
|
{ |
|
36
|
|
|
use HasCompanyDirectorsOwningManyToMany; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This is called in UsesPHPMetaDataTrait::loadClassDoctrineMetaData |
|
40
|
|
|
* |
|
41
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
42
|
|
|
* @param ClassMetadataBuilder $builder |
|
43
|
|
|
*/ |
|
44
|
|
|
private static function setCustomRepositoryClass(ClassMetadataBuilder $builder) { |
|
45
|
|
|
$builder->setCustomRepositoryClass(CompanyRepository::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var ArrayCollection|CompanyInterface[] |
|
50
|
|
|
*/ |
|
51
|
|
|
private $companies; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ValidatorClassMetaData $metadata |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
|
57
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
|
58
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function validatorMetaForCompanies( |
|
61
|
|
|
ValidatorClassMetaData $metadata |
|
62
|
|
|
): void { |
|
63
|
|
|
$metadata->addPropertyConstraint( |
|
64
|
|
|
HasCompaniesInterface::PROPERTY_NAME_COMPANIES, |
|
65
|
|
|
new Valid() |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ClassMetadataBuilder $manyToManyBuilder |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract public static function metaForCompanies( |
|
75
|
|
|
ClassMetadataBuilder $manyToManyBuilder |
|
76
|
|
|
): void; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Collection|CompanyInterface[] |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getCompanies(): Collection |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->companies; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Collection|CompanyInterface[] $companies |
|
88
|
|
|
* |
|
89
|
|
|
* @return self |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setCompanies( |
|
92
|
|
|
Collection $companies |
|
93
|
|
|
): HasCompaniesInterface { |
|
94
|
|
|
$this->setEntityCollectionAndNotify( |
|
95
|
|
|
'companies', |
|
96
|
|
|
$companies |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param CompanyInterface|null $company |
|
104
|
|
|
* @param bool $recip |
|
105
|
|
|
* |
|
106
|
|
|
* @return self |
|
107
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
108
|
|
|
*/ |
|
109
|
|
|
public function addCompany( |
|
110
|
|
|
?CompanyInterface $company, |
|
111
|
|
|
bool $recip = true |
|
112
|
|
|
): HasCompaniesInterface { |
|
113
|
|
|
if ($company === null) { |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->addToEntityCollectionAndNotify('companies', $company); |
|
118
|
|
|
if ($this instanceof ReciprocatesCompanyInterface && true === $recip) { |
|
119
|
|
|
$this->reciprocateRelationOnCompany( |
|
120
|
|
|
$company |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param CompanyInterface $company |
|
129
|
|
|
* @param bool $recip |
|
130
|
|
|
* |
|
131
|
|
|
* @return self |
|
132
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
133
|
|
|
*/ |
|
134
|
|
|
public function removeCompany( |
|
135
|
|
|
CompanyInterface $company, |
|
136
|
|
|
bool $recip = true |
|
137
|
|
|
): HasCompaniesInterface { |
|
138
|
|
|
$this->removeFromEntityCollectionAndNotify('companies', $company); |
|
139
|
|
|
if ($this instanceof ReciprocatesCompanyInterface && true === $recip) { |
|
140
|
|
|
$this->removeRelationOnCompany( |
|
141
|
|
|
$company |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Initialise the companies property as a Doctrine ArrayCollection |
|
150
|
|
|
* |
|
151
|
|
|
* @return $this |
|
152
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
153
|
|
|
*/ |
|
154
|
|
|
private function initCompanies() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->companies = new ArrayCollection(); |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
PHP; |
|
162
|
|
|
|
|
163
|
|
|
/* ************************************************************** |
|
164
|
|
|
* TEST CONTENT ENDS |
|
165
|
|
|
* **************************************************************/ |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @test |
|
169
|
|
|
* @small |
|
170
|
|
|
*/ |
|
171
|
|
|
public function itCanDoSimpleStringReplace() |
|
172
|
|
|
{ |
|
173
|
|
|
$file = $this->getFile(); |
|
174
|
|
|
$object = $this->getFindReplace($file); |
|
175
|
|
|
$object->findReplace('use', 'choose'); |
|
176
|
|
|
self::assertNotContains('use', $file->getContents()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private function getFile() |
|
180
|
|
|
{ |
|
181
|
|
|
$file = new File(); |
|
182
|
|
|
$file->setContents(self::TEST_CONTENTS); |
|
183
|
|
|
|
|
184
|
|
|
return $file; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
private function getFindReplace(File $file): FindReplace |
|
188
|
|
|
{ |
|
189
|
|
|
return new FindReplace($file); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @test |
|
194
|
|
|
* @small |
|
195
|
|
|
*/ |
|
196
|
|
|
public function itCanChangeName() |
|
197
|
|
|
{ |
|
198
|
|
|
$file = $this->getFile(); |
|
199
|
|
|
$object = $this->getFindReplace($file); |
|
200
|
|
|
$object->findReplaceName('Company', 'Sheep'); |
|
201
|
|
|
$contents = $file->getContents(); |
|
202
|
|
|
self::assertNotContains('Company', $contents); |
|
203
|
|
|
self::assertNotContains('Companies', $contents); |
|
204
|
|
|
self::assertNotContains('company', $contents); |
|
205
|
|
|
self::assertNotContains('companies', $contents); |
|
206
|
|
|
self::assertContains( |
|
207
|
|
|
'class Sheep implements |
|
208
|
|
|
SheepInterface', |
|
209
|
|
|
$contents |
|
210
|
|
|
); |
|
211
|
|
|
self::assertContains( |
|
212
|
|
|
'use HasSheepDirectorsOwningManyToMany;', |
|
213
|
|
|
$contents |
|
214
|
|
|
); |
|
215
|
|
|
self::assertContains('SheepInterface $sheep', $contents); |
|
216
|
|
|
self::assertContains('private $sheeps', $contents); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @test |
|
221
|
|
|
* @small |
|
222
|
|
|
*/ |
|
223
|
|
|
public function itCanEscapeSlashesForRegexAndDoRegexReplace() |
|
224
|
|
|
{ |
|
225
|
|
|
$file = $this->getFile(); |
|
226
|
|
|
$object = $this->getFindReplace($file); |
|
227
|
|
|
$object->findReplaceRegex( |
|
228
|
|
|
$object->escapeSlashesForRegex('%Doctrine\\.+?\\Mapping\\' . 'Builder\\ClassMetadataBuilder%'), |
|
229
|
|
|
'Foo\\Builder' |
|
230
|
|
|
); |
|
231
|
|
|
$contents = $file->getContents(); |
|
232
|
|
|
self::assertNotContains('Doctrine\\ORM\Mapping\\Builder\\ClassMetadataBuilder', $contents); |
|
233
|
|
|
self::assertContains('use Foo\\Builder', $contents); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|