phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Is it possible to double get property, while set property is private?

Open Belorusov opened this issue 2 months ago • 2 comments

PhpUnit Version: 12.4.1

I got following class and want to mock it public get property hook

class Example
{
    public function __construct(
        private(set) string $property {
               get => $this->property;
        }
    ) {
    }
}

$example = $this->createMock(Example::class);
$example
    ->expects($this->once())
    ->method(PropertyHook::get('property'))
    ->willReturn('test');

Why it's not possible and gives me an error. We just want to mock public get hook, not private.[PHPUnit\Framework\MockObject\MethodCannotBeConfiguredException] Trying to configure method "property" which cannot be configured because it does not exist, has not been specified, is final, or is static

It's a bit confusing...

Belorusov avatar Oct 16 '25 15:10 Belorusov

You provided an incomplete minimal, self-contained, reproducing test case that shows the problem you are reporting.

Here is a complete reproducing test case:

<?php declare(strict_types=1);
namespace PHPUnit\TestFixture\Issue6393;

use PHPUnit\Framework\MockObject\Runtime\PropertyHook;
use PHPUnit\Framework\TestCase;

class Example
{
    public function __construct(
        private(set) string $property {
            get => $this->property;
        }
    ) {
    }
}

final class Issue6393Test extends TestCase
{
    public function testOne(): void
    {
        $example = $this->createMock(Example::class);

        $example
            ->expects($this->once())
            ->method(PropertyHook::get('property'))
            ->willReturn('test');
    }
}

sebastianbergmann avatar Oct 17 '25 05:10 sebastianbergmann

@sebastianbergmann yep, that's it!

Belorusov avatar Oct 20 '25 07:10 Belorusov