In this episode, we're diving into writing tests for the schedule availability functionality we built previously. The focus is on making sure our Laravel application's logic for checking when employees are available is rock-solid and ready to handle different real-world scheduling scenarios.
Here's what we do step by step:
- First, we review the core tests we want to build—like making sure the system lists employee availability correctly, accounts for unique daily schedules (e.g., not working weekends), handles exclusions (such as days off and holidays), and only shows slots that are available from the current time plus one hour onward.
- We then get hands-on: generating a new Pest test, setting up our test environment to ensure database migrations run properly, and then starting to write detailed tests, each time checking assertions around start and end times for different periods.
- We pay special attention to creating scenarios where employees might have different working hours on different days, scheduled time off (like lunch breaks or full days off), and making sure our code properly excludes or splits availability based on these exceptions.
- Throughout, we use Laravel's
Carbon::setTestNow
to freeze the date and time inside tests so they're reliable and predictable.
- We test traversing through periods—like checking today, then jumping to the next period (e.g., tomorrow)—and show how to assert that the system reacts as expected when a whole day is excluded.
- At the end, we check that future appointments only show up if they're far enough ahead of the current time (e.g., at least an hour later).
Overall, this episode is all about translating our scheduling logic into robust automated tests—it might be a bit verbose, but it's super practical and leaves us confident that our scheduling system behaves exactly how we want it to, even as things get complicated!