Skip to content

Commit

Permalink
Promoted constructor properties should not be doubled
Browse files Browse the repository at this point in the history
  • Loading branch information
Grundik committed May 16, 2023
1 parent 17ae929 commit 63c73a3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
use ProxyManagerTestAsset\ClassWithMixedProperties;
use ProxyManagerTestAsset\ClassWithPromotedProperties;
use ProxyManagerTestAsset\ClassWithVariadicConstructorArgument;
use ProxyManagerTestAsset\EmptyClass;
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties;
Expand Down Expand Up @@ -133,4 +134,25 @@ public function testBodyStructureWithVariadicArguments(): void

self::assertSame($expectedCode, $constructor->getBody());
}

public function testConstructorPropertyPromotion(): void
{
$valueHolder = $this->createMock(PropertyGenerator::class);

$valueHolder->method('getName')->willReturn('foo');

$constructor = Constructor::generateMethod(
new ReflectionClass(ClassWithPromotedProperties::class),
$valueHolder
);

self::assertSame('__construct', $constructor->getName());
$parameters = $constructor->getParameters();
self::assertCount(2, $parameters);

// Promoted constructor properties should not be doubled, since they are inherited anyway
$this->assertSame('int $amount', $parameters['amount']->generate());
$this->assertSame('?int $nullableAmount', $parameters['nullableAmount']->generate());
}

}
32 changes: 32 additions & 0 deletions tests/ProxyManagerTestAsset/ClassWithPromotedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace ProxyManagerTestAsset;

/**
* Class with a promoted constructor properties
*
* @license MIT
*/
class ClassWithPromotedProperties
{
/**
* @param int $increment
*/
public function __construct(
protected int $amount,
protected ?int $nullableAmount
) {
}

public function getAmount(): int
{
return $this->amount;
}

public function getNullableAmount(): ?int
{
return $this->nullableAmount;
}
}

0 comments on commit 63c73a3

Please sign in to comment.