Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
Closed
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
34 changes: 31 additions & 3 deletions src/Stub/ReturnValueMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
continue;
}

$return = array_pop($map);
if ($invocation->parameters === $map) {
return $return;
$returnValue = $this->getReturnValue(array_pop($map));
if ($this->compare($invocation->parameters, $map)) {
return $returnValue->invoke($invocation);
}
}

Expand All @@ -42,4 +42,32 @@ public function toString()
{
return 'return value from a map';
}

/**
* @param array $actual
* @param array $expected
* @return bool
*/
protected function compare($actual, $expected) {
foreach ($expected as $index => $value) {
if ($value instanceof PHPUnit_Framework_Constraint) {
if ($value->evaluate($actual[$index], '', true) === false) {
return false;
}
} else {
if ($value !== $actual[$index]) {
return false;
}
}
}

return true;
}

protected function getReturnValue($value) {
if (!$value instanceOf PHPUnit_Framework_MockObject_Stub) {
return new PHPUnit_Framework_MockObject_Stub_Return($value);
}
return $value;
}
}
92 changes: 92 additions & 0 deletions tests/MockObject/Stub/ReturnValueMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\ExpectationFailedException;

class ReturnValueMapTest extends TestCase
{

/**
* @test
**/
public function returns_the_first_match_found()
{
$map = [
['a', 'b', 'c', 'd'],
['a', 'b', 'c', 'e'],
['e', 'f', 'g', 'h']
];

$mock = $this->getMockBuilder(AnInterface::class)
->getMock();

$mock->expects($this->any())
->method('doSomething')
->will($this->returnValueMap($map));

$this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
}

/**
* @test
**/
public function accepts_framework_matchers() {
$map = [
[$this->lessThan(2), 1],
[$this->greaterThanOrEqual(2), 2]
];

$mock = $this->getMockBuilder(AnInterface::class)
->getMock();

$mock->expects($this->any())
->method('doSomething')
->will($this->returnValueMap($map));

$this->assertEquals(1, $mock->doSomething(0));
$this->assertEquals(2, $mock->doSomething(100));
}

/**
* @test
**/
public function accepts_stub_for_return_value() {
$callback = new PHPUnit_Framework_MockObject_Stub_ReturnCallback(
function($arg) { return $arg + 1; }
);

$map = [
[$this->lessThan(2), 1],
[$this->greaterThanOrEqual(2), $callback]
];

$mock = $this->getMockBuilder(AnInterface::class)
->getMock();

$mock->expects($this->any())
->method('doSomething')
->will($this->returnValueMap($map));


$this->assertEquals(3, $mock->doSomething(2));
}

/**
* @test
**/
public function returns_null_if_no_match_found()
{
$map = [
['a', 'b', 'c', 'd'],
];

$mock = $this->getMockBuilder(AnInterface::class)
->getMock();

$mock->expects($this->any())
->method('doSomething')
->will($this->returnValueMap($map));

$this->assertEquals(null, $mock->doSomething('foo', 'bar'));
}
}
30 changes: 0 additions & 30 deletions tests/MockObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,36 +212,6 @@ public function testStubbedReturnValue()
$this->assertEquals('something', $mock->doSomething());
}

public function testStubbedReturnValueMap()
{
$map = [
['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h']
];

$mock = $this->getMockBuilder(AnInterface::class)
->getMock();

$mock->expects($this->any())
->method('doSomething')
->will($this->returnValueMap($map));

$this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
$this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
$this->assertEquals(null, $mock->doSomething('foo', 'bar'));

$mock = $this->getMockBuilder(AnInterface::class)
->getMock();

$mock->expects($this->any())
->method('doSomething')
->willReturnMap($map);

$this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
$this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
$this->assertEquals(null, $mock->doSomething('foo', 'bar'));
}

public function testStubbedReturnArgument()
{
$mock = $this->getMockBuilder(AnInterface::class)
Expand Down