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

Testing Hue API Call0:00

Okay, let's build a Laravel app where we can hook into different PHP unit events and blink our lights red when our tests fail. So before we do that, let's see if we can get the API working using the HTTP clients. So let's just go into routes/web.php and let's just use the client here and just make a PUT request. And let's grab the endpoint from the last video. So this endpoint right here, and let's paste that in. And the second program is the hue, saturation, and brightness. So on hue, saturation, and brightness. So on, we'll just hard code it to true to see if it works.

So on hue, saturation, and brightness. So on, we'll just hard code it to true to see if it works. Oops, this is an array. So true, hue, and let's just make it red, so 65, 535. Brightness is going to be 255, or sorry, saturation is 255, and brightness is 20, okay? So let's see if this works. So let me just fix this error and see if this works. So if I hit this route, it should turn our lights red, and it does, cool. So now we have to hook into different phpunit events. So for example, when our tests fail.

Reviewing PHPUnit Event Hooks1:26

So now we have to hook into different php unit events. So for example, when our tests fail. So I have an example test here, and I have a failing test here. So if we run phpunit, we'll see a fail. So we want to hook into this event when we detect a failure. So after some searching, I found this page, which is the documentation page for phpunit. And you can see that you can extend phpunit, and you can listen for these events. So these hooks right here. So the one we're interested in is after test failure hook, or you can hook into these other ones as well, like errors or warnings.

Configuring PHPUnit Extension2:00

So the one we're interested in is after testFailure hook, or you can hook into these other ones as well, like errors or warnings. So if you scroll down even more, you'll see how to use this. So we have to add this extensions tag in our phpunit.xml, and then give it a class. And then there's an example class here, which implements those hooks and has methods that can run whenever those hooks are detected. So let's grab this, and we don't need arguments. So these arguments get passed into the constructor if you have arguments, but we don't. So we'll just get rid of that. So let's go to phpunit.xml, and let's add the extensions here, yeah, here, after php.

So we'll just get rid of that. So let's go to phpunit.xml, and let's add the extensions here, yeah, here, after php. And let's just indent that. And let's get rid of this first one. And we don't need the arguments. And that should be good. And instead of the vendor folder, we're going to put it in this test folder. So let's just say test. And you can name the class whatever you want, but I'll just stick to this name here. So let's go ahead and make that.

And you can name the class whatever you want, but I'll just stick to this name here. So let's go ahead and make that. So new file, myConfigurableExtension.php. And let's go ahead and grab that boilerplate code from here. So I just need a class here, actually describe the whole thing. And space it in. And let's make some changes at the top here. So let's say namespace is now Test or Tests. And we don't need these instance variables, and we don't need the arguments in the constructor. So let's get rid of those.

And we don't need these instance variables, and we don't need the arguments in the constructor. So let's get rid of those. And I guess we don't need the constructor at all. So let's just leave that blank. And what's this complaining about? And I guess we don't need that because I deleted the instance variables. So let's get rid of this entirely. So now since we implemented beforeFirstHook and afterLastTestHook, we should be able to hook into those events. So if I just echo beforeFirstTest, then we should see this when we execute our tests.

to hook into those events. So if I just echo before firstTest, then we should see this when we execute our tests. So let's try this. See if we get this when we run phpunit. So try that again. Sorry, my folder name is incorrect in my phpunit.xml. So let's change it to tests with an S. And let's try that again. And if you look here, you'll see that that echo did show. So let's look at the other hooks that we're interested in. So back to the documentation.

Implementing AfterTestFailure Hook4:50

So let's look at the other hooks that we're interested in. So back to the documentation. So the one we're interested in is after test failure hook. So let's grab this. And let's go back to our class. Let's implement this. And let's import that. And we have to follow this format. So execute. Let's make a new method.

So execute. Let's make a new method. Execute. After test, failure. Test. Okay. And let's just remove this or copy this, comment this out, and put it in here and see if we get the same thing. So let's say after. Test failure.

So let's say after. Test failure. Okay, so let's see if we get this when we execute our tests. And I believe I have an error because I'm not following the correct signature. So let's go into afterTestFailure hook. And yes, we need these parameters. So let's grab these. And I don't think we need the word test here. And let's put that in our params. And it's still complaining.

And let's put that in our params. And it's still complaining. Let's see. I think it wants the return type too. So let's add void to the end of that. Okay. Okay, so now the squiggly is gone. And let's try running this again. Okay, so now it looks like it works. There we go.

Blinking Lights on Failure6:21

Okay, so now it looks like it works. There we go. After test failure. So now we just have to blink our lights in here, because this is where it's detecting a failed test. So if we go into our routes file and grab that code, this should work. So let's grab this. And actually, I'm going to make a method here that just toggles the lights. So this toggleLights. And I'm going to pass it a boolean to turn it off or on.

So this toggle lights. And I'm going to pass it a boolean to turn it off or on. And that's how we're going to blink our lights. So let's make another method, say toggleLights, say on. And here's where we can paste that API call for our lights. And make sure to import HTTP. Okay. And this should be this param coming in. And let's just see if the lights turn off after this runs. So let's give this another run.

This is true. This is false. This is true. So that should blink our lights. So let's try that again. Let's get rid of this echo now. Oops. PHPUnit. And there you see our light blinking. Cool.

Turning Green on Success8:58

And then we can base it based off of that hook. So let's add that hook. After last test hook, let's import it. And it's already imported. And let's take a look at that method signature. Okay. And let's just grab this. And paste it underneath here. Oh, I guess it was right here. So yeah, we can just grab this.

I have this twice. That's why it was underlined the first time. I'm sure you saw that. Let's try again. php unit. Okay, so it failed. But then you do see it turn green now. So all we can do is just have a piece of state here, say protected, say $hasError equals false by default. And if it gets here, so there's a failure.

false by default. And if it gets here, so there's a failure. So we can just set that to true. So this has error equals true. And then we can just wrap this code in a conditional to check. So we only want it to turn green if there is no error. So if not, this has error, then run this code. So let's just move that up. Okay. And now this should work.

Okay. And now this should work. So now there is an error in the test. So it should blink red and stay red. So let's try that. Okay, so it's blinking red. And it stays red. But now let's force our tests to pass. So back to our example test right here, let's just say assertTrue(true). And now it should be green.

So back to our example test right here, let's just say assert true is true. And now it should be green. And it is cool. So there you have it. We successfully communicated with the Philips Hue API to blink our lights red when our tests fail and green when our tests pass again, not the most practical thing. But I just want to share my nerdy enthusiasm with you and build something cool. If you have any other cool integration ideas with the smart lights, definitely let me know so I can continue to tinker around with the API.

so I can continue to tinker around with the API.

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