For learning purposes I was trying to write a unit test for one of my Magento 2 around
plugins (interception).
The method I'm testing receives as parameter a \Closure
object that cannot be mocked because \Closure
is a final class
How do I proceed in this case? Create an actual closure in my test class and pass that as parameter? Or any other way?
Let's say my method looks like this. Ignore the content, focus on the concept.
class Something
{
public function aroundDoStuff(SomeModel $subject \Closure $closure, $extraParam)
{
if ($subject->getSomeValue() == 1) {
return 'Custom return';
}
return $closure($extraParam);
}
}
Now for my test, I mocked the first parameter SomeModel
.
$subject = $this->getMock(SomeModel::class, [], [], '', false);
$subject->expects($this->any())->method('getSomeValue')->will($this->returnValue('1'));
$closure = ????; // WHAT GOES HERE
$obj = new Something();
$expected = 'Custom return';
$extraParam = 'not important';
$this->assertEquals($expected, $obj->aroundDoStuff($subject, $closure, $extraParam));