Skip to content

Instantly share code, notes, and snippets.

@alganet
Created March 27, 2012 17:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alganet/2218151 to your computer and use it in GitHub Desktop.
Save alganet/2218151 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";
}
]);
<?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
<?php
use FooBar\Test\Stub;
// In the sample below, $stub acts as a stub. Pretty much self-explained.
$stub = Stub::for('NamespaceVendor\\ClassName')
->sayHelloTo(function($name) {
echo "Hello $name";
});
$stub->sayHelloTo("Alexandre"); //Hello Alexandre
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment