Question

Unfortunately, PHP doesn't allow string type hinting. I want to check the below item and throw an error if it's not an instance of a certain class. Is it possible to unit test this, whether by mocking or performing this check some other way?

    if (!is_array($schedules)) {
        $schedules = array($schedules);
    }
    foreach ($schedules as $schedule) {
        if (($schedule instanceOf Schedulable) === false) {
            throw new ScheduleException('Schedule for "'.$command->getName().'" is not an instance of Schedulable');
        }

        //more stuff here
    }

The trouble I'm running into is that $schedule is a mock. and I need to test what happens in the //more stuff here section.

Currently, $schedule is an instance of Mockery::mock('Schedule');.

Was it helpful?

Solution

As we have discussed in the comments one should ensure that:

  1. The class is already defined or can be autoloaded
  2. The fully qualified name should be used
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top