-
-
Save augustohp/2218342 to your computer and use it in GitHub Desktop.
Next-gen mocks and stubs for PHP 5.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; }) | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks awesome.