dimanche 22 novembre 2015

Mockery: how to use shouldReceive with method_exists?

In my application code, I've got a method_exists check to authorize some hooking in a create process:

// Note: $myClass is implementing a ListItemFactory interface.

if ($isCreate) {
  $methodName = "create{$attr}ListItem";

  if (method_exists($myClass, $methodName)) {
    $item = $myClass->$methodName();
  } else {
    [...]
  }
}

I'm trying to test this code, mocking $myClass and checking if $methodName is in fact called. Here's how I wrote the test:

/** @test */
function specific_create_method_is_called()
{
  $factory = Mockery::mock(ListItemFactory::class)->makePartial();
  $factory->shouldReceive("createCommentsListItem")->once();
  [...]
}

But this isn't working, because method_exists is not defined in the mock. I'm fairly new to mock stuff, so maybe there's an obvious way to manage this problem, like "stubbing" the wanted function, but I wasn't able to find the way...

Thanks in advance for any help.

Aucun commentaire:

Enregistrer un commentaire