This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
11. Testing actions

Transcript

00:00
All we're really going to need to do here is create a fake user, go ahead and handle this action by running it, and then just making sure that user has a post. So let's go ahead and just start out with the phpunit.xml file just to make sure we're set up with testing. And to do this, I'm just going to enable SQLite and the memory database. That just means we can go ahead and
00:23
run our tests here with phpunit. And we've got a few already created for us as part of Laravel Breeze, but we can just kind of ignore these. So let's go ahead and write a test in here or make out a test. And let's put these in an actions folder specifically. So let's go ahead and call this create post test. That makes sense. And let's go ahead and open that up. So that'll
00:46
be now in your tests directory under feature and actions. Okay. All right. So because we're going to use database migrations, we're going to want to come down and use the refresh database trait here. And then let's go ahead and switch this example test out with something a bit more useful. So let's just create a really simple test, test that it creates and returns a post. So we are
01:10
making sure this gets created, but also gets returned at the same time. And we'll just go ahead and get rid of the example test data we have in there. So now we can just run phpunit. And let's go into tests and feature and actions. And let's run that create post test. Okay. Nothing is being asserted at the moment. So because our action is just a class, we can go ahead and
01:32
new this up. So let's create a new create post action in there, making sure we just pull that in. And now we can just go ahead and handle this. We want a return value because we want to be able to check that the post is actually returned. So let's go ahead and on the action call run. That's just similar to what we do when we're actually using the action, maybe within a
01:51
controller directly as we saw earlier. So now what we want to do is pass a user in or any of the other data that we expect to see in. So in create post, we want to pass in a user and an array of data. So in here, we can use our user factory, which will already have been created for us when we installed Laravel. So let's go ahead and assign that user at the same time as we pass
02:16
it in just to keep this nice and clean. So we're going to go ahead and use the factory for our user to create our new user. And that's the first argument taken care of. The second is, of course, just the data that we want to pass in. We don't have much in here. So let's just go ahead and pass in a body. And we'll just say a post. So when we run this test, that should create that
02:36
in the database for us. And now we can perform an assertion to make sure that that is the case. And then we pretty much know our action is creating a post and returning a post to us. So the first assertion is going to be that the user actually has posts. Now, there's a load of ways that we can do this. We can just rely on the return type if we want to. What I like to do is
02:57
go over and just check the relationship contains the post that we get back. So what we can do is say assert true and user posts and contains. So we're using the user that we created here. And we want to make sure it contains the post that we get back. What we can also do at the same time is just assert that we're getting back a post instance that's been created. So
03:22
we can use something like assert instance of and we can check that the post that we get back is of type post. So two very simple assertions that give us the confidence to show that this has been created and returned. Let's go ahead and run PHP unit on that specific test. And it looks like it fails at the moment. So this is just on the set instance of let's just go ahead
03:44
and make sure we pull the namespace in for that. And let's rerun this. And there we go. We get one test pass with two assertions. So a really simple test here, because of course, this action just does one thing. It creates a post and returns it to us. So we're now pretty confident that this is working.
12 episodes1 hr 2 mins

Overview

Actions are single classes that do one thing. Laravel Actions is a package that allows you to run these classes as anything you want, whether it's a controller, listener, console command – or just on its own.

Keeping your app structure to single classes like this lets you focus on what your app does rather than the design decisions around controllers, listeners and commands.

Single actions are also easier to test, and we'll also cover that!

This course is for you if:

  • You'd like to try a fresh approach to structuring your apps
  • Your app shares logic, and you'd like to combine this logic into one class that runs anywhere
  • You've heard of (or used) Laravel Actions, and you'd like a run-through
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!