Testing Setup Overview0:00
Next, let's figure out what our testing workflow might look like within phpStorm. Alright, so for this lesson, I will once again be working in the Laracast codebase, and specifically, we will focus on these two files. So one is a Chat Eloquent model that tracks a chat with what we call Larry, but what is actually the OpenAI API, so we can send messages back and forth with OpenAI, and then I have a corresponding ChatTest that you can see here, and yeah, you'll notice I'm using Pest.php here, which I really like. Alright, so as we go over this, don't worry about the inner code here, it doesn't matter, it's not really relevant to this lesson. But yeah, really quickly, to give you an idea, well, it starts a new chat with OpenAI, alright?
it's not really relevant to this lesson. But yeah, really quickly, to give you an idea, well, it starts a new chat with OpenAI, alright? Well, here's my API. I have this model called Chat, I call a method say, and then I send through a message, and then I perform my assertions. Well, when we do that, I expect there to be a response, and you'll see up here I'm actually faking that response so that I'm not continuously pinging the OpenAI API, and then I assert that those messages are thrown into the database. Alright, you get the idea. So why don't we go ahead and run all of the tests within this file?
Running Tests in Terminal1:13
Alright, you get the idea. So why don't we go ahead and run all of the tests within this file? And once again, I will open up the terminal here. We'll say pest on the tests/Unit/ChatTest.php file, and we'll give that just a minute. And actually, I can hit Shift Command Quote on the Mac to expand this. And uh-oh, it looks like everything's passing except one. And clearly, that's the one we're going to work on in this lesson. Alright, so let's have a look. It archives messages that are a month old. Failed asserting that 3 is identical to 2.
Investigating the Failing Test1:40
It archives messages that are a month old. Failed asserting that three is identical to two. Alright, so let's have a look. Right down here. Yeah, this is the one. Okay, let's have a look. It archives messages that are a month old, and it looks like we whip up three dummy messages in the database. And notice that, well, this one was created now, this one was created a week ago, and this one was created over a month ago.
And notice that, well, this one was created now, this one was created a week ago, and this one was created over a month ago. Okay, so at that point, of course, there should be three messages in my database table. But then I have this method called purge, and when we call that method, well, as a result, the chat messages should be reduced to two, and this especially old one that's older than a month should have been erased. So if we try to track that down, yeah, we're going to get null. It doesn't exist anymore. Okay, so let's try this. Well, first, if I only want to execute a single test, I can add a call to this only method.
Installing Pest PhpStorm Plugin2:29
Okay, so let's try this. Well, first, if I only want to execute a single test, I can add a call to this only method. And then, once again, if I bring back the terminal and we run that test again, you'll see that it ignores all the other tests in the file except for that one. Yep, we only ran a single test. All right, so that's useful, but phpStorm can help us out a good deal here. So I'm going to close out the terminal, and instead, well, first, I'm not using phpUnit, I'm using PEST. So why don't we pull in a dedicated plugin for PEST? So once again, command , to open up my settings.
So why don't we pull in a dedicated plugin for PEST? So once again, command , to open up my settings. We'll go into Plugins, into the Marketplace, and I will search for PEST. All right, and it looks like this is the one here. So let's install it and restart. All right, and I immediately see some changes here. So first up, there's a play icon next to every test. So of course, that allows me to trigger or play that single test. And then also, we have some squigglies here that provide suggestions. So notice right here, multiple expects can be converted into chainable calls.
All right, it's giving it a run, and let's see how it does. Yeah, of course it fails. We'd expect that. But now we're triggering the test from directly within PhpStorm, which means I can get rid of this only call here. All right, so now whenever I'm working on a test, I can click on the Play icon, and actually notice because the test failed, we also have a little more feedback about the failure. Okay, so I can run it again, or notice on my Mac, it's currently bound to Control-Shift-R. So I can put the cursor anywhere within this test body and hit Control-Shift-R, and now
Okay, so I can run it again, or notice on my Mac, it's currently bound to Control-Shift-R. So I can put the cursor anywhere within this test body and hit Control-Shift-R, and now we're running the test. Okay, cool. So, of course, the next step at this point is, well, let's make the test pass. So to do that, I'm going to switch over to my Chat model. And it looks like I already have this method set up, so I can Command-Click to go directly to that implementation. And yeah, notice it doesn't do anything. So of course the test is failing.
Rerunning Tests Efficiently5:11
And yeah, notice it doesn't do anything. So of course the test is failing. All right, so yeah, here's something you will often run into. As you are working on the implementation, you will often want to go back and rerun the test. But currently, it's a little annoying to, you know, just as an example, do something like this. And then, well, let's go back to ChatTest and then hit the play icon or the keyboard shortcut again. So of course, there's a way to rerun the previous test.
And it looks like it's the one on top. All right, hit Return. And now from anywhere in the code base, I can rerun this single test. But we can even do better. So let's close this out, bring back settings, and I will look in my keyMap. And let's see if we can bind that. So again, something like rerun. And let's see, which one do we want? Well, this is going to rerun all tests that failed during the previous run. And maybe that's actually what you want.
And like I said, I like Command-T, but make up your own mind. So we will apply that. Yeah, let's give it a shot. So we'll start from scratch. Imagine you're working on a test. Anywhere within a test, I can now hit Command-T to run my test. Okay, and of course, it's going to fail. Let's make it work now. Command-click on Purge, and we will start on it, and we're going to do something that doesn't work.
Command-click on Purge, and we will start on it, and we're going to do something that doesn't work. We're just going to create a variable that we never use. But now I want to rerun the test, so I can hit Shift-Command-T, all right? And now it reruns the previously failed test, and in this case, there's only one. Okay, cool. So now we can actually work on an actual implementation. And yeah, in the previous episode, we learned about leveraging OpenAI and GitHub Copilot. So within the context of a Chat class that interacts with OpenAI, it only makes sense that we leverage GitHub Copilot and OpenAI.
Implementing Purge with Copilot8:02
So within the context of a Chat class that interacts with OpenAI, it only makes sense that we leverage GitHub Copilot and OpenAI. Okay, so let's just comment out what we want to do. Already, it's giving me an idea, and yeah, it's kind of amazing. That's exactly what I want. It's ridiculous how good it is. Delete all, and let's just tweak it a little bit. Delete all chat messages that are at least 30 days old. All right, let's see what it comes up with. ChatMessage, yeah, that's the corresponding model I have.
All right, let's see what it comes up with. Chat message, yeah, that's the corresponding model I have. Where createdAt is effectively a month ago. Call delete. All right, I think that'll fix it, actually. So let's run it, Shift-Command-T. Give that just a second, and yeah, we get green. Okay, but now we do have one little issue. We bound Shift-Command-T to rerun failed tests from the previous run. But yeah, what if I want to tweak a currently passing test? Something like this.
It might be a situation where because we're calling it so quickly, now sub week within our test and our purge are identical. So yeah, let's do less than or equal. Rerun the test. Okay, yeah, that was the issue. Not a big deal, and it still works. All right, and that does it. So yeah, mostly that's going to be your workflow when running your tests within PHPStorm.
