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 97b268e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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 97b268e

Please sign in to comment.