|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class CodeHelperTest |
|
11
|
|
|
* |
|
12
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration |
|
13
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
14
|
|
|
* @coversDefaultClass \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper |
|
15
|
|
|
*/ |
|
16
|
|
|
class CodeHelperTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var CodeHelper |
|
21
|
|
|
*/ |
|
22
|
|
|
private $helper; |
|
23
|
|
|
|
|
24
|
|
|
public function setup() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->helper = new CodeHelper(new NamespaceHelper()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
* @small |
|
32
|
|
|
* @covers ::classy |
|
33
|
|
|
*/ |
|
34
|
|
|
public function classy(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$inputToExpected = [ |
|
37
|
|
|
'AlreadyClassy' => 'AlreadyClassy', |
|
38
|
|
|
'snake_casey' => 'SnakeCasey', |
|
39
|
|
|
'lower' => 'Lower', |
|
40
|
|
|
]; |
|
41
|
|
|
$actual = []; |
|
42
|
|
|
foreach (array_keys($inputToExpected) as $input) { |
|
43
|
|
|
$actual[$input] = $this->helper->classy($input); |
|
44
|
|
|
} |
|
45
|
|
|
self::assertSame($inputToExpected, $actual); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @test |
|
50
|
|
|
* @small |
|
51
|
|
|
* @covers ::consty |
|
52
|
|
|
*/ |
|
53
|
|
|
public function consty(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$inputToExpected = [ |
|
56
|
|
|
'ALREADY_CONSTY' => 'ALREADY_CONSTY', |
|
57
|
|
|
'snake_casey' => 'SNAKE_CASEY', |
|
58
|
|
|
'lower' => 'LOWER', |
|
59
|
|
|
'WasClassy' => 'WAS_CLASSY', |
|
60
|
|
|
]; |
|
61
|
|
|
$actual = []; |
|
62
|
|
|
foreach (array_keys($inputToExpected) as $input) { |
|
63
|
|
|
$actual[$input] = $this->helper->consty($input); |
|
64
|
|
|
} |
|
65
|
|
|
self::assertSame($inputToExpected, $actual); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @test |
|
70
|
|
|
* @small |
|
71
|
|
|
* @covers ::propertyish |
|
72
|
|
|
*/ |
|
73
|
|
|
public function propertyish(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$inputToExpected = [ |
|
76
|
|
|
'alreadyPropertyish' => 'alreadyPropertyish', |
|
77
|
|
|
'snake_casey' => 'snakeCasey', |
|
78
|
|
|
'lower' => 'lower', |
|
79
|
|
|
'WasClassy' => 'wasClassy', |
|
80
|
|
|
]; |
|
81
|
|
|
$actual = []; |
|
82
|
|
|
foreach (array_keys($inputToExpected) as $input) { |
|
83
|
|
|
$actual[$input] = $this->helper->propertyIsh($input); |
|
84
|
|
|
} |
|
85
|
|
|
self::assertSame($inputToExpected, $actual); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @test |
|
90
|
|
|
* @small |
|
91
|
|
|
* @covers ::fixSuppressWarningsTags |
|
92
|
|
|
*/ |
|
93
|
|
|
public function fixSuppressWarningsTags(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$generated = '@SuppressWarnings (Something)'; |
|
96
|
|
|
$expected = '@SuppressWarnings(Something)'; |
|
97
|
|
|
$actual = $this->helper->fixSuppressWarningsTags($generated); |
|
98
|
|
|
self::assertSame($expected, $actual); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @test |
|
103
|
|
|
* @small |
|
104
|
|
|
* @covers ::makeConstsPublic |
|
105
|
|
|
*/ |
|
106
|
|
|
public function makeConstsPublic(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$generated = ' const THIS="that"'; |
|
109
|
|
|
$expected = ' public const THIS="that"'; |
|
110
|
|
|
$actual = $this->helper->makeConstsPublic($generated); |
|
111
|
|
|
self::assertSame($expected, $actual); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @test |
|
116
|
|
|
* @small |
|
117
|
|
|
* @covers ::breakImplementsAndExtendsOntoLines |
|
118
|
|
|
*/ |
|
119
|
|
|
public function breakImplementsAndExtendsOntoLines(): void |
|
120
|
|
|
{ |
|
121
|
|
|
// phpcs:disable |
|
122
|
|
|
$generated = ' |
|
123
|
|
|
class Address implements DSM\Interfaces\UsesPHPMetaDataInterface, DSM\Fields\Interfaces\IdFieldInterface, HasCustomers, ReciprocatesCustomer |
|
124
|
|
|
{ |
|
125
|
|
|
|
|
126
|
|
|
use DSM\Traits\UsesPHPMetaDataTrait; |
|
127
|
|
|
use DSM\Fields\Traits\IdFieldTrait; |
|
128
|
|
|
use HasCustomersInverseManyToMany; |
|
129
|
|
|
} |
|
130
|
|
|
'; |
|
131
|
|
|
// phpcs:enable |
|
132
|
|
|
$expected = ' |
|
133
|
|
|
class Address implements |
|
134
|
|
|
DSM\Interfaces\UsesPHPMetaDataInterface, |
|
135
|
|
|
DSM\Fields\Interfaces\IdFieldInterface, |
|
136
|
|
|
HasCustomers, |
|
137
|
|
|
ReciprocatesCustomer |
|
138
|
|
|
{ |
|
139
|
|
|
|
|
140
|
|
|
use DSM\Traits\UsesPHPMetaDataTrait; |
|
141
|
|
|
use DSM\Fields\Traits\IdFieldTrait; |
|
142
|
|
|
use HasCustomersInverseManyToMany; |
|
143
|
|
|
} |
|
144
|
|
|
'; |
|
145
|
|
|
$actual = $this->helper->breakImplementsAndExtendsOntoLines($generated); |
|
146
|
|
|
self::assertSame($expected, $actual); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @test |
|
151
|
|
|
* @small |
|
152
|
|
|
* @covers ::constArraysOnMultipleLines |
|
153
|
|
|
*/ |
|
154
|
|
|
public function constArraysOnMultipleLines(): void |
|
155
|
|
|
{ |
|
156
|
|
|
// phpcs:disable |
|
157
|
|
|
$generated = <<<PHP |
|
158
|
|
|
class Address |
|
159
|
|
|
{ |
|
160
|
|
|
const ITEM = [ 'this'=>1, 'that'=>2 ]; |
|
161
|
|
|
|
|
162
|
|
|
public const ITEM2 = [ 'this'=>1, 'that'=>2 ]; |
|
163
|
|
|
|
|
164
|
|
|
const SCALAR_FIELDS_TO_TYPES = ['domain' => MappingHelper::TYPE_STRING, 'added' => MappingHelper::TYPE_DATETIME, 'checkStatus' => MappingHelper::TYPE_STRING, 'checked' => MappingHelper::TYPE_DATETIME]; |
|
165
|
|
|
} |
|
166
|
|
|
PHP; |
|
167
|
|
|
// phpcs:enable |
|
168
|
|
|
$expected = <<<PHP |
|
169
|
|
|
class Address |
|
170
|
|
|
{ |
|
171
|
|
|
const ITEM = [ |
|
172
|
|
|
'this'=>1, |
|
173
|
|
|
'that'=>2 |
|
174
|
|
|
]; |
|
175
|
|
|
|
|
176
|
|
|
public const ITEM2 = [ |
|
177
|
|
|
'this'=>1, |
|
178
|
|
|
'that'=>2 |
|
179
|
|
|
]; |
|
180
|
|
|
|
|
181
|
|
|
const SCALAR_FIELDS_TO_TYPES = [ |
|
182
|
|
|
'domain' => MappingHelper::TYPE_STRING, |
|
183
|
|
|
'added' => MappingHelper::TYPE_DATETIME, |
|
184
|
|
|
'checkStatus' => MappingHelper::TYPE_STRING, |
|
185
|
|
|
'checked' => MappingHelper::TYPE_DATETIME |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
PHP; |
|
189
|
|
|
$actual = $this->helper->constArraysOnMultipleLines($generated); |
|
190
|
|
|
self::assertSame($expected, $actual); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @test |
|
195
|
|
|
* @small |
|
196
|
|
|
* @covers ::phpcsIgnoreUseSection |
|
197
|
|
|
*/ |
|
198
|
|
|
public function phpcsIgnoreUseSection(): void |
|
199
|
|
|
{ |
|
200
|
|
|
// phpcs:disable |
|
201
|
|
|
$generated = <<<PHP |
|
202
|
|
|
<?php |
|
203
|
|
|
declare(strict_types=1); |
|
204
|
|
|
|
|
205
|
|
|
namespace DSM\GeneratedCodeTest\Project\Entities\Order; |
|
206
|
|
|
|
|
207
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Attributes\Address\Interfaces\HasAddressInterface; |
|
208
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Attributes\Address\Traits\HasAddress\HasAddressUnidirectionalOneToOne; |
|
209
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Order\Interfaces\HasOrderInterface; |
|
210
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Order\Interfaces\ReciprocatesOrderInterface; |
|
211
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Order\Traits\HasOrder\HasOrderManyToOne; |
|
212
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity as DSM; |
|
213
|
|
|
|
|
214
|
|
|
class Address implements |
|
215
|
|
|
DSM\Interfaces\UsesPHPMetaDataInterface, |
|
216
|
|
|
DSM\Interfaces\ValidateInterface, |
|
217
|
|
|
DSM\Fields\Interfaces\IdFieldInterface, |
|
218
|
|
|
HasOrderInterface, |
|
219
|
|
|
ReciprocatesOrderInterface, |
|
220
|
|
|
HasAddressInterface |
|
221
|
|
|
{ |
|
222
|
|
|
|
|
223
|
|
|
use DSM\Traits\UsesPHPMetaDataTrait; |
|
224
|
|
|
use DSM\Traits\ValidateTrait; |
|
225
|
|
|
use DSM\Fields\Traits\IdFieldTrait; |
|
226
|
|
|
use HasOrderManyToOne; |
|
227
|
|
|
use HasAddressUnidirectionalOneToOne; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
PHP; |
|
231
|
|
|
$expected = <<<PHP |
|
232
|
|
|
<?php |
|
233
|
|
|
declare(strict_types=1); |
|
234
|
|
|
|
|
235
|
|
|
namespace DSM\GeneratedCodeTest\Project\Entities\Order; |
|
236
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
|
237
|
|
|
|
|
238
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Attributes\Address\Interfaces\HasAddressInterface; |
|
239
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Attributes\Address\Traits\HasAddress\HasAddressUnidirectionalOneToOne; |
|
240
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Order\Interfaces\HasOrderInterface; |
|
241
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Order\Interfaces\ReciprocatesOrderInterface; |
|
242
|
|
|
use DSM\GeneratedCodeTest\Project\Entity\Relations\Order\Traits\HasOrder\HasOrderManyToOne; |
|
243
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity as DSM; |
|
244
|
|
|
|
|
245
|
|
|
// phpcs:enable |
|
246
|
|
|
class Address implements |
|
247
|
|
|
DSM\Interfaces\UsesPHPMetaDataInterface, |
|
248
|
|
|
DSM\Interfaces\ValidateInterface, |
|
249
|
|
|
DSM\Fields\Interfaces\IdFieldInterface, |
|
250
|
|
|
HasOrderInterface, |
|
251
|
|
|
ReciprocatesOrderInterface, |
|
252
|
|
|
HasAddressInterface |
|
253
|
|
|
{ |
|
254
|
|
|
|
|
255
|
|
|
use DSM\Traits\UsesPHPMetaDataTrait; |
|
256
|
|
|
use DSM\Traits\ValidateTrait; |
|
257
|
|
|
use DSM\Fields\Traits\IdFieldTrait; |
|
258
|
|
|
use HasOrderManyToOne; |
|
259
|
|
|
use HasAddressUnidirectionalOneToOne; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
PHP; |
|
263
|
|
|
// phpcs:enable |
|
264
|
|
|
$actual = $this->helper->phpcsIgnoreUseSection($generated); |
|
265
|
|
|
self::assertSame($expected, $actual); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @test |
|
270
|
|
|
* @small |
|
271
|
|
|
* @covers ::getGetterMethodNameForBoolean |
|
272
|
|
|
*/ |
|
273
|
|
|
public function itWillReturnAnIsMethodForABooleanField(): void |
|
274
|
|
|
{ |
|
275
|
|
|
$fieldName = 'testField'; |
|
276
|
|
|
$methodName = $this->helper->getGetterMethodNameForBoolean($fieldName); |
|
277
|
|
|
self::assertSame('isTestField', $methodName); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @test |
|
282
|
|
|
* @small |
|
283
|
|
|
* @covers ::getGetterMethodNameForBoolean |
|
284
|
|
|
*/ |
|
285
|
|
|
public function itWillNotReturnIsTwice(): void |
|
286
|
|
|
{ |
|
287
|
|
|
$fieldName = 'isReadOnly'; |
|
288
|
|
|
$methodName = $this->helper->getGetterMethodNameForBoolean($fieldName); |
|
289
|
|
|
self::assertSame($fieldName, $methodName); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @test |
|
294
|
|
|
* @small |
|
295
|
|
|
* @covers ::getGetterMethodNameForBoolean |
|
296
|
|
|
*/ |
|
297
|
|
|
public function itWillNotReturnIsHasInTheMethodName(): void |
|
298
|
|
|
{ |
|
299
|
|
|
$fieldName = 'hasHeaders'; |
|
300
|
|
|
$methodName = $this->helper->getGetterMethodNameForBoolean($fieldName); |
|
301
|
|
|
self::assertSame($fieldName, $methodName); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|