Skip to content

Commit

Permalink
Merge branch '11.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 13, 2024
2 parents f54b2a1 + 85b07dc commit 33d69e7
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 80 deletions.
79 changes: 7 additions & 72 deletions src/Framework/MockObject/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,10 @@ private function generateCodeForTestDoubleClass(string $type, bool $mockObject,
'use_statements' => $useStatements,
'mock_class_name' => $_mockClassName['className'],
'methods' => $mockedMethods,
'property_hooks' => $this->codeForPropertyHooks($propertiesWithHooks, $_mockClassName['className']),
'property_hooks' => (new HookedPropertyGenerator)->generate(
$_mockClassName['className'],
$propertiesWithHooks,
),
],
);

Expand Down Expand Up @@ -759,7 +762,7 @@ private function interfaceMethods(string $interfaceName): array
}

/**
* @param list<Property> $propertiesWithHooks
* @param list<HookedProperty> $propertiesWithHooks
*
* @return list<ConfigurableMethod>
*/
Expand Down Expand Up @@ -808,7 +811,7 @@ private function configurableMethods(DoubledMethodSet $methods, array $propertie
/**
* @param ?ReflectionClass<object> $class
*
* @return list<Property>
* @return list<HookedProperty>
*/
private function properties(?ReflectionClass $class): array
{
Expand Down Expand Up @@ -861,7 +864,7 @@ private function properties(?ReflectionClass $class): array
continue;
}

$properties[] = new Property(
$properties[] = new HookedProperty(
$property->getName(),
$mapper->fromPropertyType($property),
$hasGetHook,
Expand All @@ -871,72 +874,4 @@ private function properties(?ReflectionClass $class): array

return $properties;
}

/**
* @param list<Property> $propertiesWithHooks
* @param class-string $className
*
* @return non-empty-string
*/
private function codeForPropertyHooks(array $propertiesWithHooks, string $className): string
{
$propertyHooks = '';

foreach ($propertiesWithHooks as $property) {
$propertyHooks .= sprintf(
<<<'EOT'
public %s $%s {
EOT,
$property->type()->asString(),
$property->name(),
);

if ($property->hasGetHook()) {
$propertyHooks .= sprintf(
<<<'EOT'
get {
return $this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'%s', '$%s::get', [], '%s', $this
)
);
}

EOT,
$className,
$property->name(),
$property->type()->asString(),
);
}

if ($property->hasSetHook()) {
$propertyHooks .= sprintf(
<<<'EOT'
set (%s $value) {
$this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'%s', '$%s::set', [$value], 'void', $this
)
);
}

EOT,
$property->type()->asString(),
$className,
$property->name(),
);
}

$propertyHooks .= <<<'EOT'
}

EOT;

}

return $propertyHooks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class Property
final readonly class HookedProperty
{
/**
* @var non-empty-string
Expand Down
88 changes: 88 additions & 0 deletions src/Framework/MockObject/Generator/HookedPropertyGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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\Generator;

use function sprintf;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class HookedPropertyGenerator
{
/**
* @param class-string $className
* @param list<HookedProperty> $properties
*
* @return non-empty-string
*/
public function generate(string $className, array $properties): string
{
$code = '';

foreach ($properties as $property) {
$code .= sprintf(
<<<'EOT'
public %s $%s {
EOT,
$property->type()->asString(),
$property->name(),
);

if ($property->hasGetHook()) {
$code .= sprintf(
<<<'EOT'
get {
return $this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'%s', '$%s::get', [], '%s', $this
)
);
}

EOT,
$className,
$property->name(),
$property->type()->asString(),
);
}

if ($property->hasSetHook()) {
$code .= sprintf(
<<<'EOT'
set (%s $value) {
$this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'%s', '$%s::set', [$value], 'void', $this
)
);
}

EOT,
$property->type()->asString(),
$className,
$property->name(),
);
}

$code .= <<<'EOT'
}

EOT;

}

return $code;
}
}
14 changes: 7 additions & 7 deletions tests/unit/Framework/MockObject/Generator/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Type\Type;

#[CoversClass(Property::class)]
#[CoversClass(HookedProperty::class)]
#[Group('test-doubles')]
#[Small]
final class PropertyTest extends TestCase
Expand All @@ -24,7 +24,7 @@ public function testHasName(): void
{
$name = 'property-name';

$property = new Property($name, Type::fromName('string', false), false, false);
$property = new HookedProperty($name, Type::fromName('string', false), false, false);

$this->assertSame($name, $property->name());
}
Expand All @@ -33,35 +33,35 @@ public function testHasType(): void
{
$type = Type::fromName('string', false);

$property = new Property('property-name', $type, false, false);
$property = new HookedProperty('property-name', $type, false, false);

$this->assertSame($type, $property->type());
}

public function testMayHaveGetHook(): void
{
$property = new Property('property-name', Type::fromName('string', false), true, false);
$property = new HookedProperty('property-name', Type::fromName('string', false), true, false);

$this->assertTrue($property->hasGetHook());
}

public function testMayNotHaveGetHook(): void
{
$property = new Property('property-name', Type::fromName('string', false), false, false);
$property = new HookedProperty('property-name', Type::fromName('string', false), false, false);

$this->assertFalse($property->hasGetHook());
}

public function testMayHaveSetHook(): void
{
$property = new Property('property-name', Type::fromName('string', false), false, true);
$property = new HookedProperty('property-name', Type::fromName('string', false), false, true);

$this->assertTrue($property->hasSetHook());
}

public function testMayNotHaveSetHook(): void
{
$property = new Property('property-name', Type::fromName('string', false), false, false);
$property = new HookedProperty('property-name', Type::fromName('string', false), false, false);

$this->assertFalse($property->hasSetHook());
}
Expand Down

0 comments on commit 33d69e7

Please sign in to comment.