Skip to content

Commit

Permalink
Extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 17, 2024
1 parent 40aa90a commit 2e3e0a9
Showing 1 changed file with 69 additions and 58 deletions.
127 changes: 69 additions & 58 deletions src/Framework/MockObject/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,63 +876,6 @@ private function generateCodeForTestDoubleClass(string $type, bool $mockObject,

unset($traits);

$propertyHooks = '';

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

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

EOT,
$_mockClassName['className'],
$property->name(),
$property->type(),
);
}

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

EOT,
$property->type(),
$_mockClassName['className'],
$property->name(),
);
}

$propertyHooks .= <<<'EOT'
}

EOT;

}

$classTemplate->setVar(
[
'prologue' => $prologue ?? '',
Expand All @@ -947,7 +890,7 @@ private function generateCodeForTestDoubleClass(string $type, bool $mockObject,
'use_statements' => $useStatements,
'mock_class_name' => $_mockClassName['className'],
'methods' => $mockedMethods,
'property_hooks' => $propertyHooks,
'property_hooks' => $this->codeForPropertyHooks($propertiesWithHooks, $_mockClassName['className']),
],
);

Expand Down Expand Up @@ -1327,4 +1270,72 @@ 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(),
$property->name(),
);

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

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

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

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

$propertyHooks .= <<<'EOT'
}

EOT;

}

return $propertyHooks;
}
}

0 comments on commit 2e3e0a9

Please sign in to comment.