Need Database Rollbacks 0:00Now, there's one thing that we haven't yet accounted for. Let's take a look at our database. So, I will sign in. And if we select star from posts, well, that's not empty even though it should be for our demo. Here's what I mean. If I switch back to my example test, well, we have situations like this, where you want to say, given I have these records in a database table, when I perform this action, then I expect those results. So, if you think about it, well, after this test runs, we sort of want to roll back our database to how it was before the test began. And if we don't do that, well, we will just append new rows to this table every single time we fire our test suite, which isn't a good idea. You never want to end up in situations where one test is working with a different database structure or contents than the test that preceded it. That's how we end up with things like false positives or false negatives. Okay, so when we affect the Update TestCase Inheritance 0:50working with a different database structure or contents than the test that preceded it. That's how we end up with things like false positives or false negatives. Okay, so when we affect the database, if we want to roll it back after each test, how do we do it? Well, the integrated package makes it pretty easy, since it's so common. All we have to do for the Laravel extension is import a trait. But before we do that, one really quick thing. If we run our test suite right now, we'll see an error like this. And the reason is because there was an update to the integrated package between episode two and this video that I'm recording right now. Things move kind of fast, guys, but it's just a small one. All we have to do is, rather than extending this from our example test, well, it'd be easier if we could just have that done from our test case. That way, if you wanted to, you could also add helper methods to this file. So that's what we'll do. Right here, Using DatabaseTransactions Trait 1:39test, well, it'd be easier if we could just have that done from our test case. That way, if you wanted to, you could also add helper methods to this file. So that's what we'll do. Right here, extends integration test. Okay, now from our example test, we can remove that and return it to how it usually is in a Laravel application. Okay, so now if we give this another run, we should get green, and we do. So just a little change there. Okay, so what's next? Well, we want to pull in this trait. Use database transactions. And I will import that. Here's the full path. Now, if we were to take a look at it, let's see. You'll find that we are using annotations here to trigger them during the setup process for PHP unit and the teardown process. So here we're saying for Laravel's DB facade, this is the equivalent of this, by the way. We're going to begin a new transaction. And then once the test finishes and we're cleaning things up, we're going to roll it back, which essentially resets or Verifying Transaction Rollbacks 2:38this is the equivalent of this, by the way. We're going to begin a new transaction. And then once the test finishes and we're cleaning things up, we're going to roll it back, which essentially resets or discards everything. So that should be all we need to do here. But real quick, just to prove it to you, I'm going to temporarily comment this out. And if we return to our database, I'm going to delete from posts. And now I'm going to run PHP unit a handful of times. And you'll see that, well, before we would just append to this table over and over. But now we'll clear that out. We will import the traits, and that's all we had to do. Let's run it again a bunch of times. And now if we do another select star, it will be empty because we've specified that we want to use database transactions. Now, further, if you find that this is something you'll do in a lot of your test classes, well, you don't have to import it every time. You could place it within the test Apply Trait Globally 3:31database transactions. Now, further, if you find that this is something you'll do in a lot of your test classes, well, you don't have to import it every time. You could place it within the test case class right here. We will pull it in and then use database transactions. So now we can return this to like we had before. Very clean, very simple. But still, if we run this a bunch of times, it will still take effect. Pretty cool.