|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Creation\Src\Entity\Fields\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Traits\FieldTraitCreator; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
|
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Small\ConfigTest; |
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @small |
|
19
|
|
|
*/ |
|
20
|
|
|
class FieldTraitCreatorTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
private const FIELD_TRAIT = <<<'PHP' |
|
23
|
|
|
<?php declare(strict_types=1); |
|
24
|
|
|
|
|
25
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits; |
|
26
|
|
|
|
|
27
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
28
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
29
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
30
|
|
|
use Symfony\Component\Validator\Exception\InvalidOptionsException; |
|
31
|
|
|
use Symfony\Component\Validator\Exception\MissingOptionsException; |
|
32
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
33
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\TestStringFieldInterface; |
|
34
|
|
|
use \EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
|
35
|
|
|
use \Symfony\Component\Validator\Constraints\Length; |
|
36
|
|
|
|
|
37
|
|
|
trait TestStringFieldTrait |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string|null |
|
41
|
|
|
*/ |
|
42
|
|
|
private $testString; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ClassMetadataBuilder $builder |
|
46
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function metaForTestString(ClassMetadataBuilder $builder): void |
|
49
|
|
|
{ |
|
50
|
|
|
MappingHelper::setSimpleStringFields( |
|
51
|
|
|
[TestStringFieldInterface::PROP_TEST_STRING], |
|
52
|
|
|
$builder, |
|
53
|
|
|
TestStringFieldInterface::DEFAULT_TEST_STRING, |
|
54
|
|
|
true |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* This method sets the validation for this field. |
|
60
|
|
|
* You should add in as many relevant property constraints as you see fit. |
|
61
|
|
|
* |
|
62
|
|
|
* Remove the PHPMD suppressed warning once you start setting constraints |
|
63
|
|
|
* |
|
64
|
|
|
* @param ValidatorClassMetaData $metadata |
|
65
|
|
|
* |
|
66
|
|
|
* @throws MissingOptionsException |
|
67
|
|
|
* @throws InvalidOptionsException |
|
68
|
|
|
* @throws ConstraintDefinitionException |
|
69
|
|
|
* |
|
70
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
71
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
|
72
|
|
|
* |
|
73
|
|
|
*/ |
|
74
|
|
|
protected static function validatorMetaForPropertyTestString(ValidatorClassMetaData $metadata): void |
|
75
|
|
|
{ |
|
76
|
|
|
$metadata->addPropertyConstraint( |
|
77
|
|
|
TestStringFieldInterface::PROP_TEST_STRING, |
|
78
|
|
|
new Length(['min' => 0, 'max' => Database::MAX_VARCHAR_LENGTH]) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string|null |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getTestString(): ?string |
|
86
|
|
|
{ |
|
87
|
|
|
if (null === $this->testString) { |
|
88
|
|
|
return TestStringFieldInterface::DEFAULT_TEST_STRING; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->testString; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function initTestString(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->testString = TestStringFieldInterface::DEFAULT_TEST_STRING; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string|null $testString |
|
101
|
|
|
* |
|
102
|
|
|
* @return self |
|
103
|
|
|
*/ |
|
104
|
|
|
private function setTestString(?string $testString): self |
|
105
|
|
|
{ |
|
106
|
|
|
$this->updatePropertyValue( |
|
107
|
|
|
TestStringFieldInterface::PROP_TEST_STRING, |
|
108
|
|
|
$testString |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
PHP; |
|
115
|
|
|
|
|
116
|
|
|
private const DATETIME_FIELD_TRAIT = <<<'PHP' |
|
117
|
|
|
<?php declare(strict_types=1); |
|
118
|
|
|
|
|
119
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits; |
|
120
|
|
|
|
|
121
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
122
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
123
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
124
|
|
|
use Symfony\Component\Validator\Exception\InvalidOptionsException; |
|
125
|
|
|
use Symfony\Component\Validator\Exception\MissingOptionsException; |
|
126
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
127
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\TestDateTimeFieldInterface; |
|
128
|
|
|
|
|
129
|
|
|
trait TestDateTimeFieldTrait |
|
130
|
|
|
{ |
|
131
|
|
|
/** |
|
132
|
|
|
* @var \DateTimeImmutable|null |
|
133
|
|
|
*/ |
|
134
|
|
|
private $testDateTime; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param ClassMetadataBuilder $builder |
|
138
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function metaForTestDateTime(ClassMetadataBuilder $builder): void |
|
141
|
|
|
{ |
|
142
|
|
|
MappingHelper::setSimpleDatetimeFields( |
|
143
|
|
|
[TestDateTimeFieldInterface::PROP_TEST_DATE_TIME], |
|
144
|
|
|
$builder, |
|
145
|
|
|
TestDateTimeFieldInterface::DEFAULT_TEST_DATE_TIME |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* This method sets the validation for this field. |
|
151
|
|
|
* You should add in as many relevant property constraints as you see fit. |
|
152
|
|
|
* |
|
153
|
|
|
* Remove the PHPMD suppressed warning once you start setting constraints |
|
154
|
|
|
* |
|
155
|
|
|
* @param ValidatorClassMetaData $metadata |
|
156
|
|
|
* |
|
157
|
|
|
* @throws MissingOptionsException |
|
158
|
|
|
* @throws InvalidOptionsException |
|
159
|
|
|
* @throws ConstraintDefinitionException |
|
160
|
|
|
* |
|
161
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
162
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
|
163
|
|
|
* |
|
164
|
|
|
*/ |
|
165
|
|
|
protected static function validatorMetaForPropertyTestDateTime(ValidatorClassMetaData $metadata): void |
|
166
|
|
|
{ |
|
167
|
|
|
// $metadata->addPropertyConstraint( |
|
168
|
|
|
// TestDateTimeFieldInterface::PROP_TEST_DATE_TIME, |
|
169
|
|
|
// new NotBlank() |
|
170
|
|
|
// ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return \DateTimeImmutable|null |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getTestDateTime(): ?\DateTimeImmutable |
|
177
|
|
|
{ |
|
178
|
|
|
if (null === $this->testDateTime) { |
|
179
|
|
|
return TestDateTimeFieldInterface::DEFAULT_TEST_DATE_TIME; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this->testDateTime; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
private function initTestDateTime(): void |
|
186
|
|
|
{ |
|
187
|
|
|
$this->testDateTime = TestDateTimeFieldInterface::DEFAULT_TEST_DATE_TIME; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param \DateTimeImmutable|null $testDateTime |
|
192
|
|
|
* |
|
193
|
|
|
* @return self |
|
194
|
|
|
*/ |
|
195
|
|
|
private function setTestDateTime(?\DateTimeImmutable $testDateTime): self |
|
196
|
|
|
{ |
|
197
|
|
|
$this->updatePropertyValue( |
|
198
|
|
|
TestDateTimeFieldInterface::PROP_TEST_DATE_TIME, |
|
199
|
|
|
$testDateTime |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
PHP; |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @test |
|
209
|
|
|
*/ |
|
210
|
|
|
public function itGeneratesTheCorrectContent(): void |
|
211
|
|
|
{ |
|
212
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\TestStringFieldTrait'; |
|
213
|
|
|
$expected = self::FIELD_TRAIT; |
|
214
|
|
|
$actual = $this->getCreator() |
|
215
|
|
|
->setMappingHelperCommonType(MappingHelper::TYPE_STRING) |
|
216
|
|
|
->setUnique(true) |
|
217
|
|
|
->setNewObjectFqn($newObjectFqn) |
|
218
|
|
|
->createTargetFileObject() |
|
219
|
|
|
->getTargetFile() |
|
220
|
|
|
->getContents(); |
|
221
|
|
|
self::assertSame(trim($expected), trim($actual)); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
private function getCreator(): FieldTraitCreator |
|
225
|
|
|
{ |
|
226
|
|
|
$namespaceHelper = new NamespaceHelper(); |
|
227
|
|
|
$config = new Config(ConfigTest::SERVER); |
|
228
|
|
|
|
|
229
|
|
|
return new FieldTraitCreator( |
|
230
|
|
|
new FileFactory($namespaceHelper, $config), |
|
231
|
|
|
$namespaceHelper, |
|
232
|
|
|
new Writer(), |
|
233
|
|
|
$config, |
|
234
|
|
|
new FindReplaceFactory(), |
|
235
|
|
|
new CodeHelper($namespaceHelper) |
|
236
|
|
|
); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @test |
|
241
|
|
|
*/ |
|
242
|
|
|
public function itHandlesDeeplyNestedFieldFqn(): void |
|
243
|
|
|
{ |
|
244
|
|
|
$newObjectFqn = |
|
245
|
|
|
'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\Deeply\\Nested\\TestDateTimeFieldTrait'; |
|
246
|
|
|
$actual = $this->getCreator() |
|
247
|
|
|
->setMappingHelperCommonType(MappingHelper::TYPE_DATETIME) |
|
248
|
|
|
->setUnique(true) |
|
249
|
|
|
->setNewObjectFqn($newObjectFqn) |
|
250
|
|
|
->createTargetFileObject() |
|
251
|
|
|
->getTargetFile() |
|
252
|
|
|
->getContents(); |
|
253
|
|
|
self::assertContains('EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\Deeply\\Nested', $actual); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @test |
|
258
|
|
|
*/ |
|
259
|
|
|
public function itGeneratesTheCorrectContentDatetime(): void |
|
260
|
|
|
{ |
|
261
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\TestDateTimeFieldTrait'; |
|
262
|
|
|
$expected = self::DATETIME_FIELD_TRAIT; |
|
263
|
|
|
$actual = $this->getCreator() |
|
264
|
|
|
->setMappingHelperCommonType(MappingHelper::TYPE_DATETIME) |
|
265
|
|
|
->setUnique(true) |
|
266
|
|
|
->setNewObjectFqn($newObjectFqn) |
|
267
|
|
|
->createTargetFileObject() |
|
268
|
|
|
->getTargetFile() |
|
269
|
|
|
->getContents(); |
|
270
|
|
|
self::assertSame(trim($expected), trim($actual)); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @test |
|
275
|
|
|
*/ |
|
276
|
|
|
public function itRequiresTheSuffix(): void |
|
277
|
|
|
{ |
|
278
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\TestArray'; |
|
279
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
280
|
|
|
$this->expectExceptionMessage('$newObjectFqn must end in FieldTrait'); |
|
281
|
|
|
$this->getCreator()->createTargetFileObject($newObjectFqn); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @test |
|
286
|
|
|
*/ |
|
287
|
|
|
public function itCanCreateABooleanFieldTrait(): void |
|
288
|
|
|
{ |
|
289
|
|
|
$contents = $this->itCanCreateAFieldTrait(MappingHelper::TYPE_BOOLEAN); |
|
290
|
|
|
self::assertContains('function isTestBoolean(): ?bool', $contents); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @test |
|
295
|
|
|
* |
|
296
|
|
|
* @param string $type |
|
297
|
|
|
* |
|
298
|
|
|
* @return string |
|
299
|
|
|
*/ |
|
300
|
|
|
public function itCanCreateAFieldTrait(string $type = MappingHelper::TYPE_DATETIME): string |
|
301
|
|
|
{ |
|
302
|
|
|
$newObjectFqn = |
|
303
|
|
|
'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\Test' . ucfirst($type) . 'FieldTrait'; |
|
304
|
|
|
$contents = $this->getCreator() |
|
305
|
|
|
->setMappingHelperCommonType($type) |
|
306
|
|
|
->createTargetFileObject($newObjectFqn) |
|
307
|
|
|
->getTargetFile() |
|
308
|
|
|
->getContents(); |
|
309
|
|
|
self::assertNotEmpty($contents); |
|
310
|
|
|
|
|
311
|
|
|
return $contents; |
|
|
|
|
|
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @test |
|
316
|
|
|
*/ |
|
317
|
|
|
public function itCanGenerateUniqueFields(): void |
|
318
|
|
|
{ |
|
319
|
|
|
$newObjectFqn = 'EdmondsCommerce\\DoctrineStaticMeta\\Entity\\Fields\\Traits\\TestUniqueStringFieldTrait'; |
|
320
|
|
|
$contents = $this->getCreator() |
|
321
|
|
|
->setMappingHelperCommonType(MappingHelper::TYPE_STRING) |
|
322
|
|
|
->setUnique(true) |
|
323
|
|
|
->createTargetFileObject($newObjectFqn) |
|
324
|
|
|
->getTargetFile() |
|
325
|
|
|
->getContents(); |
|
326
|
|
|
self::assertContains( |
|
327
|
|
|
'TestUniqueStringFieldInterface::DEFAULT_TEST_UNIQUE_STRING, |
|
328
|
|
|
true', |
|
329
|
|
|
$contents |
|
330
|
|
|
); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @test |
|
335
|
|
|
*/ |
|
336
|
|
|
public function itCreatesStringFieldsWithExtraValidation(): void |
|
337
|
|
|
{ |
|
338
|
|
|
$contents = $this->itCanCreateAFieldTrait(MappingHelper::PHP_TYPE_STRING); |
|
339
|
|
|
self::assertContains('new Length([\'min\' => 0, \'max\' => Database::MAX_VARCHAR_LENGTH])', $contents); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|