Skip to content

Instantly share code, notes, and snippets.

@christeredvartsen
Created January 3, 2017 07:15
Show Gist options
  • Save christeredvartsen/8dccfc2d3e219d9db7d2df60269c1931 to your computer and use it in GitHub Desktop.
Save christeredvartsen/8dccfc2d3e219d9db7d2df60269c1931 to your computer and use it in GitHub Desktop.
Use a callback for return values with a mock in PHPUnit
<?php
class SomeClass { public function someMethod() {} }
class SomeClassTest extends PHPUnit_Framework_TestCase {
public function testSomeMethod() {
$someValue = ' ... ';
$mock = $this->createMock('SomeClass');
$mock
->expects($this->once())
->method('someMethod')
->will($this->returnCallback(function($arg1, $arg2) use ($someValue) {
// ...
}));
$mock->someMethod('foo', 'bar');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment