در حال بارگذاری ...

Goal: Import Users0:00

Hey, everyone, my name is Christoph Rumpel, and today I'm going to show you how to build and test Laravel console comments. While working with Laravel, you probably have come across all the comments provided by Laravel itself. Let's get a list by calling php artisan. And yeah, here are the ones you can use while using Laravel. And I'm pretty sure you have used a few of them already. But today we want to create our own console comment. Our goal for today is to import users to the database through our console comment. And for that, I have this API endpoint.

Our goal for today is to import users to the database through our console command. And for that, I have this API endpoint. It's a service called randomUsers. And here we get our users that we want to import to the database. As you can see, I have provided here that I want to get seven results back and only the fields gender, email, nationality, and phone. And this is what we get here. So our goal is to import them through our console command to the database. All right, so how are we going to start? I'm going to start by creating a new test.

Creating Initial Test1:03

All right, so how are we going to start? I'm going to start by creating a new test. So let's clear this out and let's create a test. Make test and we're going to call it UserImportCommentTest. And this will give us now a new test under the Feature folder here. Yeah, here we have it already. Let's get rid of this ExampleTest and I'm using a PHPStorm template to create my own first test. So this first test is going to be a very high level test because we don't know too much about our implementation yet.

So this first test is going to be a very high level test because we don't know too much about our implementation yet. We know that we want to import a User to the database. So let's start there. It imports users to the database. So how do we go about that? So while working with console commands, we have this nice console test here provided by Laravel. So what we can do here is we can expect input and output. So here's a nice example.

So what we can do here is we can expect input and output. So here's a nice example. So inside our test, we can run this artisan command to fake a call to our artisan command. In this case, it's called question. And then we can expect a specific question here and also provide the answer and also expect some output. And this is really nice and comes handy when you want to test out your console commands. And now back in our test, I'm going to call this artisan helper. And now we need to run this command. And I give it a name.

And now we need to run this command. And I give it a name. So that's the signature of the command. And let's call it importUsers. Okay. And I already know I want this command to talk to the User because we need to provide the URL. So where to find the users to import. So I'm going to expect a question here. And let's say the command says, please provide the URL to the users.

So I'm going to expect a question here. And let's say the comment says, please provide the URL to the users. Maybe like that. And then we also need to pass in here as a second argument, the users import. And what we're going to do is we're just going to copy now this API endpoint. And let's paste it in here. And I also want to expect now specific output, a success message after the import. So let's say it says, thank you. users have been imported. And then at the end, we're also going to assert a specific exit code.

Users have been imported. And then at the end, we're also going to assert a specific exit code. And most cases it's zero, which just means we're expecting no errors to occur during this console command run. So this is what we expect of our comment. But now I also want to make sure that we have the users in the database. So I'm going to call the assertCount method of PHPUnit. And I want to assert the count of the users inside the database. So we were expecting, we're expecting 7 results here. So let's just say 7, and then just call all the users.

So we were expecting, we're expecting seven results here. So let's just say seven, and then just call all the users. And let's import the User. So with this assertCount method, phpunit is checking if this is countable, and that's a collection, and that's is, and that is countable. So this is why this test should pass like this. Okay, so this was now the more heavy part, because now we just have to run our test and it will lead us to our implementation. So that's what's pretty nice about TDD. So let's run the test the first time and let's see what we can find here.

Generating Console Command4:34

So that's what's pretty nice about TDD. So let's run the test the first time and let's see what we can find here. Not found exception, the Comment import users does not exist. Okay, that's fair enough. Let's create this Comment, php artisan make:comment user import comment. Okay, and now we have here under app/Console/Commands, our UserImportComment. And let's also change the signatures. That's how we're going to call it. And let's change it to import users. Okay, back in our test, we're going to run this and we should see a different error and

And let's change it to import users. Okay, back in our test, we're going to run this and we should see a different error and we do. So the question please provide the URL to the users was not asked. Okay, let's go back to our comment. And now inside the handle method. So that's the method that's being run when we call our comment, we need to make sure that this question is being asked. And there's this ask helper. And then we're just going to provide this message.

And there's this ask helper. And then we're just going to provide this message. We don't do anything more because we're just following our test output. So let's run it again. And now he's complaining about a different output that was expected, but he didn't see. So that's this one. Now back in our Comment, we also need to make sure that we see this. So there's this info method to output some data to output this message. And yeah, inside our Comment, again, we're going to run this test. And now we see an error about the table.

And yeah, inside our Comment, again, we're going to run this test. And now we see an error about the table. So there's no User table. So by default, and however, you know that there's a User migration. So I know that this exists, but we probably need to refresh the database to actually migrate the User table for our test. So what's next? Yeah, so all our expectations for the Comment are now passing. But now we're having an error about the size of the users inside our database. Okay, so how do we implement this now?

Fetching and Inserting Users6:40

But now we're having an error about the size of the users inside our database. Okay, so how do we implement this now? So inside our handle method, we first need to get here the URL. So that's what we get back from this question here. And then we need to make a request to the endpoint to get the users. And I'm going to use this new HTTP client of Laravel. And we're just going to make a GET call. And we get back here a response. Let's take another look at our data here. So we should get back here a JSON response with some results.

Let's take another look at our data here. So we should get back here a JSON response with some results. And inside the results are the users that we need. Okay, let's just dump this out to make sure that we're getting here the users. So there is this json method on the response, which gives us back an array. And then with the key results, we should see our users. We're going to run this. And yeah, here we have our users, seven users, and that's working. That's good. So we get back here our users.

That's good. So we get back here our users. So let's just call this users. And now we need to import them to the database. And we're going to do this pretty simple here. We're calling the insert method. And of course, we need to import the User. And let's just pass in here our users. And this should already be enough to make our test pass. Nope, we're not ready yet because there is no column named gender.

And this should already be enough to make our test pass. Nope, we're not ready yet because there is no column named gender. Okay, so what we forgot about is that we haven't changed our User migration. So let's go to this file here. And yeah, here is the default migration for Laravel. And we have some different fields now. So let's delete the name. We don't need this. We don't need this. So what do we need next to the email?

We don't need this. So what do we need next to the email? We're going to need a string for gender. We're going to need a string for the nationality. And we're also going to need a string for the phone because with phone numbers, you never know what you get. So string is fine here. All right, back in our test, run it. And now we're finally passing. So that's the first time that we see this green color.

Faking HTTP Requests9:09

And now we're finally passing. So that's the first time that we see this green color. So that's really good and really positive. So this means our first test is passing. And we've made sure that we now have seven User instances in our database through our console comment. So one thing I don't like about our test is that we are making an actual call to this API every time we run this test. And of course, that's what we don't want to do in our test if possible. We were using the HTTP client of Laravel.

And of course, that's what we don't want to do in our test if possible. We were using the HTTP client of Laravel. And this comes with a nice fake method. And you're going to provide an array. And the key here is this URL, so the endpoint that we're calling. Now we can say when we call this endpoint, the specific one, we can return a specific response. And here we're going to pass in now the JSON data. So let's go to the endpoint. Let's get the raw string here.

So let's go to the endpoint. Let's get the raw string here. And we're going to pass it in like this. So this means we are faking our HTTP client. And whenever we're going to call this endpoint, we return this here. And this way, we make sure we don't actually call this endpoint. Let's run this again. And we are still passing. So that's pretty good. But of course, we don't want to keep this here in our test.

So that's pretty good. But of course, we don't want to keep this here in our test because it's pretty messy. So I think let's just put this into the setUp method of our test. And here, after calling our parent setUp, we're going to pass in here this fake call. OK, that's pretty good because we're now going to write another test. And then we can also make use of this fake data. So for our second test, it is about a new feature.

Adding URL Option10:54

And then we can also make use of this fake data. So for our second test, it is about a new feature that we want to provide. And we want to make sure that we can also pass the URL as an option. So let's say it accepts the URL as an option. Because sometimes you don't always want to provide the URL through the question. You just want to provide it with the call of the console command. This comes also handy if you want to call this command through your code and not through the console.

So now we don't want to expect this question because we're already providing the URL. Run the test. And let's check the error. So the URL option does not exist. And let's make sure that it does. So inside our Comment now, inside the signature, we can change this by saying we want to provide the URL. And with the equal sign, we're also telling that we want to provide a value of it.

This is just a simple explanation with no code.

So that's true because we don't expect in this question now, this one that we have deleted in this test, but we are still asking it inside our comment. So here. So this ask method is called every time. So to make sure that it's not being asked if we have a URL, we can just grab the option here. It's called URL. And if it's given, we want to use it. And if not, we want to ask.

But now that I see it, there's one more thing I would like to add to this first test. So we are checking the number of users in the table, but not the provided data. Let's add this by checking one of the user's attributes as well. So we are going to call the assertDatabaseHas method. Now we need to provide the table. And it's about the users table. So let's call this getTable method.

And it's about the user table. So let's call this getTable method to get the name of the table. And then we're going to provide an array of data that we expect to see in our database. So we're now testing this first user. So I'm grabbing here data with these fields. And we're now going to pass them in here. And let's change the format. We're going to need an array.

And let's change the format. We're going to need an array. And we also make sure that we use single quotes here. Yeah, so now we're also checking that inside our user table, there is this data. And with this test, we're also making sure that these fields are set correctly. And yeah, so these are also working. So that's pretty cool. And let's also run both tests together now.

So that's pretty cool. And let's also run both tests together now. Everything is still green. And now I'm pretty happy with our test. But let's also check out our comment here. So as you can see, we don't have too much code in here. So I think I'm pretty fine with how this looks like. In some cases, things like creating the users will take some more lines. Then it will be handy to create here another method.

your level console comments. We have now some nice tests to make sure that everything is working as we expect. So we have this fake method that we're using so we don't make calls to our endpoints. We were using the nice expectations of php artisan comments that help us to expect our comment. And we've been also using this database helper to make sure that we import the data and also the specific fields.

to make sure that we import the data and also the specific fields. So I hope you're now ready to create your own console comments and wish you all the best and have fun.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟