Playing
25. ddBody test method

Transcript

00:00
Okay, let's take a look at the new DDBody method within the TestResponse within Laravel Tests. Now, this doesn't matter whether you're using PHPUnit or PEST to test this. This comes back from the TestResponse class, which we will dive into very, very shortly, when we make some sort of HTTP request within our tests. So you can see at the moment, I've
00:25
got a very, very basic test that hits the slash user endpoint. And we're just using an assert status to make sure that this request is successful. If we take a look at this, I've just created a really basic root tier inline node controller that returns some JSON. This does work with HTML pages as well. So you can go ahead and use this method to inspect
00:50
HTML. Let's just take a look at what this does, though, and it's easier to demo this with JSON. Okay, so let's say we are building out a test, and I want to see what the response looks like. Perhaps I'm trying to debug what a certain parameter or certain condition has changed within the response. How do we do this? Well, we would probably start to reach for
01:13
a dye dump on the response object itself. Let's go ahead and run that test. And you can see, sure enough, we do get a bunch of stuff dumped out. And we can sort of see this data somewhere in here. But where is this coming from? Well, it's coming from if we just head up here, this base response. So what we would actually have to do is go into
01:34
this base response. And again, let's just go ahead and run that test. And yeah, this is a little bit more useful, we can see this data. If we wanted to do something with the data, though, we could say something like get content, I think it is, let's just go ahead and run that. And yeah, there we go. So we kind of get this out. But there must
01:54
be an easier way. And there is a much easier way. Now that we have the dd body method. So if you're unaware, throughout or sort of scattered throughout the Laravel framework, within things like collections, this test response, we have the ability to just dye dump. So if you're doing something like a multi line request here, with a couple of
02:17
other methods, and at some point, you just want to dump the status of what's going on, you can just reach for the dye dump method, or dd method. And this is pretty much the same for collections as well. And it exists in a couple of other places within the Laravel framework. So you can see here that although we get the same result, we've just had to
02:36
chain on a dd method. And that's going to dye dump this object at that point. Now, why is this useful? Well, what we now have is dd body, what this is going to do is it's going to take the body that we just saw. And it's going to dump out for us at the point that we call this method, much easier than coming down, dying and dumping manually on
03:00
the response and trying to extract the data you need. Let's check this out, run the test. And plus, this is now an actual array. So what I'm going to do now is just go into that test response just to have a look at this dd body method and actually show you what it's doing. So it's using this content, which to be fair, we could have used before. So we could have
03:20
used that handy method before to dye dump this out. So I did make it look a little bit more difficult than it is. But this method is still super handy. Now, if we take a look at this method signature, we can also pass in a key here. And what does that mean? Well, you can see here, if we have this JSON validate function available, and this goes ahead and allows us to
03:44
detect whether this content is actually JSON. So we validate this as real JSON, then we use the JSON key. So obviously, this is useful for when you're building out an API, and you want to extract a single value from this. So I want to dump just on the name here. Let's run this. And there we go. Now, if our controller or our response here was returning
04:09
a view instead, that wouldn't work. So you don't need to worry about this just erroring, it's just going to dump out the entire body here of the HTML page, which again, is still useful. So you can use DD body to do exactly that. Okay, so now that we've looked at DD body and name, we also have a few other things. Now, these aren't new additions,
04:33
but I thought I'd mention them since we're in the position of talking about DD body. And we have things like DD headers. So we can go ahead and dump out the headers of the response that we get as we're doing this. And there's a bunch of other ones of these, of course, it's a good idea to just go over to your test response, either look from them in here,
04:54
which they are in here. So we've got DD session, in which case you can provide some keys, DD JSON, which is used behind the scenes from that DD body. When we provide a key, DD headers, which we've just looked at DD body, which is new. These three here existed before, as well as just DD on its own. But DD body has recently been added. And I think this is really
05:17
helpful. The amount of times that I go in, and I try to go ahead and dump something out within my response, it just saves time having to do this, type out the response, type out the content, and then going ahead and running it. The other way this is useful is that sometimes within our tests, let's just get rid of what we're doing here, I'm going to get rid of DD body here
05:42
as well. Sometimes in our test, we are not assigning a response. So we're doing something like this, this get user assert status 200. That's the way that I would write the test. So if we go ahead and run this, sure enough, it passes. But this is where, at this point, if I want to go ahead and dump out what's coming back from here, so I can create some assertions,
06:06
that's where this method comes in handy. So if you didn't think it was useful before, let me demo this for you now. So if we're writing tests like this, and I want to dump out the result of what happens here, instead of having to then go ahead and reassign this a value, end this, go ahead and either comment this out, or switch this back to content, then go ahead and say
06:32
die dump content. And we should have called that response, but let's just call this content content for now, you kind of get the idea. And seeing this, now let's go back to how we kind of want to write our test, which is the shortest way possible. I can now just chain on anywhere, it doesn't matter if we have methods in between this, dd body. And there we go. So it's just a
06:57
lot more convenient. That's the whole point of these dd type methods, that we can sort of inject into the lines of our code, and it just makes development a lot quicker. So there we go. I know I've explained a lot here, but it's really, really helpful to have these in your toolkit for when you're debugging during your tests.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

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

Episode discussion

No comments, yet. Be the first!