Skip to content

Commit

Permalink
We need to dynamically (and conditionally) compile these traits as th…
Browse files Browse the repository at this point in the history
…eir code is not valid syntax on all PHP versions supported by PHPUnit 8.5 and PHPUnit 9.5
  • Loading branch information
sebastianbergmann committed Mar 24, 2022
1 parent c1c9d4d commit 7a77a52
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 98 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

98 changes: 88 additions & 10 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,58 @@
*/
final class Generator
{
private const MOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT = <<<'EOT'
namespace PHPUnit\Framework\MockObject;
trait MockedCloneMethodWithVoidReturnType
{
public function __clone(): void
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler();
}
}
EOT;

private const MOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT = <<<'EOT'
namespace PHPUnit\Framework\MockObject;
trait MockedCloneMethodWithoutReturnType
{
public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler();
}
}
EOT;

private const UNMOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT = <<<'EOT'
namespace PHPUnit\Framework\MockObject;
trait UnmockedCloneMethodWithVoidReturnType
{
public function __clone(): void
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler();
parent::__clone();
}
}
EOT;

private const UNMOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT = <<<'EOT'
namespace PHPUnit\Framework\MockObject;
trait UnmockedCloneMethodWithoutReturnType
{
public function __clone()
{
$this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationHandler();
parent::__clone();
}
}
EOT;

/**
* @var array
*/
Expand Down Expand Up @@ -954,19 +1006,11 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
$cloneTrait = '';

if ($mockedCloneMethod) {
if (PHP_MAJOR_VERSION >= 8) {
$cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;';
} else {
$cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithoutReturnType;';
}
$cloneTrait = $this->mockedCloneMethod();
}

if ($unmockedCloneMethod) {
if (PHP_MAJOR_VERSION >= 8) {
$cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithVoidReturnType;';
} else {
$cloneTrait = PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithoutReturnType;';
}
$cloneTrait = $this->unmockedCloneMethod();
}

$classTemplate->setVar(
Expand Down Expand Up @@ -1107,4 +1151,38 @@ private function isConstructor(ReflectionMethod $method): bool

return $methodName === $className;
}

private function mockedCloneMethod(): string
{
if (PHP_MAJOR_VERSION >= 8) {
if (!trait_exists('\PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType')) {
eval(self::MOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT);
}

return PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;';
}

if (!trait_exists('\PHPUnit\Framework\MockObject\MockedCloneMethodWithoutReturnType')) {
eval(self::MOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT);
}

return PHP_EOL . ' use \PHPUnit\Framework\MockObject\MockedCloneMethodWithoutReturnType;';
}

private function unmockedCloneMethod(): string
{
if (PHP_MAJOR_VERSION >= 8) {
if (!trait_exists('\PHPUnit\Framework\MockObject\UnmockedCloneMethodWithVoidReturnType')) {
eval(self::UNMOCKED_CLONE_METHOD_WITH_VOID_RETURN_TYPE_TRAIT);
}

return PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithVoidReturnType;';
}

if (!trait_exists('\PHPUnit\Framework\MockObject\UnmockedCloneMethodWithoutReturnType')) {
eval(self::UNMOCKED_CLONE_METHOD_WITHOUT_RETURN_TYPE_TRAIT);
}

return PHP_EOL . ' use \PHPUnit\Framework\MockObject\UnmockedCloneMethodWithoutReturnType;';
}
}

0 comments on commit 7a77a52

Please sign in to comment.