Testing Models First0:00
Before we start writing our controllers and our logic, I want to test our models. Admittedly, I'm not much of a tester. I will write tests if something breaks, but generally I don't write tests before writing code. But I think it's important in this particular case because I want to be sure that all of our models are going to work as I expect them to. Because when it comes time to actually implementing the controllers and the logic
Because when it comes time to actually implementing the controllers and the logic and all of that, if creating posts, replying to posts, reposting posts, liking and following, if all of that stuff works, then it'll make it much easier down the road. So the first thing I want to do is check to see if pest is installed because I want to use Pest. But whenever we ran the installer, I don't remember an option.
But whenever we ran the installer, I don't remember an option. And sure enough it's not here because that would've been, I think the default option. And this is one of the things I don't really understand. It used to be part of the installer and then the installer that we ran, uh, which was an older version as it is today, and it wasn't there. But if we ran the installer now it would be an option.
Installing Pest Framework1:10
and it wasn't there. But if we ran the installer now it would be an option. Dunno. But anyway, this means that we need to install pest or at least I do check to make sure if you need to install it or not. Because chances are when you installed the project pest was or, or the testing framework was an option. So here we want to run composer require pest/pest, but we want this as a dev dependency.
pest php/pest, but we want this as a dev dependency. We also want with all dependencies, and this is just the first step, there are other things that we need to run. Like for example, we need to install the plugin. So once again composer require pest php/pest-plugin-laravel. This is a dev dependency. So once again use that dev flag.
Creating Pest Test File2:00
This is a dev dependency. So once again use that --dev flag and when that's done we need to initialize pest. So we'll run vendor/bin/pest in it, that initializes everything. And yes, I would like to start the GitHub repository. I've already done that. So I'm gonna hit no because next we need to create our test. So we will make a test, let's call it PostBehaviorTest because our file name needs to end with the word test.
So we will make a test, let's call it PostBehaviorTest because our file name needs to end with the word test and that's going to at least get us started. I do however, want to go ahead and remove the example tests because those are tests that we just don't need to run. So we'll get rid of those. Let's open up our test file and let's write our first test. We will be working with the database. So let's go ahead and pull in Illuminate\Foundation\Testing\RefreshDatabase
So let's go ahead and pull in Illuminate\Foundation\Testing\RefreshDatabase because we want to refresh the database. And now that I think about it, we need to run our migrations because we didn't do that. At least I don't think we did that. So let's go back to the command line and we will run php artisan migrate and yay, there's no errors with the migrations or at least from a technical syntax standpoint.
Testing Post Publishing3:17
and yay, there's no errors with the migrations or at least from a technical syntax standpoint. So we're good there. Now we just need to write our test. The first thing I want to do is test that it allows a Profile to publish a Post. And the first thing that we need is a Profile. So let's create that Profile. We will use the factory to create that one Profile and then we need to create a Post. Now of course we could use the Post factory to create this,
and then we need to create a Post. Now of course we could use the PostFactory to create this, but I also want to think in terms what we would do inside of a Controller or you know, things like that. Because I want to start building an API for the lack of a better term so that when it comes time to actually do these things inside of the controllers, then we will have everything ready to go. So we want to include Profile. We also want to include the Post model as well.
So we want to include Profile. We also want to include the Post model as well. So let's think about this. We could have a static method called publish so that we could say we want to publish a Post for the given Profile and then we would have the content of the Post pass to it. That would be an easy way to create a Post for a given Profile. I like it. So let's implement that.
for a given profile. I like it. So let's implement that. We'll go to the Post model and we'll just add that static method. So public static function called publish. We want the profile that is going to create this profile. Then we also want the content and this is going to return self so that we will return static::create, and then what we need the profile_id.
that we will return static create, and then what we need the profileId to be the provided profileId. Then we also want to set the content. And since this is just a normal Post, it's not a reply, it's not a repost. Uh, let's be sure that the parentId is null as well as the repostId. And those are two things that we can also check in our test just to make sure that whenever we create a normal Post or,
And those are two things that we can also check in our test just to make sure that whenever we create a normal Post or, or rather whenever we publish a normal Post, then it is not a reply, it is not a repost. So I think that's good. That gives us a Post so that then we can expect first of all that the Post exists. That would be kind of an important thing. So we want to be sure that that is true, but then we have several other things that we want to check,
So we want to be sure that that is true, but then we have several other things that we want to check, such as the profile of that post. So we will check if the profile is the profile that we have and we want to ensure that that is true as well. So we'll have that. It's not too blue to be true. It's kind of hard to talk and type at the same time. And then we need to check the, uh, the reply and the repost IDs.
And then we need to check the, uh, the reply and the repost IDs. So first if the postParentId to be null, and we essentially want the same thing for the repostId. So that's gonna be our test. Everything should work. Okay, well as long as everything is set up. Okay, so let's run the test php artisan test
Debugging Test Failures6:50
Okay, so let's run the test php artisan test and hopefully it's going to, well it failed. So let's take a look here. Uh, general one air, one table users has no column named name. Yeah, because we took that out. We took it out of the migration, but we didn't do that out of the factory, did we? So let's go to the UserFactory and let's take out name.
but we didn't do that out of the factory, did we? So let's go to the UserFactory and let's take out name. And you know, there is by default inside of the database seeders where the User is created. So let's take that out as well. So, okay, so that should fix that particular issue. So let's run our test again. Fingers crossed, didn't work. It failed this time. Insert into profiles and it is an array to string conversion, array
Insert into profiles and it is an array to string conversion, array to string conversion. So let's open up the ProfileFactory and what would be an array here. Uh, the faker name, the username, the sentences, that's it. That creates an array of however many sentences that you want. We need to pass in true so that it returns it as text.
however many sentences that you want. We need to pass in true so that it returns it as text. Should have remembered that, but oh, well, let's run our test again and it failed again. At least we're fixing these things, which is why I wanted to run these tests to begin with so we can be sure that by the end of testing all of this functionality that it's gonna work when we need it to. Okay, so general error, no such table main repost.
it's gonna work when we need it to. Okay, so general error, no such table main_repost_ofs, that's gonna be inside of the create_post_migration. So let's open that up. And you know, we have the parent_id, I'm kind of surprised we didn't get one for there. So let's do this for constrained. We're gonna specify the name of the table, which is posts because that is where we want to be sure that the repost_of_id and the parent_id are the same.
because that is where we want to be sure that the repost of ID and the parent ID are the same. So those will be constrained there, which means we should rerun our migration. So let's do php artisan migrate:fresh, that will rerun our migrations and that should work. So that now php artisan test, let's see what we get. It passed. Hooray. So we fixed some very important issues.
Recap and Next Tests9:20
It passed. Hooray. So we fixed some very important issues that would've been there and if we were implementing our controllers and running into those issues might be a little more involved to find those problems. But at least by just running a test and being sure that the tests run well, that makes it easier to debug these types of problems, at least in my opinion. So since that took a little bit of extra time that I want, we at least have one test for creating
So since that took a little bit of extra time that I want, we at least have one test for creating or rather for publishing a Post. In the next episode, I want to test other forms of, in the next episode, I want to test other things such as replying and reposting Posts.
