skip to Main Content

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


  1. Well, I’m not sure what you want to test here, but here an example:

        $expectedDate = new DateTimeImmutable('2022-11-01');
        $now = new DateTimeImmutable('2022-10-01');
    
        $myClass = new MyClass();
        $newDate = $myClass->calcul($now);
    
        // ± 5 seconds are equals
        self::assertEqualsWithDelta($expectedDate->getTimestamp(), $newDate->getTimestamp(), 5);
    

    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

    Login or Signup to reply.
  2. 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.

    public function testGetLunchTimesBeginHour()
    {
        $minLunchTime = DateTimeImmutable::createFromFormat('H:i', self::MIN_BEGIN_LUNCH);
        $maxLunchTime = DateTimeImmutable::createFromFormat('H:i', self::MAX_END_LUNCH);
    
    
        //Class you are testing, you will need to mock the class dependancies and pass them in.
        $testObject = new GetLunchTimesBeginHour();
    
        foreach($this->daysOfAppointments as $dayOfAppointments){
            $appointments = $this->makeAppointmentsDatetime($dayOfAppointments);
            $mock = $this->getMockBuilder(GetLunchTimesBeginHour::class)->getMock();
            $expected = DateTimeImmutable::createFromFormat('H:i', $dayOfAppointments['expected']);
         
            //get real response from created object you are testing.
            $actualResult = $testObject->getLunch($appointments, $minLunchTime, $maxLunchTime, self::DURATION_LUNCH);
    
            $this->assertEquals(
                $expected,
                $actualResult,
                "unexpected result"
            );
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search