Skip to content

Commit

Permalink
Add Unit Test for Readonly Properties on PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zanbaldwin committed Feb 11, 2022
1 parent 629cdb6 commit 830786e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ private function isShortIdentifierGetter($method, ClassMetadata $class)
/**
* Generates the list of public properties to be lazy loaded, that are writable.
*
* @return array<int, string>
* @return list<string>
*/
public function getWriteableLazyLoadedPublicPropertiesNames(ClassMetadata $class): array
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Doctrine\Tests\Common\Proxy;

class Php81ReadonlyPublicPropertyType
{
public readonly string $readable;
public string $writeable = 'default';

public function __construct(
public readonly string $id,
) {}
}
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/ProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,41 @@ public function testPhp81NeverType()
);
}

/**
* @requires PHP >= 8.1.0
*/
public function testPhp81ReadonlyPublicProperties()
{
$className = Php81ReadonlyPublicPropertyType::class;
$proxyClassName = 'Doctrine\Tests\Common\ProxyProxy\__CG__\Php81ReadonlyPublicPropertyType';

if ( ! class_exists($proxyClassName, false)) {
$metadata = $this->createClassMetadata($className, ['id']);

$metadata
->expects($this->any())
->method('hasField')
->will($this->returnCallback(static function ($fieldName) {
return in_array($fieldName, ['id', 'readable', 'writeable']);
}));

$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy');
$this->generateAndRequire($proxyGenerator, $metadata);
}

// Readonly properties are removed from unset.
self::assertStringContainsString(
'unset($this->writeable);',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp81ReadonlyPublicPropertyType.php')
);

// But remain in property listings.
self::assertStringContainsString(
"'readable' => NULL",
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp81ReadonlyPublicPropertyType.php')
);
}

/**
* @requires PHP >= 8.1.0
*/
Expand Down

0 comments on commit 830786e

Please sign in to comment.