File: PropertyGeneratorTest.php

package info (click to toggle)
php-zend-code 4.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,648 kB
  • sloc: php: 13,824; xml: 2,327; makefile: 16
file content (424 lines) | stat: -rw-r--r-- 16,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php

namespace LaminasTest\Code\Generator;

use Generator;
use Laminas\Code\Generator\DocBlock\Tag\VarTag;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\Exception\InvalidArgumentException;
use Laminas\Code\Generator\Exception\RuntimeException;
use Laminas\Code\Generator\PropertyGenerator;
use Laminas\Code\Generator\PropertyValueGenerator;
use Laminas\Code\Generator\TypeGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Laminas\Code\Reflection\ClassReflection;
use Laminas\Code\Reflection\PropertyReflection;
use LaminasTest\Code\Generator\TestAsset\ClassWithTypedProperty;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use stdClass;

use function addslashes;
use function array_shift;
use function str_replace;
use function uniqid;

#[Group('Laminas_Code_Generator')]
#[Group('Laminas_Code_Generator_Php')]
class PropertyGeneratorTest extends TestCase
{
    public function testPropertyConstructor(): void
    {
        $codeGenProperty = new PropertyGenerator();
        self::assertInstanceOf(PropertyGenerator::class, $codeGenProperty);
    }

    /**
     * @return bool[][]|string[][]|int[][]|null[][]
     */
    public static function dataSetTypeSetValueGenerate(): array
    {
        return [
            ['string', 'foo', "'foo';"],
            ['int', 1, '1;'],
            ['integer', 1, '1;'],
            ['bool', true, 'true;'],
            ['bool', false, 'false;'],
            ['boolean', true, 'true;'],
            ['number', 1, '1;'],
            ['float', 1.23, '1.23;'],
            ['double', 1.23, '1.23;'],
            ['constant', 'FOO', 'FOO;'],
            ['null', null, 'null;'],
        ];
    }

    #[DataProvider('dataSetTypeSetValueGenerate')]
    public function testSetTypeSetValueGenerate(string $type, mixed $value, string $code): void
    {
        $defaultValue = new PropertyValueGenerator();
        $defaultValue->setType($type);
        $defaultValue->setValue($value);

        self::assertSame($type, $defaultValue->getType());
        self::assertSame($code, $defaultValue->generate());
    }

    #[DataProvider('dataSetTypeSetValueGenerate')]
    public function testSetBogusTypeSetValueGenerateUseAutoDetection(string $type, mixed $value, string $code): void
    {
        if ('constant' === $type) {
            self::markTestSkipped('constant can only be detected explicitly');
        }

        $defaultValue = new PropertyValueGenerator();
        $defaultValue->setType('bogus');
        $defaultValue->setValue($value);

        self::assertSame($code, $defaultValue->generate());
    }

    public function testPropertyReturnsSimpleValue(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'some string value');
        self::assertSame('    public $someVal = \'some string value\';', $codeGenProperty->generate());
    }

    public function testPropertyMultilineValue(): void
    {
        $targetValue = [
            5,
            'one'   => 1,
            'two'   => '2',
            'null'  => null,
            'true'  => true,
            "bar's" => "bar's",
        ];

        $expectedSource = <<<EOS
    public \$myFoo = [
        5,
        'one' => 1,
        'two' => '2',
        'null' => null,
        'true' => true,
        'bar\'s' => 'bar\'s',
    ];
EOS;

        $property = new PropertyGenerator('myFoo', $targetValue);

        $targetSource = $property->generate();
        $targetSource = str_replace("\r", '', $targetSource);

        self::assertSame($expectedSource, $targetSource);
    }

    public static function visibility(): Generator
    {
        yield 'public' => [PropertyGenerator::FLAG_PUBLIC, 'public'];
        yield 'protected' => [PropertyGenerator::FLAG_PROTECTED, 'protected'];
        yield 'private' => [PropertyGenerator::FLAG_PRIVATE, 'private'];
    }

    #[DataProvider('visibility')]
    public function testPropertyCanProduceConstantWithVisibility(int $flag, string $visibility): void
    {
        $codeGenProperty = new PropertyGenerator('FOO', 'bar', [PropertyGenerator::FLAG_CONSTANT, $flag]);
        self::assertSame('    ' . $visibility . ' const FOO = \'bar\';', $codeGenProperty->generate());
    }

    public function testPropertyCanProduceConstantModifier(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'some string value', PropertyGenerator::FLAG_CONSTANT);
        self::assertSame('    public const someVal = \'some string value\';', $codeGenProperty->generate());
    }

    public function testPropertyCanProduceFinalConstantModifier(): void
    {
        $codeGenProperty = new PropertyGenerator(
            'someVal',
            'some string value',
            PropertyGenerator::FLAG_CONSTANT | PropertyGenerator::FLAG_FINAL
        );
        self::assertSame('    final public const someVal = \'some string value\';', $codeGenProperty->generate());
    }

    #[DataProvider('visibility')]
    public function testPropertyCanProduceReadonlyModifier(int $flag, string $visibility): void
    {
        $codeGenProperty = new PropertyGenerator(
            'someVal',
            'some string value',
            PropertyGenerator::FLAG_READONLY | $flag
        );

        self::assertSame(
            '    ' . $visibility . ' readonly $someVal = \'some string value\';',
            $codeGenProperty->generate()
        );
    }

    public function testFailToProduceReadonlyStatic(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'some string value');

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Modifier "readonly" in combination with "static" not permitted.');

        $codeGenProperty->setFlags(PropertyGenerator::FLAG_READONLY | PropertyGenerator::FLAG_STATIC);
    }

    public function testFailToProduceReadonlyConstant(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'some string value');

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Modifier "readonly" in combination with "constant" not permitted.');

        $codeGenProperty->setFlags(PropertyGenerator::FLAG_READONLY | PropertyGenerator::FLAG_CONSTANT);
    }

    #[Group('PR-704')]
    public function testPropertyCanProduceConstantModifierWithSetter(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'some string value');
        $codeGenProperty->setConst(true);
        self::assertSame('    public const someVal = \'some string value\';', $codeGenProperty->generate());
    }

    public function testPropertyCanProduceStaticModifier(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'some string value', PropertyGenerator::FLAG_STATIC);
        self::assertSame('    public static $someVal = \'some string value\';', $codeGenProperty->generate());
    }

    #[Group('Laminas-6444')]
    public function testPropertyWillLoadFromReflection(): void
    {
        $reflectionClass = new ClassReflection(TestAsset\TestClassWithManyProperties::class);

        // test property 1
        $reflProp = $reflectionClass->getProperty('_bazProperty');

        $cgProp = PropertyGenerator::fromReflection($reflProp);

        self::assertSame('_bazProperty', $cgProp->getName());
        $bazPropertyValue = $cgProp->getDefaultValue();
        self::assertNotNull($bazPropertyValue);
        self::assertSame([true, false, true], $bazPropertyValue->getValue());
        self::assertSame('private', $cgProp->getVisibility());

        $reflProp = $reflectionClass->getProperty('_bazStaticProperty');

        // test property 2
        $cgProp = PropertyGenerator::fromReflection($reflProp);

        self::assertSame('_bazStaticProperty', $cgProp->getName());
        $bazStaticValue = $cgProp->getDefaultValue();
        self::assertNotNull($bazStaticValue);
        self::assertSame(TestAsset\TestClassWithManyProperties::FOO, $bazStaticValue->getValue());
        self::assertTrue($cgProp->isStatic());
        self::assertSame('private', $cgProp->getVisibility());
    }

    #[Group('Laminas-6444')]
    public function testPropertyWillEmitStaticModifier(): void
    {
        $codeGenProperty = new PropertyGenerator(
            'someVal',
            'some string value',
            PropertyGenerator::FLAG_STATIC | PropertyGenerator::FLAG_PROTECTED
        );
        self::assertSame('    protected static $someVal = \'some string value\';', $codeGenProperty->generate());
    }

    #[Group('Laminas-7205')]
    public function testPropertyCanHaveDocBlock(): void
    {
        $codeGenProperty = new PropertyGenerator(
            'someVal',
            'some string value',
            PropertyGenerator::FLAG_STATIC | PropertyGenerator::FLAG_PROTECTED
        );

        $codeGenProperty->setDocBlock('@var string $someVal This is some val');

        $expected = <<<EOS
    /**
     * @var string \$someVal This is some val
     */
    protected static \$someVal = 'some string value';
EOS;
        self::assertSame($expected, $codeGenProperty->generate());
    }

    public function testOtherTypesThrowExceptionOnGenerate(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', new stdClass());

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Type "stdClass" is unknown or cannot be used as property default value');

        $codeGenProperty->generate();
    }

    public function testCreateFromArray(): void
    {
        $propertyGenerator = PropertyGenerator::fromArray([
            'name'             => 'SampleProperty',
            'const'            => false,
            'defaultvalue'     => 'default-foo',
            'docblock'         => [
                'shortdescription' => 'foo',
            ],
            'abstract'         => true,
            'final'            => true,
            'static'           => true,
            'visibility'       => PropertyGenerator::VISIBILITY_PROTECTED,
            'omitdefaultvalue' => true,
            'type'             => TypeGenerator::fromTypeString(self::class),
        ]);

        self::assertSame('SampleProperty', $propertyGenerator->getName());
        self::assertFalse($propertyGenerator->isConst());
        self::assertFalse($propertyGenerator->isReadonly());
        self::assertInstanceOf(ValueGenerator::class, $propertyGenerator->getDefaultValue());
        self::assertInstanceOf(DocBlockGenerator::class, $propertyGenerator->getDocBlock());
        self::assertTrue($propertyGenerator->isAbstract());
        self::assertTrue($propertyGenerator->isFinal());
        self::assertTrue($propertyGenerator->isStatic());
        self::assertSame(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility());
        self::assertStringNotContainsString('default-foo', $propertyGenerator->generate());
        self::assertEquals(self::class, $propertyGenerator->getType());
        self::assertInstanceOf(TypeGenerator::class, $propertyGenerator->getType());
        $reflectionOmitDefaultValue = new ReflectionProperty($propertyGenerator, 'omitDefaultValue');

        $reflectionOmitDefaultValue->setAccessible(true);

        self::assertTrue($reflectionOmitDefaultValue->getValue($propertyGenerator));
    }

    public function testCreateReadonlyFromArray(): void
    {
        $propertyGenerator = PropertyGenerator::fromArray([
            'name'     => 'ReadonlyProperty',
            'readonly' => true,
        ]);

        self::assertSame('ReadonlyProperty', $propertyGenerator->getName());
        self::assertFalse($propertyGenerator->isConst());
        self::assertFalse($propertyGenerator->isAbstract());
        self::assertFalse($propertyGenerator->isFinal());
        self::assertFalse($propertyGenerator->isStatic());
        self::assertTrue($propertyGenerator->isReadonly());
        self::assertSame(PropertyGenerator::VISIBILITY_PUBLIC, $propertyGenerator->getVisibility());
    }

    #[Group('3491')]
    public function testPropertyDocBlockWillLoadFromReflection(): void
    {
        $reflectionClass = new ClassReflection(TestAsset\TestClassWithManyProperties::class);

        $reflProp = $reflectionClass->getProperty('fooProperty');
        $cgProp   = PropertyGenerator::fromReflection($reflProp);

        self::assertSame('fooProperty', $cgProp->getName());

        $docBlock = $cgProp->getDocBlock();
        self::assertInstanceOf(DocBlockGenerator::class, $docBlock);
        $tags = $docBlock->getTags();
        self::assertIsArray($tags);
        self::assertCount(1, $tags);
        $tag = array_shift($tags);
        self::assertInstanceOf(VarTag::class, $tag);
        self::assertSame('var', $tag->getName());
    }

    #[DataProvider('dataSetTypeSetValueGenerate')]
    public function testSetDefaultValue(string $type, mixed $value): void
    {
        $property = new PropertyGenerator();
        $property->setDefaultValue($value, $type);

        $valueGenerator = $property->getDefaultValue();
        self::assertNotNull($valueGenerator);
        self::assertSame($type, $valueGenerator->getType());
        self::assertSame($value, $valueGenerator->getValue());
    }

    public function testOmitType()
    {
        $property = new PropertyGenerator('foo', null);
        $property->omitDefaultValue();

        self::assertSame('    public $foo;', $property->generate());
    }

    public function testFromReflectionOmitsDefaultValueIfItIsNull(): void
    {
        $reflectionClass    = new ClassReflection(TestAsset\TestClassWithManyProperties::class);
        $propertyReflection = $reflectionClass->getProperty('fooStaticProperty');

        $generator = PropertyGenerator::fromReflection($propertyReflection);
        $code      = $generator->generate();

        $this->assertSame('    public static $fooStaticProperty;', $code);
    }

    public function testFromReflectionWithTypeHintInTypedProperty(): void
    {
        $reflectionProperty = new PropertyReflection(ClassWithTypedProperty::class, 'typedProperty');

        $generator = PropertyGenerator::fromReflection($reflectionProperty);
        $code      = $generator->generate();

        self::assertSame('    private string $typedProperty;', $code);
    }

    public function testFromReflectionReadonlyProperty(): void
    {
        $className = uniqid('ClassWithReadonlyProperty', false);

        eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{ public readonly string $readonly; }');

        $reflectionProperty = new PropertyReflection(__NAMESPACE__ . '\\' . $className, 'readonly');

        $generator = PropertyGenerator::fromReflection($reflectionProperty);
        $code      = $generator->generate();

        self::assertSame('    public readonly string $readonly;', $code);
    }

    public function testPropertyCanProduceTypeHinting(): void
    {
        $codeGenProperty = new PropertyGenerator('someVal', 'value', [], TypeGenerator::fromTypeString('SomeClass'));
        self::assertSame('    public \SomeClass $someVal = \'value\';', $codeGenProperty->generate());
    }

    public function testFromArrayWithIncorrectTypePassed(): void
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessageMatches('/is expecting ' . addslashes(TypeGenerator::class) . '/');

        PropertyGenerator::fromArray([
            'name' => 'someVal',
            'type' => 'invalidStringn',
        ]);
    }

    public function testCanChangeTypeForPropertyGenerator(): void
    {
        $property = new PropertyGenerator('p', null, [], TypeGenerator::fromTypeString('?string'));
        self::assertSame('    public ?string $p = null;', $property->generate());

        $property->setType(null);
        self::assertSame(
            '    public $p = null;',
            $property->generate(),
            'A property type can be dropped'
        );
    }
}