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