HOW TO SET UP PHP DEBUGGING WITH PHPSTORM, XDEBUG, AND PHPUNIT

Sun, Nov 3, 2019.Filed Under:Software Development.Estimated reading time: 8 minutes.by Matthew Setter.

Bạn đang xem: How to set up php debugging with phpstorm, xdebug, and phpunit


Still using var_dump lớn debug your PHPhường code? Stop! While var_dump can be convenient, it’s a very blunt approach. In this article, I’ll show you how to mix up proper debugging with PhpStorm, Xdebug, and PHPUnit, và give you a modern, sophisticated debugging experience.

Recently, I’ve been developing a side-project, và have had lớn create several dependent packages khổng lồ support it.These dependent packages perkhung several smaller tasks, ones that don’t belong in the main package.

One example is a package called currency-filter.Its task is to lớn filter a European-format currency string, such as -1.432.156,54 inkhổng lồ its integer equivalent.In this case, it would be -143215654.

Not such a tricky request, you might think.But the more test data I fed the code, the more use cases I found which needed khổng lồ be handled.Have a look at the project’s tests for a full danh sách, but here’s a short menu.

1.00,00-3500,00-3.500,01-4.501,21.887.761.2341.887.761.234,011.887.761.234,21887761234,21887761234,211887761234,218877612341234.24

In the early stages of development, in some cases, the tests validated that the expected values were being returned.However, in others, the values that I expected khổng lồ be returned, weren’t.Instinctively (wrongly) I started using PHP’s var_dump method lớn attempt khổng lồ see what the value being returned was.

Well, if you’ve ever tried khổng lồ var_dump a variable in the middle of a PHPUnit thử nghiệm, then you’ll know that PHPUnit doesn’t take kindly to that.Not only did I not see the value that I wanted, I saw an unwieldy staông chồng trace instead.

So, what’s a man to lớn vày.I needed lớn know what was being returned, but the ham-fisted way I was attempting khổng lồ find it wasn’t working.The solution was khổng lồ use use step-through debugging in PhpStorm.

If you’re not familiar with step-through debugging, it’s where you, as the name implies, step through your code, one line at a time.

Setting Up Step-Through Debugging in PHP

Gladly, khổng lồ mix it up in PHP.., you don’t need a lot.All you need are three things:

PHPhường with XdebugPHPUnit; and

Whenever I write PHP.. code, it’s reasonably safe to say that I use PhpStorm.So that’s why, in this article, I’m going to show you how to integrate Xdebug, PHPUnit, và PhpStorm.

On Small Caveat

This article doesn’t cover debugging web-based applications with PhpStorm, but smaller code libraries.Getting (remote) debugging setup for web-based applications can be a finicky và intricate affair at times, depending on the structure of your development environment.

What You’ll Need

You won’t need very much to follow along with this tutorial, just the following:

A recent release of PhpStorm (I’m using 2019.2.3)An installation of PHPhường. (I’m using 7.3.10)A package manager (such as Apt on Linux, Homebrew on macOS, or Chocolatey on Windows)About 10 minutes of your time
Learn the Mezzio FrameworkMezzio Essentials teaches you the fundamentals of PHP"s Mezzio framework — the fundamentals that you need — lớn begin building get paid for applications with the Mezzio framework right away. It’s a practical, hands-on approach, which shows you just enough of about the underlying principles & concepts before stepping you through the process of creating an application.Learn More

Install and Configure Xdebug

The first thing that we need to lớn do is lớn add Xdebug support to lớn our PHPhường binary.To bởi vì that, I strongly encourage you khổng lồ use the package manager on your Linux distribution, or a package manager for your operating system, such as Homebrew on macOS.

For the examples in this article, I’m going to assume that you’re running a Debian-based Linux distribution, such as Debian, Ubuntu, or Linux Mint.However, please adapt them to your package manager of choice.

To install Xdebug, run the following command:

subởi apt-get install -y php-xdebugThen, once Xdebug is installed, you need to lớn ensure that it’s both enabled & properly configured.The package manager will most likely enable it, but lớn be sure run the following comm&.

php -m | grep -i xdebugIf you see “Xdebug” in the output, then it’s installed and enabled.Now we need lớn configure it properly.To bởi vì that, we need to ensure that remote_enable is enabled.

First, we need to find the relevant configuration tệp tin.We can find it by running the following command.

php --ini | grep xdebugThis should output something lượt thích /etc/php/7.3/cli/conf.d/20-xdebug.ini to lớn the console.xuất hiện that tệp tin in your favourite editor of choice, và add the following line:

xdebug.remote_enable=1Save sầu the file, & then kiểm tra that the setting’s taken effect, by running the following command:

php --ri xdebug | grep remote_enableYou should see xdebug.remote_enable => On => On printed lớn the console if the setting’s taken effect.

Configuring PhpStorm’s PHPhường Setup

Now that Xdebug’s installed và configured, we need to lớn configure PhpStorm.To vì chưng that, in PhpStorm’s settings, under “Languages & Frameworks -> PHP”, clichồng on the More button, next khổng lồ the CLI Interpreter dropdown menu, which you can see an example of in the screenshot below.

Xem thêm: Tải Game Vxp Free - Cửa Hàng Game & Apps

PhpStorm’s PHP Interpreter Settings

That will show you the list of configured PHPhường interpreters.Select your version of PHP in the left-h& column.Next, ensure that “Xdebug” is written next khổng lồ “Debugger”, as in the screenshot below.

PhpStorm’s CLI Interpreter settings window

If it is, then PhpStorm is working with a PHP binary that has Xdebug tư vấn available.

Configuring PhpStorm’s PHPUnit Support

Now, we need lớn ensure that PhpStorm has PHPUnit integration set up correctly.To bởi that, open PhpStorm’s settings dialog và filter by either “phpunit”, or navigate khổng lồ “Languages & Frameworks -> PHPhường -> Test Frameworks”.

If there are no configurations listed in the middle column, cliông chồng on the plus button khổng lồ add one.In the popup that appears, choose “PHPUnit Local”.Next, we need to lớn configure how PHPUnit is available on your system.

There are three choices:

Use Composer autoloaderPath to phpunit.pharLoad from include path (PEAR)

I commonly include PHPUnit on a per-project basis via Composer, as a development requirement.So that’s the option I’m going khổng lồ step through in this example.Given that, I’ll choose “Use Composer autoloader”.

After doing that, clichồng the folder on the far right-hvà side of “Path lớn script:”.In the file browser dialog that appears, find & select Composer’s autoloader (vendor/autoload.php) in your project.After doing so, if you cliông chồng the reload ibé, next to the autoloader path, you should see a green confirmation message, telling you that the PHPUnit version information has been successfully updated.Also, you’ll see the version of PHPUnit printed below the path, next to lớn “PHPUnit version”.

With that done, cliông xã “OK” to cthua the settings window.

Set One Or More Breakpoints In Your Project

Now that we’ve sầu completed all of the required setup và configuration, it’s time khổng lồ phối one or more breakpoints in our code và start the debugger.In the screenshot below, you can see that I’ve sầu mix a breakpoint on an arbitrary line in one of the source files of my project.

Setting a code breakpoint in PhpStorm

To bởi vì that in your project, cliông chồng on the tệp tin gutter, on the line that you want khổng lồ mix a breakpoint.If you cliông xã the right area, a red icon will appear, indicating that you have sầu phối a breakpoint, and the line will also be highlighted in red (allowing for your theme).To remove sầu it, clichồng that spot again.

Start Step-Through Debugging

Now, you’re ready to start the step-through debugger.There are multiple ways khổng lồ vì chưng this; however, my preferred approaches are:

Clicking the arrow next to the name of the test that you want to lớn run, and clicking the second option, starting with “Debug”Right-clicking the desired test file và clicking the option starting with “Debug” (most of the way down the list)Opening the Debug panel và clicking the debug button.

Piông chồng the way that you prefer và launch the debugger.

Depending on the speed of your development machine, the debugger will stop at the breakpoint and các mục the execution path khổng lồ that line in your code in the Debug panel’s Frames window and menu one or more variables in the Variables window.In the example below, you can see that I have sầu seven variables listed in the Variables window.

Stopping on a breakpoint during step-through debugging in PhpStorm

In particular, you can see the value of the variable being passed khổng lồ the method that I’m debugging $filter->filter(), the value that it’s being compared with $validData.However, what it doesn’t have sầu is the return value of that function.To add that, I’ll select the function Call, right-cliông chồng, và select “Add lớn watches”.That then adds the function hotline to lớn the Variables window and shows the value that it will return.

From here, several other options are available.These are:

Show Execution PointThis navigates you to lớn the code that is currently executing.Step OverExecutes the next line, jumps khổng lồ the line after và pauses.Step IntoIf the next line of the code is a function Gọi, the code pauses at the first line of the function.Step OutThis executes the remainder of the current function & returns you to the caller, và pauses.Run to CursorThis will exedễ thương all the code between the current breakpoint và where the cursor has been placed.Evaluate ExpressionThis opens a dialog that allows you khổng lồ evaluate a custom expression, based on the current code state.Force Step OverThis enables you to jump over the method call, ignoring the breakpoints.Force Run to CursorThis enables you lớn jump to lớn the cursor position while ignoring existing breakpoints on the way.

In Conclusion

And that’s how to lớn thiết lập PHP.. debugging with PhpStorm, Xdebug, and PHPUnit.Sure, there are several steps involved, but they’re more or less straight-forward, và logical.

Secondly, they shouldn’t take much time to complete, perhaps 5 - 10 minutes.Plus, when they’re done, they make knowing, with precision, exactly what your code is doing at any moment in time effectively trivial, whether you’re executing code directly, or testing it with PHPUnit-based unit tests.

What’s more, you can stop using the large hammer that is var_dump, & avoid littering your code with such statements that you later have lớn remove.