Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Fix #319 #320

Merged
merged 3 commits into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,20 +686,15 @@ public function generateClassFromWsdl($wsdlFile, $className, array $methods = []
*/
private function generateMock($type, $methods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods)
{
$templateDir = __DIR__ . DIRECTORY_SEPARATOR . 'Generator' . DIRECTORY_SEPARATOR;
$classTemplate = $this->getTemplate($templateDir . 'mocked_class.tpl');
$methodReflections = [];
$templateDir = __DIR__ . DIRECTORY_SEPARATOR . 'Generator' . DIRECTORY_SEPARATOR;
$classTemplate = $this->getTemplate($templateDir . 'mocked_class.tpl');

$additionalInterfaces = [];
$cloneTemplate = '';
$isClass = false;
$isInterface = false;

$mockClassName = $this->generateClassName(
$type,
$mockClassName,
'Mock_'
);

if (is_array($type)) {
foreach ($type as $_type) {
if (!interface_exists($_type, $callAutoload)) {
Expand All @@ -712,6 +707,12 @@ private function generateMock($type, $methods, $mockClassName, $callOriginalClon
}

$additionalInterfaces[] = $_type;
$typeClass = new ReflectionClass($this->generateClassName(
$_type,
$mockClassName,
'Mock_'
)['fullClassName']
);

foreach ($this->getClassMethods($_type) as $method) {
if (in_array($method, $methods)) {
Expand All @@ -723,11 +724,18 @@ private function generateMock($type, $methods, $mockClassName, $callOriginalClon
);
}

$methods[] = $method;
$methodReflections[$method] = $typeClass->getMethod($method);
$methods[] = $method;
}
}
}

$mockClassName = $this->generateClassName(
$type,
$mockClassName,
'Mock_'
);

if (class_exists($mockClassName['fullClassName'], $callAutoload)) {
$isClass = true;
} elseif (interface_exists($mockClassName['fullClassName'], $callAutoload)) {
Expand Down Expand Up @@ -827,12 +835,14 @@ private function generateMock($type, $methods, $mockClassName, $callOriginalClon
}
} else {
foreach ($methods as $methodName) {
$mockedMethods .= $this->generateMockedMethodDefinition(
$templateDir,
$mockClassName['fullClassName'],
$methodName,
$cloneArguments
);
if ($this->canMockMethod($methodReflections[$methodName])) {
$mockedMethods .= $this->generateMockedMethodDefinitionFromExisting(
$templateDir,
$methodReflections[$methodName],
$cloneArguments,
$callOriginalMethods
);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,16 @@ public function testExceptionIsRaisedForMutuallyExclusiveOptions()
{
$this->generator->getMock(stdClass::class, [], [], '', false, true, true, true, true);
}

/**
* @covers PHPUnit_Framework_MockObject_Generator::getMock
*/
public function testCanImplementInterfacesThatHaveMethodsWithReturnTypes()
{
if (PHP_MAJOR_VERSION < 7) {
$this->markTestSkipped('Code fails in PHP < 7.0');
}

$this->generator->getMock([AnInterfaceWithReturnType::class, AnInterface::class]);
}
}
5 changes: 5 additions & 0 deletions tests/_fixture/AnInterfaceWithReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
interface AnInterfaceWithReturnType
{
public function returnAnArray(): array;
}