Skip to content

Instantly share code, notes, and snippets.

@augustohp
Forked from alganet/mocks.php
Created March 27, 2012 17:50
Show Gist options
  • Save augustohp/2218342 to your computer and use it in GitHub Desktop.
Save augustohp/2218342 to your computer and use it in GitHub Desktop.
Next-gen mocks and stubs for PHP 5.4
<?php
use FooBar\Test\Mock;
// In the sample below, $mock expects the method "sayHelloTo" to be called one time
// with the parameter $name equals "Alexandre" and return "Hello Alexandre"
$mock = (new Mock('NamespaceVendor\\ClassName'))([
'sayHelloTo' => function($name="Alexandre") {
return "Hello Alexandre";
},
// Different expectations for the same method
'sayHappy' => [
function($animal='Panda') { return 'Happy Panda'; },
function($animal='Bear') { return 'Happy Bear'; }
],
// Matching number of calls on a given method
'__toString' => array_fill(0, 10, function() { return 'Happy Panda'; })
]);
<?php
use FooBar\Test\Stub;
// In the sample below, $stub acts as a stub. Pretty much self-explained.
$stub = (new Stub('NamespaceVendor\\ClassName'))([
'sayHelloTo' => function($name) {
echo "Hello $name";
}
]);
$stub->sayHelloTo("Alexandre"); //Hello Alexandre
@alganet
Copy link

alganet commented Mar 27, 2012

This looks awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment