1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Medium; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Attribute\WeightEmbeddable; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Financial\MoneyEmbeddable; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Geo\AddressEmbeddable; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Identity\FullNameEmbeddable; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use ts\Reflection\ReflectionClass; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta |
20
|
|
|
* @medium |
21
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
22
|
|
|
*/ |
23
|
|
|
class DoctrineStaticMetaTest extends AbstractTest |
24
|
|
|
{ |
25
|
|
|
public const WORK_DIR = self::VAR_PATH . '/' . self::TEST_TYPE_MEDIUM . '/DoctrineStaticMetaTest'; |
26
|
|
|
|
27
|
|
|
private const TEST_ENTITY_FQN = self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_PERSON; |
28
|
|
|
|
29
|
|
|
private const TEST_CUSTOM_RELATION_FQN = |
30
|
|
|
self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_CUSTOM_RELATION; |
31
|
|
|
|
32
|
|
|
protected static $buildOnce = true; |
33
|
|
|
|
34
|
|
|
public function setup() |
35
|
|
|
{ |
36
|
|
|
parent::setUp(); |
37
|
|
|
if (false === self::$built) { |
38
|
|
|
$this->getTestCodeGenerator() |
39
|
|
|
->copyTo(self::WORK_DIR); |
40
|
|
|
self::$built = true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
*/ |
47
|
|
|
public function itCanGetAndSetMetaData(): void |
48
|
|
|
{ |
49
|
|
|
$expected = new ClassMetadata(self::TEST_ENTITY_FQN); |
50
|
|
|
$actual = $this->getDsm()->setMetaData($expected)->getMetaData(); |
51
|
|
|
self::assertSame($expected, $actual); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getDsm($entityFqn = self::TEST_ENTITY_FQN): DoctrineStaticMeta |
55
|
|
|
{ |
56
|
|
|
return new DoctrineStaticMeta($entityFqn); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
|
public function itCanGetEmbeddableProperties(): void |
63
|
|
|
{ |
64
|
|
|
$expected = [ |
65
|
|
|
'moneyEmbeddable' => MoneyEmbeddable::class, |
66
|
|
|
'addressEmbeddable' => AddressEmbeddable::class, |
67
|
|
|
'fullNameEmbeddable' => FullNameEmbeddable::class, |
68
|
|
|
'weightEmbeddable' => WeightEmbeddable::class, |
69
|
|
|
]; |
70
|
|
|
$actual = $this->getDsm(self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_ALL_EMBEDDABLES) |
71
|
|
|
->getEmbeddableProperties(); |
72
|
|
|
self::assertSame($expected, $actual); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function itCanGetGetters(): void |
79
|
|
|
{ |
80
|
|
|
$expected = [ |
81
|
|
|
'getAttributesAddress', |
82
|
|
|
'getAttributesEmails', |
83
|
|
|
'getCompanyDirector', |
84
|
|
|
'getLargeRelation', |
85
|
|
|
'getId', |
86
|
|
|
'getUuid', |
87
|
|
|
'getString', |
88
|
|
|
'getDatetime', |
89
|
|
|
'getFloat', |
90
|
|
|
'getDecimal', |
91
|
|
|
'getInteger', |
92
|
|
|
'getText', |
93
|
|
|
'isBoolean', |
94
|
|
|
'getArray', |
95
|
|
|
'getObject', |
96
|
|
|
]; |
97
|
|
|
$actual = $this->getDsm()->getGetters(); |
98
|
|
|
self::assertSame($expected, $actual); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
*/ |
104
|
|
|
public function itCanGetPlural(): void |
105
|
|
|
{ |
106
|
|
|
$expected = 'people'; |
107
|
|
|
$actual = $this->getDsm()->getPlural(); |
108
|
|
|
self::assertSame($expected, $actual); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @test |
113
|
|
|
*/ |
114
|
|
|
public function itCanGetReflection(): void |
115
|
|
|
{ |
116
|
|
|
$expected = new ReflectionClass(self::TEST_ENTITY_FQN); |
117
|
|
|
$actual = $this->getDsm()->getReflectionClass(); |
118
|
|
|
self::assertEquals($expected, $actual); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @throws ReflectionException |
123
|
|
|
* @test |
124
|
|
|
*/ |
125
|
|
|
public function itCanGetRequiredRelationProperties(): void |
126
|
|
|
{ |
127
|
|
|
$expected = [ |
128
|
|
|
'person' => |
129
|
|
|
[ |
130
|
|
|
'My\Test\Project\Entity\Interfaces\PersonInterface', |
131
|
|
|
false, |
132
|
|
|
], |
133
|
|
|
'orderAddresses' => |
134
|
|
|
[ |
135
|
|
|
'My\Test\Project\Entity\Interfaces\Order\AddressInterface', |
136
|
|
|
true, |
137
|
|
|
], |
138
|
|
|
]; |
139
|
|
|
$entityFqn = self::TEST_ENTITIES_ROOT_NAMESPACE . TestCodeGenerator::TEST_ENTITY_ORDER; |
140
|
|
|
$required = $this->getDsm($entityFqn) |
141
|
|
|
->getRequiredRelationProperties(); |
142
|
|
|
$actual = []; |
143
|
|
|
foreach ($required as $property => $relation) { |
144
|
|
|
$actual[$property] = [ |
145
|
|
|
$relation->getRelationEntityFqn(), |
146
|
|
|
$relation->isPluralRelation(), |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
self::assertSame($expected, $actual); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @throws ReflectionException |
154
|
|
|
* @test |
155
|
|
|
*/ |
156
|
|
|
public function itCanGetRequiredRelationPropertiesForCustomRelations(): void |
157
|
|
|
{ |
158
|
|
|
$expected = [ |
159
|
|
|
'assignedTo' => [ |
160
|
|
|
'My\Test\Project\Entity\Interfaces\CompanyInterface', |
161
|
|
|
false, |
162
|
|
|
|
163
|
|
|
], |
164
|
|
|
'company' => [ |
165
|
|
|
'My\Test\Project\Entity\Interfaces\CompanyInterface', |
166
|
|
|
false, |
167
|
|
|
], |
168
|
|
|
|
169
|
|
|
]; |
170
|
|
|
$entityFqn = self::TEST_CUSTOM_RELATION_FQN; |
171
|
|
|
$required = $this->getDsm($entityFqn) |
172
|
|
|
->getRequiredRelationProperties(); |
173
|
|
|
$actual = []; |
174
|
|
|
foreach ($required as $property => $relation) { |
175
|
|
|
$actual[$property] = [ |
176
|
|
|
$relation->getRelationEntityFqn(), |
177
|
|
|
$relation->isPluralRelation(), |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
self::assertSame($expected, $actual); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @test |
185
|
|
|
*/ |
186
|
|
|
public function itCanGetSetters(): void |
187
|
|
|
{ |
188
|
|
|
$expected = [ |
189
|
|
|
'getAttributesAddress' => 'setAttributesAddress', |
190
|
|
|
'getAttributesEmails' => 'setAttributesEmails', |
191
|
|
|
'getCompanyDirector' => 'setCompanyDirector', |
192
|
|
|
'getLargeRelation' => 'setLargeRelation', |
193
|
|
|
'getId' => 'setId', |
194
|
|
|
'getString' => 'setString', |
195
|
|
|
'getDatetime' => 'setDatetime', |
196
|
|
|
'getFloat' => 'setFloat', |
197
|
|
|
'getDecimal' => 'setDecimal', |
198
|
|
|
'getInteger' => 'setInteger', |
199
|
|
|
'getText' => 'setText', |
200
|
|
|
'isBoolean' => 'setBoolean', |
201
|
|
|
'getArray' => 'setArray', |
202
|
|
|
'getObject' => 'setObject', |
203
|
|
|
]; |
204
|
|
|
$actual = $this->getDsm()->getSetters(); |
205
|
|
|
self::assertSame($expected, $actual); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @test |
210
|
|
|
*/ |
211
|
|
|
public function itCanGetShortName(): void |
212
|
|
|
{ |
213
|
|
|
$expected = 'Person'; |
214
|
|
|
$actual = $this->getDsm()->getShortName(); |
215
|
|
|
self::assertSame($expected, $actual); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @test |
220
|
|
|
*/ |
221
|
|
|
public function itCanGetSingular(): void |
222
|
|
|
{ |
223
|
|
|
$expected = 'person'; |
224
|
|
|
$actual = $this->getDsm()->getSingular(); |
225
|
|
|
self::assertSame($expected, $actual); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @test |
230
|
|
|
*/ |
231
|
|
|
public function itCanGetStaticMethods(): void |
232
|
|
|
{ |
233
|
|
|
$expectedCount = 34; |
234
|
|
|
$actual = $this->getDsm()->getStaticMethods(); |
235
|
|
|
self::assertCount($expectedCount, $actual); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $getterName |
240
|
|
|
* @param string $expectedPropertyName |
241
|
|
|
* |
242
|
|
|
* @test |
243
|
|
|
* @dataProvider provideGetterNamesToPropertyNames |
244
|
|
|
*/ |
245
|
|
|
public function itCanGetThePropertyNameFromTheGetterName(string $getterName, string $expectedPropertyName): void |
246
|
|
|
{ |
247
|
|
|
$actualPropertyName = $this->getDsm()->getPropertyNameFromGetterName($getterName); |
248
|
|
|
self::assertSame($expectedPropertyName, $actualPropertyName); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $propertyName |
253
|
|
|
* @param string $expectedSetterName |
254
|
|
|
* |
255
|
|
|
* @test |
256
|
|
|
* @dataProvider provideSetterNamesToPropertyNames |
257
|
|
|
*/ |
258
|
|
|
public function itCanGetTheSetterNameForThePropertyName(string $expectedSetterName, string $propertyName): void |
259
|
|
|
{ |
260
|
|
|
$actualSetterName = $this->getDsm()->getSetterNameFromPropertyName($propertyName); |
261
|
|
|
self::assertSame($expectedSetterName, $actualSetterName); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $setterName |
266
|
|
|
* @param string $expectedPropertyName |
267
|
|
|
* |
268
|
|
|
* @test |
269
|
|
|
* @dataProvider provideSetterNamesToPropertyNames |
270
|
|
|
*/ |
271
|
|
|
public function itCanSetThePropertyNameFromTheSetterName(string $setterName, string $expectedPropertyName): void |
272
|
|
|
{ |
273
|
|
|
$actualPropertyName = $this->getDsm()->getPropertyNameFromSetterName($setterName); |
274
|
|
|
self::assertSame($expectedPropertyName, $actualPropertyName); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function provideGetterNamesToPropertyNames(): array |
278
|
|
|
{ |
279
|
|
|
return [ |
280
|
|
|
'getAttributesEmails' => ['getAttributesEmails', 'attributesEmails'], |
281
|
|
|
'getAttributesAddress' => ['getAttributesAddress', 'attributesAddress'], |
282
|
|
|
'getCompanyDirector' => ['getCompanyDirector', 'companyDirector'], |
283
|
|
|
'getLargeRelation' => ['getLargeRelation', 'largeRelation'], |
284
|
|
|
'getId' => ['getId', 'id'], |
285
|
|
|
'getString' => ['getString', 'string'], |
286
|
|
|
'getDatetime' => ['getDatetime', 'datetime'], |
287
|
|
|
'getFloat' => ['getFloat', 'float'], |
288
|
|
|
'getDecimal' => ['getDecimal', 'decimal'], |
289
|
|
|
'getInteger' => ['getInteger', 'integer'], |
290
|
|
|
'getText' => ['getText', 'text'], |
291
|
|
|
'isBoolean' => ['getBoolean', 'boolean'], |
292
|
|
|
'getArray' => ['getArray', 'array'], |
293
|
|
|
'getObject' => ['getObject', 'object'], |
294
|
|
|
]; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function provideSetterNamesToPropertyNames(): array |
298
|
|
|
{ |
299
|
|
|
return [ |
300
|
|
|
'setAttributesEmails' => ['setAttributesEmails', 'attributesEmails'], |
301
|
|
|
'setAttributesAddress' => ['setAttributesAddress', 'attributesAddress'], |
302
|
|
|
'setCompanyDirector' => ['setCompanyDirector', 'companyDirector'], |
303
|
|
|
'setLargeRelation' => ['setLargeRelation', 'largeRelation'], |
304
|
|
|
'setId' => ['setId', 'id'], |
305
|
|
|
'setString' => ['setString', 'string'], |
306
|
|
|
'setDatetime' => ['setDatetime', 'datetime'], |
307
|
|
|
'setFloat' => ['setFloat', 'float'], |
308
|
|
|
'setDecimal' => ['setDecimal', 'decimal'], |
309
|
|
|
'setInteger' => ['setInteger', 'integer'], |
310
|
|
|
'setText' => ['setText', 'text'], |
311
|
|
|
'setBoolean' => ['setBoolean', 'boolean'], |
312
|
|
|
'setArray' => ['setArray', 'array'], |
313
|
|
|
'setObject' => ['setObject', 'object'], |
314
|
|
|
]; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|