Skip to content

Commit

Permalink
Fixed mocking or the Throwable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov authored and sebastianbergmann committed May 14, 2019
1 parent d7d9cee commit cc897f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
40 changes: 33 additions & 7 deletions src/Framework/MockObject/Generator.php
Expand Up @@ -747,23 +747,49 @@ private function generateMock($type, $explicitMethods, $mockClassName, $callOrig

// @see https://github.com/sebastianbergmann/phpunit/issues/2995
if ($isInterface && $class->implementsInterface(\Throwable::class)) {
$actualClassName = \Exception::class;
$additionalInterfaces[] = $class->getName();
$interfaceOwnMethods = [];
$isInterface = false;

foreach ($this->getInterfaceOwnMethods($mockClassName['fullClassName']) as $method) {
$interfaceOwnMethods[] = MockMethod::fromReflection($method, $callOriginalMethods, $cloneArguments);
try {
$class = new \ReflectionClass($actualClassName);
} catch (\ReflectionException $e) {
throw new RuntimeException(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

$mockMethods->addMethods(...$interfaceOwnMethods);
foreach ($this->getInterfaceOwnMethods($mockClassName['fullClassName']) as $method) {
$methodName = $method->getName();

if ($class->hasMethod($methodName)) {
try {
$classMethod = $class->getMethod($methodName);
} catch (\ReflectionException $e) {
throw new RuntimeException(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

if (!$this->canMockMethod($classMethod)) {
continue;
}
}

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

$mockClassName = $this->generateClassName(
\Exception::class,
$actualClassName,
'',
'Mock_'
);

$class = new ReflectionClass($mockClassName['fullClassName']);
}

// https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/Framework/MockObject/GeneratorTest.php
Expand Up @@ -202,7 +202,7 @@ public function testCanInvokeMethodsOfNonExistentClass(): void
$this->assertNull($mock->someMethod());
}

public function testMockingOfThrowable(): void
public function testMockingOfExceptionWithThrowable(): void
{
$stub = $this->generator->getMock(ExceptionWithThrowable::class);

Expand All @@ -211,6 +211,15 @@ public function testMockingOfThrowable(): void
$this->assertInstanceOf(MockObject::class, $stub);
}

public function testMockingOfThrowable(): void
{
$stub = $this->generator->getMock(Throwable::class);

$this->assertInstanceOf(Throwable::class, $stub);
$this->assertInstanceOf(Exception::class, $stub);
$this->assertInstanceOf(MockObject::class, $stub);
}

public function testVariadicArgumentsArePassedToOriginalMethod()
{
/** @var ClassWithVariadicArgumentMethod|MockObject $mock */
Expand Down

0 comments on commit cc897f1

Please sign in to comment.