Permalink
Browse files

Fixes #103

 * Added the ability to mock interfaces that extend `Traversable`, but not `Iterator` or `IteratorAggregate`.
  • Loading branch information...
1 parent 9db32e6 commit 96a6794eb592d8eb462d0bb3f832066b4ee017d2 @whatthejeff whatthejeff committed Feb 5, 2013
@@ -589,6 +589,8 @@ protected function generateMock($originalClassName, $methods, $mockClassName, $c
$classTemplate = new Text_Template(
$templateDir . 'mocked_class.tpl'
);
+
+ $additionalInterfaces = array();
$cloneTemplate = '';
$isClass = FALSE;
$isInterface = FALSE;
@@ -669,6 +671,15 @@ protected function generateMock($originalClassName, $methods, $mockClassName, $c
$mockedMethods = '';
if (isset($class)) {
+
+ // https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
+ if ($isInterface && $class->implementsInterface('Traversable') &&
+ !$class->implementsInterface('Iterator') &&
+ !$class->implementsInterface('IteratorAggregate')) {
+ $additionalInterfaces[] = 'Iterator';
+ $methods = array_merge($methods, get_class_methods('Iterator'));
+ }
+
foreach ($methods as $methodName) {
try {
$method = $class->getMethod($methodName);
@@ -699,7 +710,9 @@ protected function generateMock($originalClassName, $methods, $mockClassName, $c
'prologue' => isset($prologue) ? $prologue : '',
'epilogue' => isset($epilogue) ? $epilogue : '',
'class_declaration' => $this->generateMockClassDeclaration(
- $mockClassName, $isInterface
+ $mockClassName,
+ $isInterface,
+ $additionalInterfaces
),
'clone' => $cloneTemplate,
'mock_class_name' => $mockClassName['className'],
@@ -755,25 +768,31 @@ protected function generateClassName($originalClassName, $className, $prefix)
/**
* @param array $mockClassName
* @param boolean $isInterface
+ * @param array $additionalInterfaces
* @return array
*/
- protected function generateMockClassDeclaration(array $mockClassName, $isInterface)
+ protected function generateMockClassDeclaration(array $mockClassName, $isInterface, array $additionalInterfaces = array())
{
$buffer = 'class ';
+ $additionalInterfaces[] = 'PHPUnit_Framework_MockObject_MockObject';
+ $interfaces = implode(', ', $additionalInterfaces);
+
if ($isInterface) {
$buffer .= sprintf(
- "%s implements PHPUnit_Framework_MockObject_MockObject, %s%s",
+ "%s implements %s, %s%s",
$mockClassName['className'],
+ $interfaces,
!empty($mockClassName['namespaceName']) ? $mockClassName['namespaceName'] . '\\' : '',
$mockClassName['originalClassName']
);
} else {
$buffer .= sprintf(
- "%s extends %s%s implements PHPUnit_Framework_MockObject_MockObject",
+ "%s extends %s%s implements %s",
$mockClassName['className'],
!empty($mockClassName['namespaceName']) ? $mockClassName['namespaceName'] . '\\' : '',
- $mockClassName['originalClassName']
+ $mockClassName['originalClassName'],
+ $interfaces
);
}
@@ -53,6 +53,7 @@
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'PartialMockTestClass.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'SomeClass.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'StaticMockTestClass.php';
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TraversableMockTestInterface.php';
/**
*
@@ -322,6 +323,12 @@ public function testGetMockForAbstractClass()
->method('doSomething');
}
+ public function testGetMockForTraversableInterface()
+ {
+ $mock = $this->getMock('TraversableMockTestInterface');
+ $this->assertInstanceOf('Traversable', $mock);
+ }
+
public function testGetMockForTrait()
{
$mock = $this->getMockForTrait('AbstractTrait');
@@ -0,0 +1,4 @@
+<?php
+interface TraversableMockTestInterface extends Traversable
+{
+}

0 comments on commit 96a6794

Please sign in to comment.