I’m not familiar with PHPUnit and just want to execute a simple method that return a DateTimeImmutable and compare it with another DateTimeImmutable.
public function testGetLunchTimesBeginHour()
{
$minLunchTime = DateTimeImmutable::createFromFormat('H:i', self::MIN_BEGIN_LUNCH);
$maxLunchTime = DateTimeImmutable::createFromFormat('H:i', self::MAX_END_LUNCH);
foreach($this->daysOfAppointments as $dayOfAppointments){
$appointments = $this->makeAppointmentsDatetime($dayOfAppointments);
$mock = $this->getMockBuilder(GetLunchTimesBeginHour::class)->getMock();
$expected = DateTimeImmutable::createFromFormat('H:i', $dayOfAppointments['expected']);
$actualResult = $mock->expects($this->once())->method('getLunch')->with($appointments, $minLunchTime, $maxLunchTime, self::DURATION_LUNCH);
$this->assertEquals(
$expected,
$actualResult,
"unexpected result");
}
}
I understand the problem is that $actualResult is a PHPUnitFrameworkMockObjectBuilderInvocationMocker instead of a DateTimeImmutable.
How to just execute the method and get the result ? Must I call the real class instead of a mock ?
2
Answers
Well, I’m not sure what you want to test here, but here an example:
And use a foreach in a unit-test is not a good practice, it’s better to use a dataProvider
https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#writing-tests-for-phpunit-data-providers
I would not mock what you are trying to test. Try something like this, you will need to fill in the gaps and mock any dependancies though. This is assuming that the method
getLunch
returns a dateTime object.