This episode is for members only

Sign up to access "Laravel Mentions" right now.

Get started
Already a member? Sign in to continue
Playing
06. Testing mention syncing

Transcript

00:00
Okay let's go ahead and write a couple of tests just to verify that this data does get synced and
00:04
also that we get the correct response when this gets edited as well so that's really important we'll add the ability to edit a comment a little bit later so we can see this in action. Okay so we've chosen PEST as our testing framework the first thing I'm going to do is come over to phpunit.xml and for the database connection during tests running I want to set this to SQLite and for the
00:26
database I want that to be in memory. Now with Laravel Vrees we already get a bunch of tests created some of these are going to fail because we've changed over our username stuff so you can see that when users get created via the user factory the username doesn't exist. Let's just tidy these up first and that's obviously going to mean we can run our entire test suite. So the user
00:51
factory let's go ahead and add in a username here so let's take this name copy it over and change this to username and I think we can actually use a username method here as well and let's go ahead and run our test again and see what we get we get a few failed so let's just check our profile test here and yes so we've got component has errors username let's go over to our profile test
01:20
and update this very quickly so let's look for where we are passing through a name and let's set a username in here as well username and let's say test username just run those tests again and we get one failed which is our registration test let's open this up and let's look for name and let's just add in a username in here and we should be good so let's
01:50
just say test username again and perfect all of our tests are now running so what we want to do is create out a specific test for let's just say comment mentions so let's use pest to make out a test and we'll call this comment mention test let's open up comment mention test and let's figure out what we want to check here now I've created this as a feature test rather than a unit test
02:21
it just makes more sense because we are inserting into the database and we'll just keep this kind of separate from the unit so the first thing that we want to test is it detects and stores mentioned users we'll just do all of this in one test so let's create out a user here who's going to be responsible for creating out this comment so let's say user factory and create let's go ahead
02:49
and create two users that we want to mention within this test so to do this we can destructure this so we can say first user and second user and we can use our user factory again but we can generate two users here and then we have them in here and they should both have usernames that we can then mention in our tests so next up we want to create a comment
03:18
and we want to fill in the body with these two users usernames remember that our comment observer will be triggered here which should then create these in the database so let's create out a comment here and we'll say user so the user who's creating the comment we'll use the comment relationship to create out a comment with a body in here and this body is going to be something like
03:43
mentioning at and then it will be the first user and and then at and it will be the second user so because we've got that first user and second user destructured we can just say first user username and we can say second user username in there okay let's just run this test in isolation so let's run tests feature comment mention test and yeah this works it's just risky because we
04:10
don't have any assertions so that should have been created in the database so what do we want to assert here well we want to expect that the comment mentions relationship has a count of two we basically want to make sure two records have been inserted into here you can also use things like um assert database has so you can say assert database has comments mentions whatever you wanted
04:39
to do but i prefer doing this directly through the relationship because that also tests that we have the relationship in there as well okay so our test fails here let's just take a look here we get a failed asserting the actual size zero matches expected size two now everything looks good here let's just go ahead and die dump on the string that we're inserting so i've got a feeling that
05:00
the usernames that we're inserting potentially contain characters that we don't allow so we've got things like dots in there which i don't think we allow so if we come over to our user factory this is the culprit here the username method will generate usernames with dots and stuff like that so there's a couple of options here we could either set the username manually when we're
05:21
creating the users uh which i think we'll do so let's go and set the state in here and let's create our new sequence to manually insert these and then for the user factory we can just use these to generally insert users as well okay so for a sequence when we create this out into here we can just pass in the state for each of the users that we want to create so we can manually
05:46
say well for the first user we want the username to be alex and for the second user we want the username to be tabby that now means that this string will use the two values for the state that we've just created and you can see we've got mentioning alex and tabby which is what we know will work so now when we run this test we should get green every single time great so that's a lot
06:08
more reliable so we know that the mentions now has the count of two what else can we do well we can make sure that the correct users have been mentioned how do we do this well there's a ton of ways that you can do this the cleanest way would be to pluck out from the pivot table because remember we're working with a pivot here the user id and we want to check that contains so we can say to
06:34
contain the first user's id and the second user's id so basically make sure that the pivot can pivot table here contains both of the ids of the users that we mentioned in the first place so that should work let's go ahead and run that test and there we go we've got an extra assertion there that makes sense okay so the next thing we want to do is check that it removes users who have been unmentioned
07:01
which is basically the same thing as just verifying that we're syncing this data and not inserting it every single time so what i'm going to do is i'm going to copy the test that we've already created because the setup here is going to be very similar and let's just change the name of this so let's say removes users who are unmentioned okay so we're still going to do the same thing we've got
07:24
a user who's creating the comment we've got a first user and a second user we want to create that comment as usual but then we want to update the comment down here and then make the assertion this data has been unsynced so we basically want to take the comment itself because this will remember insert them using the observer we want to update the comment body to be pretty much the
07:53
same thing but i want to remove say the second user from this and what that should do is it should only leave us with one mention and it should have just the first user's id and we can even say something like not to contain and the second user's id just to verify that this definitely has been removed okay so hopefully that makes sense create the comment as normal but then we're
08:18
going to update it like we would on a form to remove the second user and then we should only have one mention containing the first user's id and not the second user's id okay let's run both them tests and great we get green on both of them so really them two tests are just enough to know that we are properly syncing this data to that mentions table
15 episodes1 hr 52 mins

Overview

Add mentionable functionality to your Laravel applications and mention users, projects, issues… literally anything.

We’ll start by setting up a simple comment system with Livewire, then detect, sync, notify and test mentions step-by-step. Not using Livewire? Don’t worry, the core functionality works with any stack.

If you are using Alpine/Livewire, we’ll add mention support to textareas to get a list of users we’re able to mention when we hit a trigger key.

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

Episode discussion

No comments, yet. Be the first!