Testing video seeding0:00
But of course, we don't only need courses, we also need videos. So if we go back to our test here, we also want to make sure that we add some specific videos. So again, very similar to this test here. Let me copy this data here. First, we want to make sure that our video table is empty. Then we're going to run db:seed, and then we want to make sure that we have a specific amount of videos. And at the end, we want to make sure that we have, I think it's eight videos, which we are going to add.
And at the end, we want to make sure that we have, I think it's eight videos, which we are going to add. Of course, this will fail now. courses table matches entry of eight, but actually we have only three. And that's because here we need to check here for the video, not for the courses. Yep, like this. And now we should see that we only have zero, yeah. No videos yet given. Okay, so this means we're going to create now a new seeder, add given VideoSeeder. And also I can copy in here the creation of the video, which I already have prepared.
Creating VideoSeeder1:08
Okay, so this means we're going to create now a new seeder, add given video seeder. And also I can copy in here the creation of the Video, which I already have prepared. And here we have it again. At the beginning, we're going to check if the data is already given. Then here we are grabbing the three Courses just to make sure that we can add the videos to the correct course. The videos with the data, which I already have defined for all the different courses. All right, let's go back to our test. Let's make sure if this now works. It does not.
Let's make sure if this now works. It does not. It's still zero. And again, we have forgotten to add this to our main seeder class. So let's call this here as well, add given VideoSeeder class. Like this. Let's run a test again. And yeah, this one is now passing. Perfect. But here I want to be a little bit more specific because eight videos just means we have eight
Asserting videos per course2:06
Perfect. But here I want to be a little bit more specific because eight videos just means we have eight videos in general, but it doesn't check if we have the correct amount of videos for the specific Course. So I think it's better here to check for every Course if we have a specific amount of videos. So I'm going to get here from the Seeder, this here, we're going to grab all the different Courses. So this means we have now all the free Courses and now I want to check for all of them. So maybe let's check this like we're going to use the inspection API of Passport.
So this means we have now all the free courses and now I want to check for all of them. So maybe let's check this like we're going to use the inspection, inspection API of past. So expect, let's start with level for beginners course, and we're going to call videos and here can now say to have account of three. I think the first course has three videos from this it's working and the second one only also has three and the last one has two. This is the advanced level course and this is the TDD course. Yeah. So here we see an inspection that we can change multiple expect call into one. This will then look like this, which actually I don't like.
So here we see an inspection that we can change multiple expect calls into one. This will then look like this, which actually I don't like. That's why I mostly don't do this. So let's get back here and what I'm going to do is disable the inspection because I think this is a little better to read. And then let's run this and yes, it's working. Okay, cool. So now we have not only make sure that we have eight videos, but also that the specific course has a specific amount of videos. And a little tip here, instead of writing it like this, where we're calling the videos
course has a specific amount of videos. And a little tip here, instead of writing it like this, where we're calling the videos inside the expect method, we can also bring the video call outside. So the property videos, we can bring it outside of the expect method and use higher order expectation bypass. And let's change it here and also here and here as well, it should still pass. And yet a benefit now is here that it is a little bit better to read what we're doing because we're having a Course, we're grabbing all the videos and then we are checking the count. So if we did getting the videos inside here, this column, I think this is better to read
Ensuring idempotent seeding4:29
count. So if we did getting the videos inside here, this column, I think this is better to read and I would always prefer this one. So a little tip here. And then also here, we also want to check for the videos that they are only getting added once. So let's start by copy this in here and then we're going to run this twice and at the end we want to make sure that we still have only 8 videos like this. Let's run it and now this is already working just because inside our video seeder, I have already added here this check at the beginning to make sure that we're only going to run
Let's run it and now this is already working just because inside our video seeder, I have already added here this check at the beginning to make sure that we're only going to run this seeder for a specific state. And here down there, similar to what we have checked inside our test, we're also making sure that we have the correct amount of videos for the specific courses. Okay, good. Let's run all the tests here and they are still green. That's good. So overall, in this test, we're checking that we're adding our given courses, they can only be added once with our given videos and also they can only be added once.
Adding local test user5:31
So overall, in this test, we're checking that we're adding our given courses, they can only be added once with our given videos and also they can only be added once. And the next video, I think we're going to take a first look at the application through the browser. And for that, I already know that I'm going to need a test user inside the users table, which we can use here for testing and checking the site. So I think it's a good idea to also test this here. So let's start again by writing a test for this. It adds local test user and at the same time, we also want to make sure that we don't edit for other environments.
It adds local test User and at the same time, we also want to make sure that we don't edit for other environments. So only for the local environment, we want to add a test User. It does not add test User for production environment. So this is the most important one to check as well. So we're going to start with an insertion. Insert database count for the user table should be zero. So here I'm going to make an insertion again, at the end, it should be one. And in between, we have to act. And here we're going to call our php artisan db:seed command again for seeding our database.
And in between, we have to act. And here we're going to call our artisan command again for seeding our database. Right like this, and it should fail now because it matches not the expected one because we have zero. So this means we need to create now a new seeder and let's call it AddLocalTestUser. All right, like this. And here we're going to create now a new User with the email test@test.at for Austria. Then the name should be Christoph, my name and passport. We need to use bcrypt here and it should be test. I think this should do it.
We need to use bcrypt here and it should be test. I think this should do it. Let's give this a try. And it's local User and it's still not working. We still have zero entries and yet it's because inside our database seeder, we now also need to add a call to our new local test User. And now we run this again. It is now working perfect. And let's copy this one. And you want to make sure that after seeding, we don't see, we still don't see any User inside.
And let's copy this one. And you want to make sure that after seeding, we don't see, we still don't see any use inside the database and this should now fail because we are looking for zero User entities and we have one. Yes, exactly what we expected. So this means inside our UserSeeder, we now have to make sure that we only add the User for the local environment. So we can do this by checking the environment and want to make sure it is local. And if it is local, then we're going to add our test User right here. Now we're going to run this and now this test works.
Mocking environment in tests8:32
And if it is local, then we're going to add our testUser right here. Now we're going to run this and now this test works. So we're not adding a testUser here, but now if we run this now, this will fail here because still we have zero users. So this means for both our tests here, we don't add the user. And this is because during all of our tests, the environment is set to testing. So we need to define that it is local for a specific test. So the first idea would be to try to use the config helper. And here we can use the set method to say the app environment, we're going to set it to local, but you will see that also this is failing because at this point when we get
And here we can use the set method to say the app environment, we're going to set it to local, but you will see that also this is failing because at this point when we get the information here, level has already collected information and we would still get an environment testing. But there's a different way we can do this by calling a partial mock here on our application. We're going to say when we're going to call the environment helper method, we're going to return here. Information is not what we need, we want to make it local. And now we should add a User and yes, we do. And just to be sure, let's also try it here again.
And now we should add a User and yes, we do. And just to be sure, let's also try it here again. Now this is also working, even though we haven't defined the production environment. So I think it's even better to also copy this inside here. That's actually in the range part and we're going to make sure that we add here production so that we can be sure that for production, we also don't end up with a test User. And this now works. And here, let's also bring this up here because this now is part of our arrange part. All right, just to be sure, let's also call all the tests here from our new database seeder test and now all of those are working.
All right, just to be sure, let's also call all the tests here from our new DatabaseSeeder test and now all of those are working. Okay, perfect. So this is how we've made sure that we add data to our database, but we're only doing this if the data is not given yet. And this means we can run the php artisan db:seed, our artisan command now as often as we want to. And we're still only going to end up with the data once.
