Configure PHPUnit Database0:00
Let's go ahead and write some tests for the features that we have so far. So I want to make sure that I have decent test coverage for all of the features that we write. So for now, we have two pages that pull data from a database, so there shouldn't be too many tests to write. So let's get started. Let's go to our phpunit.xml, and scroll down here. And I make use of SQLite and an in-memory database for as long as I can. But sometimes I eventually make use of the database-specific layer. For example, I might use something specific to MySQL, and at that point, I can no longer
Create Feature Test0:28
But sometimes I eventually make use of the database-specific layer. For example, I might use something specific to MySQL, and at that point, I can no longer use SQLite. But for now, it should be fine. So let's uncomment that. And let's go ahead and write our first feature test. So php artisan make:test. Let's call it ShowIdeasTest. Okay. And let's go to that.
Okay. And let's go to that. So as you can see, Brie has already added some tests here. And I'm also going to get rid of this example test here. Don't need that. And I believe there's one in the unit test folder as well. So let's delete this one as well. Okay, so let's remove this test. Let's use RefreshDatabase. And let's write our first test.
Test Ideas Index Page1:16
Let's use php artisan migrate:fresh. And let's write our first test. So I want to make sure that the list of Ideas shows on main page. So we are going to go to the main page. So let's say $response = $this->get(route('idea.index')). We can just do that. Or I like making use of route names. So idea.index is the name. And this should work. If we do $response->assert(200), or I like using assertSuccessful.
And this should work. If we do response, assert, okay, or I like using assertSuccessful. Okay, so this should pass. So for our arrange step, let's go ahead and make two Idea models here. So let's say ideaOne, let's make use of the IdeaFactory. So IdeaFactory::create(); And let's say title, myFirstTitle. And description, let's say descriptionOfMyFirstTitle. Okay. And let's go ahead and make a second one and just change it to ideaTwo.
Okay. And let's go ahead and make a second one and just change it to Idea two. And let's say $secondTitle and $secondDescription of my second title. Okay. So now we have two Ideas and let's make sure to import Idea. So that's our arrange step. This is our act step. We're just visiting the homepage and this is our assert step. So that one works. Let's say $response.
So that one works. Let's say response. Let's assert that we see the first Idea. So my first idea. Sorry, I meant to say idea here. I'm not sure why I said title. So let's say idea, idea, idea, and idea. Okay. So assert see my first idea, or we can just use the variable here. So let's say ideaOneTitle.
So assertSee my first Idea, or we can just use the variable here. So let's say ideaOneTitle. So let's see if this passes. Okay. That does. Let's assertSee the description. Should work as well. Okay. And we can do the same for the second Idea. So let's just duplicate this and say ideaTwo.
And we can do the same for the second Idea. So let's just duplicate this and say ideaTwo. Okay. This should pass. Cool. And if you want to make sure that that is working correctly, if we go to our index.blade.php and remove the title, say ideaTitle. So if I were just to remove this, the test should fail now. And it does. Cool.
Test Single Idea Page4:24
And it does. Cool. Actually, it was failing for another reason. So I removed the double braces entirely, and now it's failing for the correct reason. So I can't find this text here. So if I put this back, it should pass again. Okay. So let's work on the other test that tests the show page. So let's just grab this and rename it to say singleIdeaShowsCorrectly on the show page. So we should only have one Idea here, and we don't need a second one.
So let's just grab this and rename it to say singleIdea shows correctly on the show page. So we should only have one Idea here, and we don't need a second one. So we are going to idea.show, and we're passing in the idea. And it should be pretty much the same thing, except this is now called idea, and this should pass if I did everything correctly. And I forgot the annotation. Grab this. Okay. And let's run that again. Okay.
Test Pagination Behavior5:32
And let's run that again. Okay. And it passes. Okay. I have two more tests here, which aren't really necessary, in my opinion, but I'm going to write them anyways. So the first one is to test if pagination works. Again, I say this is not necessary because Laravel has its own tests to test pagination, but I like adding it anyways to make sure it works within the context of my application. So let's say ideas pagination works.
but I like adding it anyways to make sure it works within the context of my application. So let's say ideas pagination works. And oh, yeah, I wanted to make one change to our pagination. So let's do that. So let's go into our IdeaController. And we have this 10 here, which is sort of a magic number. So I want to change this to a constant, and I'll put it on the Idea model. So let's say Idea, and we'll name it paginationCount. So let's grab that. And let's go to the Idea model.
So let's grab that. And let's go to the Idea model. And let's make a const here. So I'll put it right here. Let's say const paginationCount, and we'll put 10 in here. And this should still work in our app. It does. There's 10 and 20 and 30. Okay, so let's go ahead and write that test back to our ShowIdeasTest. And I want to make the ideaFactory 11 here.
Okay, so let's go ahead and write that test back to our ShowIdeasTest. And I want to make the IdeaFactory 11 here. But let's make use of that constant. So ideaPaginationCount plus 1, because we're going to test if the 11th Idea shows up on the second page. So let's say create. So this should create 20, or yeah, 21 Ideas, or sorry, 11 Ideas. So 10 plus 1. Let's grab the first Idea, idea1 equals Idea::findOne.
Let's grab the first Idea, ideaOne equals idea. Find one. Let's change the title so we can assert against it. title, say my first Idea. Okay, and let's save that. ideaOne.save(). And let's do the same for the 11th Idea. So let's say idea11 is idea.find(11). Let's say my 11th Idea. And we're saving it.
Let's say my 11th Idea. And we're saving it. Okay, so now let's grab the first page. So we can do this. Let's assert that we see the first Idea. So same as this, and also the description if you want, okay, which is the title. Okay, and this should pass for now. Let's run that. Okay, and let's assert that we don't see the 11th Idea. So assert don't see idea 11 title, because that's on the second page.
Okay, and let's assert that we don't see the 11th Idea. So assert don't see idea 11 title, because that's on the second page. Okay, so I'm going to switch this to just a /, because it's easier to read when we add the query string for the second page, which is what we're doing now. So we're going to go to the second page. So in this case, it's page equals 2, like that. So we can just specify the query string in here. So page equals 2. And we can pretty much do the same thing here, but reverse it. So we want to assert that we do see the 11th title.
Test Duplicate Slug Handling9:27
And we can pretty much do the same thing here, but reverse it. So we want to assert that we do see the 11th title. So assertSee(idea 11 title) and assertDontSee(idea one title). So let's save that. Okay, so it is passing. And one more test, which is again, unnecessary, but it has to do with two titles that have the same name, but a different slug name. So I forgot to show you this in the previous video, but I'll show you it working in php artisan tinker well here. Okay, so I pasted in some code here.
well here. Okay, so I pasted in some code here. And this just creates two new Ideas. And both of them have the same title. And in this case, we're still working with the real database. So this should create two new Ideas, but the slugs should be different. So let's try running this. And that should have worked. So let's check our database. And let's see if we get those two new entries.
So let's check our database. And let's see if we get those two new entries. So right now we have 30 and now we have 32. And as you see, we have my first Idea and then my first Idea dash one, because it detected a duplicate. So again, the package does handle this. But if you want to write a test to make sure it works within your own app, you can do that as well. So I just pasted it in here. This is what we just had.
So I just pasted it in here. This is what we just had. So it creates two new Idea models. My first Idea with the same name. And then we are getting or we are going to the route for the first Idea, as you can see here, idea-one. And we are just asserting that the path is ideas/my-first-idea. Now we're going to the second Idea here, and we're asserting that the path is ideas with this new slug with the 1 appended on to the end.
Now we're going to the second Idea here, and we're asserting that the path is ideas with this new slug with the one appended on to the end. So let me just run this and see if it works. And it does. Let's run our entire test suite or the entire file. And we have four tests and they are all passing. Let's make sure to run the entire test suite to make sure everything is working correctly. So php artisan test. So it looks like it doesn't like the fact that I deleted the unit test folder. So I'm going to put that back.
So it looks like it doesn't like the fact that I deleted the unit test folder. So I'm going to put that back. Okay, so I put that file back. Let's run our test again. And everything looks like it's passing. So I'm going to move all these off tests to their own folder. So it doesn't interfere with our tests. Let's make a new folder here. Say off. Let's move all of these tests, which are all off related into here.
Say off. Let's move all of these tests, which are all off related into here. Let's move it. And I think we have to change the namespace. So yeah, we have to add \Off here. Okay, let's add that to all of them. Okay, so I changed the namespace for all of them. Let's composer dump-autoload to make sure everything is still in the right place. And let's run our tests again. php artisan test.
And let's run our tests again. php artisan test. And everything still looks like it's passing. So let's go ahead and make a commit. git add. This is episode 10. Test showing Ideas.
