Skip to content

Instantly share code, notes, and snippets.

@havvg
Created June 5, 2012 12:58
Show Gist options
  • Save havvg/2874818 to your computer and use it in GitHub Desktop.
Save havvg/2874818 to your computer and use it in GitHub Desktop.
PHPUnit mock interface extending Countable
<?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;
}
}
<?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.
@cordoval
Copy link

cordoval commented Jun 5, 2012

it would be great if you explain it into a blog post

@havvg
Copy link
Author

havvg commented Jun 5, 2012

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