Skip to content

Commit

Permalink
Merge branch '8.4' into 8.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 4, 2019
2 parents b9e8c1f + 456ff27 commit 5e9c970
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ public function mockClassMethods(string $className, bool $callOriginalMethods, b
/**
* @return \ReflectionMethod[]
*/
private function getInterfaceOwnMethods(string $interfaceName): array
private function userDefinedInterfaceMethods(string $interfaceName): array
{
try {
$reflect = new \ReflectionClass($interfaceName);
// @codeCoverageIgnoreStart
$interface = new \ReflectionClass($interfaceName);
} catch (\ReflectionException $e) {
throw new RuntimeException(
$e->getMessage(),
Expand All @@ -524,10 +524,12 @@ private function getInterfaceOwnMethods(string $interfaceName): array

$methods = [];

foreach ($reflect->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() === $interfaceName) {
$methods[] = $method;
foreach ($interface->getMethods() as $method) {
if (!$method->isUserDefined()) {
continue;
}

$methods[] = $method;
}

return $methods;
Expand Down Expand Up @@ -637,18 +639,18 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
}
// @codeCoverageIgnoreEnd

foreach ($this->getClassMethods($_type) as $methodTrait) {
if (\in_array($methodTrait, $interfaceMethods, true)) {
foreach ($this->getClassMethods($_type) as $method) {
if (\in_array($method, $interfaceMethods, true)) {
throw new RuntimeException(
\sprintf(
'Duplicate method "%s" not allowed.',
$methodTrait
$method
)
);
}

try {
$methodReflection = $typeClass->getMethod($methodTrait);
$methodReflection = $typeClass->getMethod($method);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new RuntimeException(
Expand All @@ -664,7 +666,7 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
MockMethod::fromReflection($methodReflection, $callOriginalMethods, $cloneArguments)
);

$interfaceMethods[] = $methodTrait;
$interfaceMethods[] = $method;
}
}
}
Expand Down Expand Up @@ -736,8 +738,8 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
}
// @codeCoverageIgnoreEnd

foreach ($this->getInterfaceOwnMethods($mockClassName['fullClassName']) as $methodTrait) {
$methodName = $methodTrait->getName();
foreach ($this->userDefinedInterfaceMethods($mockClassName['fullClassName']) as $method) {
$methodName = $method->getName();

if ($class->hasMethod($methodName)) {
try {
Expand All @@ -758,13 +760,13 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
}

$mockMethods->addMethods(
MockMethod::fromReflection($methodTrait, $callOriginalMethods, $cloneArguments)
MockMethod::fromReflection($method, $callOriginalMethods, $cloneArguments)
);
}

$mockClassName = $this->generateClassName(
$actualClassName,
'',
$mockClassName['className'],
'Mock_'
);
}
Expand Down Expand Up @@ -816,7 +818,7 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
foreach ($explicitMethods as $methodName) {
if ($class !== null && $class->hasMethod($methodName)) {
try {
$methodTrait = $class->getMethod($methodName);
$method = $class->getMethod($methodName);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new RuntimeException(
Expand All @@ -827,9 +829,9 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
}
// @codeCoverageIgnoreEnd

if ($this->canMockMethod($methodTrait)) {
if ($this->canMockMethod($method)) {
$mockMethods->addMethods(
MockMethod::fromReflection($methodTrait, $callOriginalMethods, $cloneArguments)
MockMethod::fromReflection($method, $callOriginalMethods, $cloneArguments)
);
}
} else {
Expand All @@ -852,10 +854,10 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
$configurable[] = new ConfigurableMethod($mockMethod->getName(), $mockMethod->getReturnType());
}

$methodTrait = '';
$method = '';

if (!$mockMethods->hasMethod('method') && (!isset($class) || !$class->hasMethod('method'))) {
$methodTrait = \PHP_EOL . ' use \PHPUnit\Framework\MockObject\Method;';
$method = \PHP_EOL . ' use \PHPUnit\Framework\MockObject\Method;';
}

$cloneTrait = '';
Expand All @@ -880,7 +882,7 @@ private function generateMock($type, ?array $explicitMethods, string $mockClassN
'clone' => $cloneTrait,
'mock_class_name' => $mockClassName['className'],
'mocked_methods' => $mockedMethods,
'method' => $methodTrait,
'method' => $method,
]
);

Expand Down
56 changes: 56 additions & 0 deletions tests/end-to-end/mock-objects/generator/3967.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/3967
--FILE--
<?php declare(strict_types=1);
interface Bar extends \Throwable
{
public function foo(): string;
}

interface Baz extends Bar
{
}

require __DIR__ . '/../../../../vendor/autoload.php';

$generator = new \PHPUnit\Framework\MockObject\Generator;

$mock = $generator->generate(
'Baz',
[],
'MockBaz',
true,
true
);

print $mock->getClassCode();
--EXPECT--
declare(strict_types=1);

class MockBaz extends Exception implements Baz, PHPUnit\Framework\MockObject\MockObject
{
use \PHPUnit\Framework\MockObject\Api;
use \PHPUnit\Framework\MockObject\Method;

public function foo(): string
{
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
}
}

$__phpunit_result = $this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'Bar', 'foo', $__phpunit_arguments, ': string', $this, true
)
);

return $__phpunit_result;
}
}

0 comments on commit 5e9c970

Please sign in to comment.