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

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix mocking __toString() return value
  • Loading branch information
nicolas-grekas committed Nov 9, 2015
1 parent 382c729 commit b28b029
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/Framework/MockObject/InvocationMocker.php
Expand Up @@ -100,13 +100,7 @@ public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$exception = null;
$hasReturnValue = false;

if (strtolower($invocation->methodName) == '__tostring') {
$hasReturnValue = true;
$returnValue = '';
} else {
$returnValue = null;
}
$returnValue = null;

foreach ($this->matchers as $match) {
try {
Expand All @@ -129,6 +123,8 @@ public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)

if ($hasReturnValue) {
return $returnValue;
} elseif (strtolower($invocation->methodName) == '__tostring') {
return '';
}

return $invocation->generateReturnValue();
Expand Down
8 changes: 8 additions & 0 deletions tests/MockObjectTest.php
Expand Up @@ -841,4 +841,12 @@ public function testStringableClassDoesNotThrow()

$this->assertInternalType('string', (string) $mock);
}

public function testStringableClassCanBeMocked()
{
$mock = $this->getMock('StringableClass');
$mock->method('__toString')->willReturn('foo');

$this->assertSame('foo', (string) $mock);
}
}

0 comments on commit b28b029

Please sign in to comment.