Skip to content

Instantly share code, notes, and snippets.

@tomphp
Last active August 29, 2015 14:06
Show Gist options
  • Save tomphp/eb58b1e7734103b54ec6 to your computer and use it in GitHub Desktop.
Save tomphp/eb58b1e7734103b54ec6 to your computer and use it in GitHub Desktop.
Testing Closures
<?php
interface Item
{
public function runAction();
}
interface SomeCollection
{
public function foreachItem(callable $fn);
}
class CollectionUseSpec extends ObjectBehavior
{
function it_iterates_collection(SomeCollection $collection, Item $item)
{
$collection->foreachItem(Argument::that(
function ($fn) use ($item) {
$fn($item);
return true;
}
))->willReturn();
$this->run($collection);
$item->runAction()->shouldHaveBeenCalled();
}
}
class CollectionUse
{
public function run(SomeCollection $collection)
{
$collection->foreachItem(function (Item $item) {
$item->runAction();
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment