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

Updating Middleware Context0:00

In the previous episode, we saw how we could use the context inside of our tests, and it worked very well, except I want a little more flexibility. I want to be able to directly inject any Account into the context and use that Account for our tests. And we can do that, but we need to address our middleware, because that is where the active account context key is currently being overwritten. So let's open up that middleware, and let's make it a little more intelligent. Now, in the grand scheme of things, I'm not necessarily sure this is something that I would actually want to do, but it does add more flexibility. So therefore, let's just go ahead and do it. So we can check if the context already has a key set up in the context,

So therefore, let's just go ahead and do it. So we can check if the context already has a key set up in the context, and we can do that with the has method. We just pass the key, and it will tell us exactly where the key is. But in our case, we want to check if it does not exist, because if it doesn't, then we will want to add whatever is in the session. So we could go ahead and just use the add method. We want the active account, and then the value is, of course, going to come from the session. We'll leave everything else alone, at least as far as this file is concerned, and we should now be able to inject the key.

Refactoring Tests Setup1:10

We'll leave everything else alone, at least as far as this file is concerned, and we should now be able to inject the key. So I want to do this. We're going to change our beforeEach so that we're going to have two tests. We're going to check if the dashboard displays the account's name using the context. Then we will have essentially the same test, but it's going to use whatever was provided in the session. So that means inside of our beforeEach, we need to make some changes. First of all, we need to make the user and the key, First of all, we need to make the user and the account accessible.

First of all, we need to make the User and the Account accessible. So we're going to make those properties. We just need to paste that in in a few places. And then we are going to call the actingAs, passing in the User, and that's going to be it as far as our setup is concerned, because inside of our other tests, we will want to add the active Account using the context or using the session. And so that's easy enough to do. Inside of the test for our context,

And so that's easy enough to do. Inside of the test for our context, we are just going to add that account to the context. And then we will do the same thing for the session, except that we will, of course, call the withSession method. So let's clean this up a little bit. And then inside of our test for the session, we will add the account using the session. So we should be able to go to our tests. And I guess, first of all, we need to do this.

Running and Fixing Tests2:32

So we should be able to go to our tests. And I guess, first of all, we need to do this. Let's make these tests fail. Just so that we can see that they actually do fail. And then we will make them so that they work. And, okay, I was not expecting that. Oh, I need to save the file. That's kind of important. So let's run it again. And now we will see if it fails.

So let's run it again. And now we will see if it fails. Yes, thankfully it does fail. So we are good here. I'm going to assume that this 500 is because we cannot access the name on null. So let's just now add the context. So back in the command line, we will run our tests again. And then we will see that they both pass. You know, but here's one thing that I have a question. Is the context already set up with the active account key for this other test?

You know, but here's one thing that I have a question. Is the context already set up with the active account key for this other test? Frankly, I don't know. But I want to be sure that there is not an active account already in the context before we add it to the session. So what we could do then is use the context to call the forget method. And then we can pass in whatever key that we want the context to forget. So this is essentially going to remove that item from the context, therefore making it not there. So whenever we add it to the session,

therefore making it not there. So whenever we add it to the session, then the middleware should be automatically adding that account from the session to the context. So let's run our test again just to make sure. And sure enough, it passes. Now, there is a different way that we can approach this inside of our middleware. This is the most straightforward, and it is, of course, the easiest thing to do. But we do have another method that we can use called wins. And it accepts three arguments.

Alternative win() Approach4:10

But we do have another method that we can use called win. And it accepts three arguments. The first is a Boolean expression. So we can check to see if the context has the key of activeAccount. And if that is true, then the closure that we provide as the second argument is going to execute. Now, in this particular case, we don't want to do anything because the context already has the activeAccount. So we don't want to overwrite that with anything else. We just want to do nothing.

So we don't want to overwrite that with anything else. We just want to do nothing. So our closure is essentially going to do nothing. However, if our condition is false, well, then the third argument, which is another closure, is going to execute. And in this case, we would want to add the activeAccount and give it the value from the session. So this is an alternative way, which in this particular case, I don't necessarily like just because it makes it a little more difficult to read. But we should end up with the same result.

Introducing Stacks and Lifecycle5:06

I don't necessarily like just because it makes it a little more difficult to read. But we should end up with the same result. So if we run our tests again, then we should see that they both pass. However, I can see some cases where using the win method would be beneficial. It's just in this particular case, I don't think it is. So we'll just stick with that. Now, we're almost done looking at the context. There's just a couple more things that we need to look at. The first is this concept of stacks. And then the second is how to hook into lifecycle events.

The first is this concept of stacks. And then the second is how to hook into lifecycle events.

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