Feature and Unit Testing

Feature and Unit Testing

I delved into Unit testing to target specific application components and Feature testing to provide an end user's perspective, ensuring seamless functionality across the application.

For unit testing, I navigated private methods utilizing the reflection class for effective testing.

  
public function test_booked_dates_showing_on_timeslots(): void
{
    $this->post('/schedule-a-call', [
        'name' => 'DHH',
        'schedule_call' => '2024-08-20 10:00:00',
        'timezone' => 'Europe/Copenhagen',
        'email' => '[email protected]',
        'notes' => 'Ruby on Rails'
    ]);
    $calendar = new Booker('2024', '08', '2024-08-20', 'Europe/Copenhagen');
    $reflector = new \ReflectionClass($calendar);
    $function = $reflector->getMethod('bookedTimeslots');
    $this->assertContains('10:00:00', $function->invoke($calendar));
}
  

Showcased in a feature test, I simulated a user booking an appointment, asserting invalid and making sure error message are provided.

  
public function test_fail_with_message_if_dates_are_already_booked(): void
{
    Booking::factory()->create([
        'schedule_call' => '2024-08-27 12:00:00',
    ]);

    $response = $this->post('/schedule-a-call', [
        'name' => 'Primagen',
        'schedule_call' => '2024-08-27 12:00:00',
        'timezone' => 'America/Los_Angeles',
        'email' => '[email protected]',
        'notes' => 'Go and HTMX'
    ]);
    $response->assertInvalid(['schedule_call']);
    $response->assertSessionHasErrors([
        'schedule_call' => 'Something went wrong. Please go back and select a date again.'
    ]);
}
  

Crucial database testing was carried out, enabling real-world scenarios emulation through meticulous setup of database connections in both the config/database.php and .env files.

Unit testing in PHP, powered by frameworks like PHPUnit, was pivotal in maintaining code quality and reliability by pinpointing bugs early on and upholding a top-notch coding standard throughout the development lifecycle.


Do you need help with your PHP project?

Schedule a Call