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

Multiple Dusk browsers0:00

Here's a neat trick to throw in your tool belt. I have a Laravel Dusk test here, and notice I call a browse method, and then I provide a closure here. Now for the parameter list, I'm requesting a browser instance that I then use to visit a page and fill out a form. But the main thing we're interested in here is that parameter list. So as it turns out, when you're using Dusk, I can request as many browser instances that I want. And a use case for this might be testing real-time notifications. Browser number one performs a set of steps to trigger a notification.

And a use case for this might be testing real-time notifications. Browser number one performs a set of steps to trigger a notification. Browser two visits the page and asserts that you do see it in the bottom right. That would be a use case. But yeah, that's kind of cool, right? You can request as many of these as you want, and it'll just work. Now if you use Laravel, you almost take this for granted, because you already know for your controllers and various other areas of the framework, you can type in what you want and Laravel Service Container will do its best to resolve the instance and send it through to you.

Closure parameter injection0:56

and Laravel ServiceContainer will do its best to resolve the instance and send it through to you. However, in the case of a closure, it's a little bit more tricky, right? How exactly would that work? Well, I'll show you. We'll do an example, and again, you're not going to reach for it all the time, but it's something to throw in your back pocket for those cases when it would be appropriate. Okay, so let's call a generic method, maybe load whatever would be appropriate for your system. And all we're going to do here is, let's just say I want to load any number of Users.

system. And all we're going to do here is, let's just say I want to load any number of Users. Okay, so we'll start with this, and then I'll verify it by dumping the User. Okay, so if I give this a run, it is in fact going to fail, and that's because we don't yet have a load method. All right, let's set that up, and that will accept the callable. It sounds like we need to figure out, well, how many Users do you want? So let's say we're going to request two Users. We need to inspect this and figure it out. And to do that, php offers a variety of reflection-based classes.

Reflecting closure parameters1:50

We need to inspect this and figure it out. And to do that, php offers a variety of reflection-based classes. Take a look. All of these. And in fact, it's sort of the backbone for Laravel ServiceContainer. Anyhow, in our case, we want ReflectionFunction. We will reflect into this anonymous function or closure. Okay, so let's take a look at that, and all I'm going to do here is dump it. We'll give our test a run, and if we scroll down, here's what we get. So take a look.

We'll give our test a run, and if we scroll down, here's what we get. So take a look. Among other things, we can review the parameter list, the name of the parameter, the class type, the number of them, all sorts of things. All right, so let's see. For the methods, I'm going to call getNumberOfParameters, and if I give this a run, we'll get two. So two because we have two users here. If I request a third one, it'll read that parameter list and return three, as you see there. All right, so in our case, if we're only passing in users, and that's all we will ever pass.

Generating users dynamically2:44

there. All right, so in our case, if we're only passing in users, and that's all we will ever pass in, similar to that Laravel desk example, then this ends up being really easy. All I have to do is say for each range one through reflect number of parameters, and in fact, reflect is a bad name. Let's rename that to simply function for now. All right, for each one of those, all we have to do is whip up what the user wants, a user. So let's do it as an array, and maybe we'll refactor in a minute. So we'll say users, push to it a brand new User, or we could use a factory class, and that's why in this case, because we have factories, it's not overly useful, but it's only an example.

So we'll say users, push to it a brand new User, or we could use a Factory class, and that's why in this case, because we have factories, it's not overly useful, but it's only an example that you can use in other areas of your code base. Anyways, iterate through as many users as I've requested here, and for each one, instantiate a User. You know, in real life, you might be sending through other data, you might be resolving stuff, you might be building up dependency lists and things like that. Now, at this point, we should have a total of three users, and in fact, we do. All right, so the only remaining step is to trigger that callable and send through the users.

Invoking callable with args3:50

All right, so the only remaining step is to trigger that callable and send through the users. But now be careful, users is an array, right? And what we really want is three individual users, not an array of users. So we will, what is it, destructure, destructure that into three parameters. All right, so now if I give it a run, and there we go, we have our user. In fact, we just say func getArgs, and we'll have an array of three users, which is exactly what we want. Let's request another one. Now we have an array of four users.

Refactoring with collections4:23

Let's request another one. Now we have an array of four Users. And as it turns out, behind the scenes, this is basically what Laravel Desk is doing. It reads the parameter list, it determines how many you want, it creates the browser in whatever way is appropriate there, and then it sends it back to you. So now if you want to play around, just as an exercise, let's change this into a collection. So what we'll do here is I'll say, collect this range, like so, and then we will map over it. And for each index, I'm just going to return a brand new User. And yeah, I think that should do it, run it again.

And for each index, I'm just going to return a brand new User. And yeah, I think that should do it, run it again. There's our four users. And yeah, that's the basic approach. Of course, you could extract this to a trait or a parent helper method. And again, you're not going to reach for it all the time. But every once in a while, this might be exactly what you need.

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