This episode is for members only

Sign up to access "Build an Appointment Booking System With Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
19. Writing tests for service availablity

Episodes

0%
Your progress
  • Total: 4h 49m
  • Played: 0m
  • Remaining: 4h 49m
Join or sign in to track your progress

Transcript

00:00
Okay so now we've finished up our service slot generator, let's go ahead and just retrospectively go back and write some tests to cover this at a really high level, so as you can see I've written out some of the things that we should be testing for and this pretty much covers everything that we need but again if you wanted to go in and add some more or if you change anything
00:20
then go ahead and add, okay so let's go ahead and just create out another test here so let's say and we're gonna create our what do we call this service slot availability and of course we'll call that test, we're going to put that in the feature folder again because we're hitting the database but it doesn't really matter too much, okay so let's go and just open that up so service
00:41
slot availability test and let's copy over all of our tests that we want to write and just put these at the top here, great, so the first one shows available time slots for a service so this is just going to be a really basic test that we'll go ahead and grab between two dates and just make sure them slots are in there that we're returning, that's pretty much what we want to do, so once
01:05
again we're going to go ahead and use carbon and we're going to set the test now date that's again really really important because we want to control this and not have our tests dependent on the current date and time, so we're just going to go ahead and parse a date in here it could be absolutely anything, so again we'll just pick the 1st of January 2000 to keep things simple,
01:26
so we can actually take from our other tests some of the setup stuff that we've done, so we need an employee and we need a service and we're pretty much going to need an employee and service for every one of these tests, we will also want to test multiple employees as well at some point but let's go ahead and just pull in the very basic setup we have here with our employee in our
01:46
service and then we can start to new this class up and run it, so let's change this actually just to make it a little bit simpler because we don't want too much data rolling in here we just want to know that we're getting these slots back, so let's say end of day, so we basically just want to check availability from the start to the end of the day with this particular service and we can
02:05
bump the duration up if we want to but let's see how we go, so we'll go ahead and create out an availability variable in here that's going to be our new service slot availability class, of course we expect an employee to be passed into here but this is a collection of employees, so we could change that but let's go ahead and just new up a collection and then pop an array in there with
02:28
that employee and that kind of makes it a little bit easier to do, so let's go ahead and pass the service in and then let's immediately go ahead and invoke this for a particular period and the period that we're going to choose is just the start of the current day and the end of the current day since that's all we have availability for as we've set everything up here, okay so now we want to go
02:50
ahead and obviously expect that we see some kind of slots in here, so this is going to be the most easiest to write but then later on we're going to need to start digging into the data that we're testing to make sure, now there are helpers in PEST that allow us to write really elegant tests, these are going to go in the slight opposite direction because we're going to have to really dig into the
03:10
data that we've got, there's no real way around that apart from creating our own helpers in our application but we are testing and that's the main thing, okay so let's go ahead and create out an expectation and we're going to say availability and first, so we're going to grab the first slot from this, let's just go ahead and die dump that just so we know that all of our setup and
03:31
everything is working, so let's find that, go ahead and run PEST and yeah sure enough we should have in here a date object with that detail, them details in, yeah so we've got a date here and then obviously our items in here contain our employees and all of our slot data, so that's coming through nicely, okay so we want to grab the first one, we want to grab the date and we'll cast it to a date
03:55
string, so remember this is a carbon object that we're storing within here, so let's cast it to a date string and then let's make sure it equals the current date that we are looking at, so let's just pull that into there, that's what we need and we'll say to equal and now to date string and let's go ahead and run PEST and yeah we get green, so basically we want to make sure that
04:21
the slot that's coming back for the day that we've requested is that slot, so the date actually works, now we could go a little bit further than this, so what we could do is create another expectation here and we could count the slots, we could even then look into the first slot and check that it gives us a slot back, but we're going to do that a little bit later,
04:41
so let's go ahead and just die dump here on availability and let's say first because that's going to be the first date that we've requested and let's say slots just to make sure that we can get them out, great and let's count on them here, so obviously while we're testing this it's really difficult because we don't really know how many slots are available, we'd have to just work
05:01
it out anyway, the easiest way to do this is just die dump on account and you can see we get 16, so we can just for a 30 minute appointment with the availability that this employee has, just assume that we should always get that amount of slots back, it's not entirely necessary but it's going to help, so we'll just say let's change this to an expectation and we don't need count
05:22
here because we can just pass this in and say expect this to have count of 16 and let's go ahead and run pest and there we go we get green gray, so I'm pretty happy with that, we know that we're getting the date back, we will look at testing multiple dates over several days as well but that's a good starting point, okay so that one's done, so list multiple slots over more than one
05:43
day, that's what we're going to cover next, so again let's grab the test that we've already written because most of the setup here is what we would expect and let's grab the name of the test that we want to write and let's go ahead and replace that out in here, again keeping the same date but this time we want to adjust the employee's schedule, so we'll just say end of year, it doesn't
06:02
matter how many we add in here because we can limit the amount we look at in our slot generator, let's go ahead and keep our service the same and let's do adding on a day, so we'll say add day here end of day, so we're looking over two days now and we basically want to make sure in here that we've got two dates and they both have slots, so let's go and add in our expectation
06:27
and just before we do that let's just die dump on our availability just to remind ourselves what the structure of this is, so if we look at this we've got well we've got a huge amount of data in here at the moment but basically this is just going to be our date collection with all of that data inside of there, so what we could actually do is map through this, extract the dates out that are
06:51
within there rather than going and looking at each individual object, so we could get an array back of these dates and then we could just make sure that that array contains the two dates that we're requesting which are the two dates here today and tomorrow, so let's go ahead and just start to add our expectation in and we'll map through, so let's go ahead and add our map back in there and let's
07:11
do an inline closure and we're going to say we want to map through these dates we won't bother type hinting it here and we'll say date date remember our date object will contain the date just inside of here and we'll say to date string so what that will do is it will map through that and it will return just a string so we'll basically end up with an array or a collection of of them
07:35
things so we're going to say to contain and we want to pass in today's date strings remember we're going through a mapping and grabbing out the date string for each one and we will add a day and we'll say to date string so basically fetch all the date strings out in an array or a collection and then make sure it contains today and tomorrow so let's go ahead and run pest and yeah there we
08:02
go we get green great so we can do a similar thing here to what we did up here where we have expect this to have a count of 16 and let's go ahead and do that down here and we can do exactly the same thing here and that should work but then we can go ahead and say get instead and we can get this index one I think that's going to work let's try index two just in case this isn't zero indexed
08:25
and yeah that should be good so if we change that to index one we get that great so this is just a shortcut to grab a item at a particular index inside of a larifel collection so we know that the first day has 16 and the next day has 16 okay so I think that's okay for now so let's go and just get rid of this one get rid of this one and we'll go ahead and write our next test these are
08:48
just going to gradually get a little bit more interesting so this one's done so it excludes booked appointments for the employee that's really important we don't really need to test any kind of availability stuff here like when the day cuts off in terms of the slots but we do remember need to include appointments because we included appointments exclusions inside of here so let's
09:06
go ahead and change this over to that and of course for our employee now we need to add in an appointment so what we're going to need to do is set the schedule which we can keep as end of day because we just need to test one day here but we want to have add-on that this has an appointment so let's go and create out an appointment factory instance and this needs to be for a service so we open up
09:33
our appointment factory we didn't include much in here but remember our appointment belongs to a service so we can do this at the same time as adding this into our employees appointments so we are saying that the employee has an appointment for that particular service and then we can go ahead and set the state for that appointment now obviously the service is down here so we're going
09:57
to need to bring that up so that's available before we actually create the employee okay so let's add in the state here that we need we'll just add these starts at and ends at date because we filled everything else out in our factory so let's say now and we'll just say set time from time string so we can be really precise here and let's say that this starts at 12 and let's say that this
10:20
ends at 12 45 we'll do a slightly weird one just to make sure that this works nicely okay so we will check our availability for just today we're just checking today for this and let's get rid of these expectations here and figure out how we're going to test that this is working so what we want to do is basically just get out a list of slots in an array and the times we're just looking
10:45
at one day here so we just want a list of all of the times that are available for that day so let's just play around with this so let's go and create a slots variable obviously we can access the availability we need to grab the first one so that's pretty much all we need to do because we're just working with one day here and that makes things a lot simpler and then we want to go ahead
11:05
and map through that first one and then just return the slots so let's create out a long closure here and we'll get a date in for each of these and let's go ahead and type in this just so we get our editor and then from that date what we can do while we're mapping through that is we can go and grab the slot and map through them and then format them so we'll go over this in just a second
11:32
let's go ahead and bring that slot into here and let's return the slots time to a time string to time string and we should be good okay let's just die dump on these slots and if we've done anything wrong we can fix that up and yeah so let's have a look and map yeah so okay well I think we can just map through that normally and grab out the first date yeah we can do that and
11:57
then we can just flatten it at the end so let's go ahead and run pest and yeah there we go sure enough we get all of them items now these are within a collection within a collection because we've got our custom date collection but what we can do with these is just flatten them so we can use this really handy flatten method and if we just go ahead and run that again you can see sure
12:17
enough this just pulls it straight down to a date collection now a date collection is just like a normal collection so we can cast this straight to an array just to make it a little bit easier to test and finally we end up with this so basically map through all of the dates which is just one and then for each of the slots that are within dates return them as part of the map so we get a
12:37
collection of them timed okay so now that we have got them slots we can go ahead and very easily test them with just a couple of expectations so let's say expect slots to contain so we're just working with a standard array here we want this to contain let's just have a look at this we know that this works because we've tested it we've got an appointment at 11 30 with a 30 minute
13:00
appointment or service time so that will run up to 12 the appointments booked in at 12 to 12 45 and the next one is then therefore available at one o'clock so we'll just test around this just to make sure that all of these times are in there so we'll check that this does contain 11 30 we'll expect it not to contain 12 so we can just chain on not here with pest which makes it really
13:23
easy to write these and let's just make sure we format these properly okay so let's just start with m2 and run pest great that's working and we want it not to contain 12 30 and then what we can do is finally chain on that we want it to contain and we're just kind of doing these in order one o'clock so we're just testing that all of these appointments are available around what we have
13:50
already booked okay let's go ahead and run pest and yeah sure enough we already saw that data but we know that that is working nicely great so that is working and we now want to look at ignoring cancelled appointments very very important just to remind you if we go over to our slot availability and we search for cancelled app remember we when we looked through appointments we made sure
14:12
that the cancelled app was null if if cancelled app is cancelled then we can include it in what we're doing okay let's go ahead and just again pull any of these tests down because the setup here is going to be more or less the same this one's going to be a little bit longer because we're going to need to create that cancelled appointment but that's absolutely fine okay let's
14:31
go and just bring back that ignored cancelled appointments let's put this down here and let's grab this and let's change this over okay so this needs to check cancelled appointment okay so let's go and just think about this so we've got an employee with a schedule we've got an appointment here and we're going to say cancelled at now it doesn't matter when the cancelled update is as
15:01
long as it's not null so that's why we just can put now in there so this is pretty much exactly the same as the test we wrote before we've got an appointment at 12 to 12 45 but it is cancelled so basically with this with these slots that we get back we should expect to see 11 30 which we can leave in there if we want to but we should expect to see 12 we should expect to see 12 30 and we
15:26
should expect to see one o'clock so we're just now doing the kind of opposite thing where we're looking around them appointment times to make sure that because that is cancelled it's still available and sure enough you can see it works now again just to demonstrate this if we were to get rid of this where null it is of course going to include that and there we go we get a fail so that's
15:45
really really important tiny thing added in there to make sure that we calculate this availability properly okay so the last thing to do is that it shows multiple employees available for a service really really important we did focus on that a lot pushing the employees that can do this into the array so let's go ahead and just grab any of these tests and pop this down here and let's say
16:07
shows multiple employees available for a service now with this the employee stuff we're just going to completely get rid of because we're going to need to generate multiple so let's get rid of all of our expectations in here we'll leave this in here in in here what we want to do is change this to have a count of two we could create out two separate employees with different availability
16:33
but as long as we know that they're both going in there we know that the availability checks are working properly so we're pretty confident that the availability checking per employee is working nicely okay so we've got a count of two employees at the start at the start of the day finish at the end of the day and we could add in that they've both got appointments i think that works nicely as
16:52
well and we can check that that ignores both of them and then we just want to do our standard availability stuff okay so let's go and just create a really simple first expectation let's grab the first available date which is the only available day grab the slots grab the first slot and then grab the employees out of that slot and expect them to have a count of two so let's go
17:21
ahead and run pest and we get a fail okay so let's just take a look here uh must be a type employee ah okay so what we did is we created multiple employees here but we didn't pass them directly into here as a collection so let's go ahead and say employees change that to employees and we should be good let's just rerun this and yeah sure enough we can see that both of these employees are
17:45
getting pushed into the first available slot for that day i think because we've already tested availability i'm going to get rid of this appointment here because we already know that it does cut out the dates for the appointment so let's go ahead and just run pest again and yeah that's working nicely okay so we've created some really high level overview tests of this class
18:05
which pretty much cover everything that we need to cover in here all of the main important bits we haven't broken this out to test any of the smaller classes because all of these are already being used the slot generator is being used by this so think of this as a feature test where we're just testing i want to get back this information and behind the scenes it's obviously
18:25
using lots of them smaller classes that we've already created there is an opportunity to add more tests of course on our date collection if we wanted to but i think for now we are pretty much good with making sure that this is solid if we make any changes or tweaks to it we're pretty covered
37 episodes4 hrs 49 mins

Overview

Building an availability calendar and booking system is a notoriously difficult problem to solve. That’s exactly what we’re going to cover in this course.

Step by step, we’ll build an appointment slot generator that calculates availability based on employee schedules, employee’s booked time off, the length of service chosen, existing appointments, and cancelled appointments.

For maximum flexibility, we’ll also allow multi-employee availability checks, so we’ll be able to see every employee who can perform a service (and their available slots).

To finish up, we’ll build a simple UI with Alpine.js, with a beautiful booking calendar that shows detailed availability across multiple dates, the ability to choose a time slot — and finally the ability to book an appointment.

Phew. We’ve got a lot to learn — let’s build a booking system with Laravel!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!