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

New Coverage Test Options0:00

Next up, in Laravel 9, you can now generate a code coverage report as part of the php artisan test command. Let's have a look. php artisan test. And of course, we get green with the standard formatting, and this is true for Laravel 8 as well. But if we view the help, you'll find two new options, coverage and min. So of course, coverage indicates whether code coverage should be included, and the min option would be if you want to set a minimum percentage of code coverage that is necessary in order for the test to be considered green. That's basically what it does.

Missing Xdebug Error0:31

that is necessary in order for the test to be considered green. That's basically what it does. But yeah, if you don't have something like xdebug installed and you run this, you're going to get kind of a confusing error. They might want to make this a little more clear. Code coverage driver not available. And again, that's because you need something like xdebug installed. So I'm on a Mac here. We could use something like Homebrew and PECL to take care of that. Let's see if I can do this quickly.

Installing Xdebug0:54

We could use something like Homebrew and Peckle to take care of that. Let's see if I can do this quickly. Peckle install xdebug. All right, and I think we're okay there. Now, if I open a new tab and run php -v, though, I don't see any reference to xdebug. So let's do this. php.ini. And here's our loaded configuration file. And you can see we have some additional configuration files for OPCache or Redis, but none for xdebug. So let's do that now.

Configuring Xdebug INI1:21

but none for xdebug. So let's do that now. I'm going to touch a file here, and they recommend you call it 99-xdebug.ini. Okay, so if we run this again, of course it's empty, but it has been discovered. All right, so let's open this up. And we'll sed send extension equals xdebug. And that's all we need to do there. All right, now we need to restart our web server. I use Laravel Valet, so it should be as simple as valet restart. And yeah, notice we did restart php.

Enabling Coverage Mode1:49

I use Laravel Valet, so it should be as simple as valet restart. And yeah, notice we did restart php. Okay, so let's run it again, and now notice it does include xdebug support. All right, let's give it a shot. So php artisan test --coverage, and we still get one more error, but this time we have some more feedback. Did you set xdebug's coverage mode? What sort of coverage do we want? Well, we can do this in two ways. We can update the php.ini file that we were just working on,

Well, we can do this in two ways. We can update the php.ini file that we were just working on, or we could declare the coverage mode as an environment variable, doing xdebug_mode=, and then you provide the mode you want. And, of course, you can visit the xdebug website to review all of the modes that you can turn on, and you can even use a comma-separated list for multiple ones. So in our case, we want to generate code coverage reports, so I will use this key here, paste that in, and then run our command. So yeah, this part at the beginning, we are setting an environment variable.

Coverage Threshold and Wrap-Up3:11

whereas before, it passed. So that's something you might want to turn on if you want to be fairly strict about achieving a certain code coverage threshold. All right, and that's it. It took a little bit of setup work if you don't have xdebug installed already, but once you do, in Laravel 9, you can now generate a code coverage report in seconds.

Install XdebugGenerate Code Coverage Report

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