تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Unit test isAdmin0:00

Okay, let's work on testing this setStatus functionality. So the first test I'm going to write is just checking if this isAdmin function works correctly. So let's make that a unit test. Make UserTest, make a unit test. And I think I had a UserTest initially, but then I removed it. So let's go here. And actually, I'm going to grab this stuff from the other test. So let's go ahead and replace this. Let's use refreshDatabase. And let's go ahead and write that test.

Let's use php artisan migrate:refresh. And let's go ahead and write that test. Let's say canCheckIfUserIsAdmin. So let's make a User, and let's make sure their email is an admin. So $user = User::factory()->create(), or we can use make here and just store it in memory. So let's try that instead. $name, say Andre, and $email is an admin email. So in this case, my email, okay. And let's make another User as well, which does not have an admin email. So let's say $userB.

And let's make another User as well, which does not have an admin email. So let's say userB. And let's just change this to some other email that's not an admin, okay. And let's make sure to add the semicolon. And we can just make some assertions here. So this assertTrue(user->isAdmin()). And assertFalse(userB->isAdmin()). Okay, let's try this. And it does work. So just to make sure, let me just change my email here.

Create feature test suite1:55

And it does work. So just to make sure, let me just change my email here. And this should fail. And it does, cool. Okay, now let's go ahead and make a feature test to test the Livewire component. So make test, let's call it AdminSetStatusTest. And it's a feature test, okay. Okay, AdminSetStatusTest. And let's go ahead and use RefreshDatabase as always. And let's remove this one and write our own.

And let's go ahead and use php artisan migrate --refresh as always. And let's remove this one and write our own. So the first one is to test if the Livewire component renders if we're an admin. And we'll have another one to check if it doesn't render if we're not an admin. Social page contains set status Livewire component when User is admin. So as always, I'm just gonna paste some setup in here. Okay, so I pasted in some setup. Let's import User, Category, and Status as well. So we have a User that is an admin. We have some categories.

So we have a User that is an admin. We have some categories. We have a status and we have an Idea. So let's go ahead and test this. So in this case, we are an admin. So we are logged in. So acting as User, let's go ahead and go to the show page. So route('idea.show'), and we're going to the Idea. And we just have to assert that Livewire component exists because we are an admin.

And we just have to assert that Livewire component exists because we are an admin. Assert see Livewire and the component name is setStatus. Okay. So let's go ahead and test this out. And it does pass. So let's try the opposite case. So we can just duplicate this and change the name. Okay. So show page does not contain setStatus Livewire component.

Okay. So show page does not contain set status Livewire component when User is not admin. So let's change the User's email. So let's just say user@example.com. And then acting as User, go to this page and assert don't see Livewire. So assert that we don't see this component because this User is not an admin. Okay.

Test initial status state4:29

because this User is not an admin. Okay. And we are at green. All right, next test I'm going to write is to check if the status is set correctly once we visit the page. So it's considering and we have the correct radio button selected for this status. So let's say initial status is set. Correctly.

So let's say initial status is set. Correctly. And again, I'll paste in some setup. So again, setup is pretty much the same, but I'm using a new status here, status considering. So ID is two. Okay. And let's go ahead and test this component. So we'll use Livewire here. So Livewire test or acting as first.

So we'll use Livewire here. So Livewire test or acting as first. So this is admin and let's go ahead and test it. So test the setStatus component. Okay. And that takes in a param of an Idea. So let's pass that through. Okay. So all we want to assert is that that status is set correctly.

So all we want to assert is that status is set correctly. So assertSet. So if you don't remember, let's go to that. So setStatus. So status is being set right here. And that comes from the Idea that's being passed through. So the Idea that's being passed through is this one. The status is this one. So assertSet status.

The status is this one. So assertSetStatus. And we can grab it from the variable or we can just hard code it. I'll grab it from the variables. So status considering ID. And let's go ahead and import Livewire. And let's test this out. And we are not passing. I think I have to import setStatus.

Test status update action6:27

And we are not passing. I think I have to import setStatus. Let's try that again. And we are passing now. Okay, one more test here. And that's to test if we can actually setStatus. So let's say can setStatus correctly. Okay. And let me paste in some setup. So basically the same thing.

And let me paste in some setup. So basically the same thing. We're starting off on status considering right here. So ID 2, and then I'm gonna change it to ID 3. So let's do that. I'm gonna grab this here and space that in here. Okay. And let's get rid of this. Let's set the status

And let's get rid of this. Let's set the status to the new status. So status in progress ID. So this would be the equivalent of just selecting one of these new statuses. And then when you wanna hit update, and then that would be just to call the setStatus method. So we can call setStatus. Okay.

So we can call setStatus. Okay. And let's go back to that component. So this updates the actual Idea. And it emits this event. So let's assert that the event is emitted. So we can do assertEmitted. Status was updated. Okay. So let's try this out first.

Okay. So let's try this out first. And it does pass. And then after this Livewire test, we can check if the database is correct. So this would be checking if the status was updated on the Idea correctly. So we can do this, assertDatabaseHas, or you can just work with the model directly if you want,

assert database has, or you can just work with the model directly if you want, but I'll just use this. So we're checking the ideas table. And we wanna check if there is an ID of ideaId. Okay. And the statusId of that idea should be the newStatus or the new, yeah, the newStatus. So statusInProgressId, because that's what we changed it to.

Run tests and commit8:53

So status in progress ID, because that's what we changed it to. Okay. So let's see if this works. It does. Let's run our entire test suite again to make sure we didn't break anything else. test parallel. And everything looks good. So as always, let's make a commit.

And everything looks good. So as always, let's make a commit. This is episode 33. git commit episode 33. Admin set status test.

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