Mockery: how to use shouldReceive with method_exists?

In my application code, I have a check method_existsto authorize some connections during the creation process:

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

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

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

I am trying to test this code by ridiculing $myClassand checking if it is actually called $methodName. Here is how I wrote the test:

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

But this does not work, because the exists_ method is not defined in the layout. I am new to mocking things, so there may be an obvious way to deal with this problem, for example, "drowning out" the desired function, but I could not find a way ...

Thanks in advance for your help.

+4
source share

Source: https://habr.com/ru/post/1616951/


All Articles