|
9 | 9 | use Laminas\Code\Generator\Exception\InvalidArgumentException; |
10 | 10 | use Laminas\Code\Generator\GeneratorInterface; |
11 | 11 | use Laminas\Code\Generator\MethodGenerator; |
| 12 | +use Laminas\Code\Generator\PromotedParameterGenerator; |
12 | 13 | use Laminas\Code\Generator\PropertyGenerator; |
13 | 14 | use Laminas\Code\Reflection\ClassReflection; |
| 15 | +use LaminasTest\Code\Generator\TestAsset\ClassWithPromotedParameter; |
14 | 16 | use LaminasTest\Code\TestAsset\FooClass; |
15 | 17 | use PHPUnit\Framework\TestCase; |
16 | 18 | use ReflectionMethod; |
@@ -1356,4 +1358,68 @@ class ClassWithFinalConst |
1356 | 1358 | $output = $classGenerator->generate(); |
1357 | 1359 | self::assertSame($expectedOutput, $output, $output); |
1358 | 1360 | } |
| 1361 | + |
| 1362 | + /** @requires PHP >= 8.0 */ |
| 1363 | + public function testGenerateClassWithPromotedConstructorParameter(): void |
| 1364 | + { |
| 1365 | + $classGenerator = new ClassGenerator(); |
| 1366 | + $classGenerator->setName('ClassWithPromotedParameter'); |
| 1367 | + |
| 1368 | + $classGenerator->addMethod('__construct', [ |
| 1369 | + new PromotedParameterGenerator( |
| 1370 | + 'bar', |
| 1371 | + 'Foo', |
| 1372 | + PromotedParameterGenerator::VISIBILITY_PRIVATE, |
| 1373 | + ), |
| 1374 | + ]); |
| 1375 | + |
| 1376 | + $expectedOutput = <<<EOS |
| 1377 | +class ClassWithPromotedParameter |
| 1378 | +{ |
| 1379 | + public function __construct(private \Foo \$bar) |
| 1380 | + { |
| 1381 | + } |
| 1382 | +} |
| 1383 | +
|
| 1384 | +EOS; |
| 1385 | + |
| 1386 | + self::assertEquals($expectedOutput, $classGenerator->generate()); |
| 1387 | + } |
| 1388 | + |
| 1389 | + /** @requires PHP >= 8.0 */ |
| 1390 | + public function testClassWithPromotedParameterFromReflection(): void |
| 1391 | + { |
| 1392 | + $classGenerator = ClassGenerator::fromReflection( |
| 1393 | + new ClassReflection(ClassWithPromotedParameter::class) |
| 1394 | + ); |
| 1395 | + |
| 1396 | + $expectedOutput = <<<EOS |
| 1397 | +namespace LaminasTest\Code\Generator\TestAsset; |
| 1398 | +
|
| 1399 | +class ClassWithPromotedParameter |
| 1400 | +{ |
| 1401 | + public function __construct(private string \$promotedParameter) |
| 1402 | + { |
| 1403 | + } |
| 1404 | +} |
| 1405 | +
|
| 1406 | +EOS; |
| 1407 | + |
| 1408 | + self::assertEquals($expectedOutput, $classGenerator->generate()); |
| 1409 | + } |
| 1410 | + |
| 1411 | + /** @requires PHP >= 8.0 */ |
| 1412 | + public function testFailToGenerateClassWithPromotedParameterOnNonConstructorMethod(): void |
| 1413 | + { |
| 1414 | + $classGenerator = new ClassGenerator(); |
| 1415 | + $classGenerator->setName('promotedParameterOnNonConstructorMethod'); |
| 1416 | + |
| 1417 | + $this->expectExceptionObject( |
| 1418 | + new InvalidArgumentException('Promoted parameter can only be added to constructor.') |
| 1419 | + ); |
| 1420 | + |
| 1421 | + $classGenerator->addMethod('thisIsNoConstructor', [ |
| 1422 | + new PromotedParameterGenerator('promotedParameter', 'string'), |
| 1423 | + ]); |
| 1424 | + } |
1359 | 1425 | } |
0 commit comments