Playing
09. Creating a custom Pest function

Transcript

00:00
So, let's create our first pest custom function.
00:04
We saw these a little bit earlier over in pest.php when we were looking here, and you can see that these are the functions we can define. They're literally just PHP functions that we can add into this pest.php file. You could technically put them anywhere.
00:19
So we're going to look at creating one that actually is within the docs, but we are going to create some others a little bit later on. And this is going to be an acting as function. So effectively, we're going to replace the need to have to use this, much like we've
00:33
been doing for get and post, a very small change, but of course, anything that speeds up your test is a good thing. So let's go over here to pest.php and look at what we can do. Now what do we need to pass in here?
00:49
Well, we need to pass in a user. You could type in this if you wanted to, to authenticatable. So let's pull that namespace in here. That's just anything that can authenticate, could be a user, a different admin model.
01:03
And what do we do here? Because normally, we would do this acting as, so that would kind of be the shorthand that we were after. But of course, in the context of a function, we can't do this, and also, it doesn't really
01:16
make sense. Well, within pest, we have this test function, which we can effectively use to just chain on anything that we need to do. So just by doing this and going ahead and returning that, that's going to have exactly
01:29
the same effect as what we're doing here. We're passing through what we want, what user we want to act as, and we're just using this test to then allow ourselves to chain on acting as. So a very, very small change.
01:42
But once you've done this, now what you can technically do, if we just run our test just to make sure this is all working first of all, we can get rid of this. Now we have that function that's going to allow us to pass that in. So now, if we just go and rerun our tests, sure enough, it works.
01:58
So a very small change, but that's going to save you having to type that this keyword every single time. And technically, within this test now, what we could do is we could pull this down and pull it in.
02:08
We could have done that before, but that makes your test even shorter and a lot more elegant. And again, when we run our tests, we still get green. So now we've created a custom function. This is a kind of test that we're going to be using a lot.
02:21
It redirects an authenticated user if they're not supposed to be in there, e.g. if we're using that guest middleware. So we'll head over to the next episode and look at how we can pretty much create this entire test in one line.
35 episodes4 hrs 19 mins

Overview

Pest is a PHP testing framework that brings beautifully simple syntax to your tests, without sacrificing on features. In this course, we'll get up and running with Pest in a Laravel project and write tests for a real-world application that we'll build along the way.

You'll learn how to set Pest up in a Laravel project, write tests with Pest's built-in assertions, generate code coverage, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!