phpunit
phpunit copied to clipboard
Is it possible to double get property, while set property is private?
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...
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 yep, that's it!