Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP error instead of PHPUnit error when trying to create test double for read-only class #5113

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Framework/MockObject/Exception/ClassIsReadonlyException.php
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\MockObject;

use function sprintf;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class ClassIsReadonlyException extends \PHPUnit\Framework\Exception implements Exception
{
public function __construct(string $className)
{
parent::__construct(
sprintf(
'Class "%s" is declared "readonly" and cannot be doubled',
$className
)
);
}
}
9 changes: 9 additions & 0 deletions src/Framework/MockObject/Generator.php
Expand Up @@ -81,6 +81,7 @@ final class Generator
* @throws ClassAlreadyExistsException
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws ClassIsReadonlyException
* @throws DuplicateMethodException
* @throws InvalidMethodNameException
* @throws OriginalConstructorInvocationRequiredException
Expand Down Expand Up @@ -202,6 +203,7 @@ public function getMockForInterfaces(array $interfaces, bool $callAutoload = tru
* @throws ClassAlreadyExistsException
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws ClassIsReadonlyException
* @throws DuplicateMethodException
* @throws InvalidArgumentException
* @throws InvalidMethodNameException
Expand Down Expand Up @@ -264,6 +266,7 @@ interface_exists($originalClassName, $callAutoload)) {
* @throws ClassAlreadyExistsException
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws ClassIsReadonlyException
* @throws DuplicateMethodException
* @throws InvalidArgumentException
* @throws InvalidMethodNameException
Expand Down Expand Up @@ -347,6 +350,7 @@ public function getObjectForTrait(string $traitName, string $traitClassName = ''
/**
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws ClassIsReadonlyException
* @throws ReflectionException
* @throws RuntimeException
*/
Expand Down Expand Up @@ -625,6 +629,7 @@ private function getObject(MockType $mockClass, string $type = '', bool $callOri
/**
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws ClassIsReadonlyException
* @throws ReflectionException
* @throws RuntimeException
*/
Expand Down Expand Up @@ -684,6 +689,10 @@ private function generateMock(string $type, ?array $explicitMethods, string $moc
throw new ClassIsFinalException($_mockClassName['fullClassName']);
}

if (method_exists($class, 'isReadOnly') && $class->isReadOnly()) {
throw new ClassIsReadonlyException($_mockClassName['fullClassName']);
}

// @see https://github.com/sebastianbergmann/phpunit/issues/2995
if ($isInterface && $class->implementsInterface(Throwable::class)) {
$actualClassName = Exception::class;
Expand Down
1 change: 1 addition & 0 deletions src/Framework/MockObject/MockBuilder.php
Expand Up @@ -53,6 +53,7 @@ public function __construct(TestCase $testCase, string $type)
* @throws ClassAlreadyExistsException
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws ClassIsReadonlyException
* @throws DuplicateMethodException
* @throws InvalidMethodNameException
* @throws OriginalConstructorInvocationRequiredException
Expand Down
22 changes: 22 additions & 0 deletions tests/_files/mock-object/ReadonlyClass.php
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\MockObject;

readonly class ReadonlyClass
{
public function __construct(private mixed $value)
{
}

public function value(): mixed
{
return $this->value;
}
}
9 changes: 9 additions & 0 deletions tests/unit/Framework/MockObject/GeneratorTest.php
Expand Up @@ -32,6 +32,7 @@
use PHPUnit\TestFixture\MockObject\AnotherInterface;
use PHPUnit\TestFixture\MockObject\ClassWithVariadicArgumentMethod;
use PHPUnit\TestFixture\MockObject\FinalClass;
use PHPUnit\TestFixture\MockObject\ReadonlyClass;
use PHPUnit\TestFixture\MockObject\InterfaceWithSemiReservedMethodName;
use PHPUnit\TestFixture\SingletonClass;
use RuntimeException;
Expand Down Expand Up @@ -324,6 +325,14 @@ public function testCannotMockFinalClass(): void
$this->createMock(FinalClass::class);
}

public function testCannotMockReadonlyClass(): void
{
$this->expectException(ClassIsReadonlyException::class);

/* @noinspection ClassMockingCorrectnessInspection */
$this->createMock(ReadonlyClass::class);
}

public function testCanDoubleIntersectionOfMultipleInterfaces(): void
{
$stub = $this->generator->getMockForInterfaces(
Expand Down