From 97b268e240d9a640d2e6f20d72830009462e5e75 Mon Sep 17 00:00:00 2001 From: Zan Baldwin Date: Fri, 11 Feb 2022 20:35:53 +0100 Subject: [PATCH] Add Unit Test for Readonly Properties on PHP 8.1 --- .../Proxy/Php81ReadonlyPublicPropertyType.php | 13 +++++++ .../Tests/Common/Proxy/ProxyGeneratorTest.php | 35 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/Doctrine/Tests/Common/Proxy/Php81ReadonlyPublicPropertyType.php diff --git a/tests/Doctrine/Tests/Common/Proxy/Php81ReadonlyPublicPropertyType.php b/tests/Doctrine/Tests/Common/Proxy/Php81ReadonlyPublicPropertyType.php new file mode 100644 index 000000000..011cbfa11 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Proxy/Php81ReadonlyPublicPropertyType.php @@ -0,0 +1,13 @@ += 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 */