Created
June 5, 2012 12:58
-
-
Save havvg/2874818 to your computer and use it in GitHub Desktop.
PHPUnit mock interface extending Countable
This file contains hidden or 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 | |
| namespace Ormigo\Tests; | |
| abstract class AbstractTest extends \PHPUnit_Framework_TestCase | |
| { | |
| /** | |
| * Apply expectations for a \Countable on a mock object. | |
| * | |
| * @see http://php.net/Countable | |
| * | |
| * @param object $mock A mock object to apply expectations to. | |
| * @param int $count The count to return. | |
| * @param int $counter The method invocation start counter. | |
| * This parameter should be altered when using additional | |
| * expectations before the actual count. | |
| * | |
| * @return int The next method invocation counter to add further expectations. | |
| */ | |
| public function expectCountable($mock, $count, $counter = 0) | |
| { | |
| $mock | |
| ->expects($this->at($counter)) | |
| ->method('count') | |
| ->will($this->returnValue($count)) | |
| ; | |
| return ++$counter; | |
| } | |
| } |
This file contains hidden or 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 | |
| // The usage is fairly simple. | |
| $vouchers = $this->getVoucherCollectionMock(); | |
| $this->expectCountable($vouchers, 0); | |
| // see https://gist.github.com/2852498#file_example_test.php for explanations on the $counter parameter. |
Yeah, I'm writing on it for both expectations. Should be up by tomorrow evening.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it would be great if you explain it into a blog post