Learn how to configure XDebug and PHPUnit in PHPStorm, allowing you to write better tests and fix bugs faster.
Test Driven Development (TDD) is an old topic, I know, but it seems many people don’t understand how it makes you write better code. The point here is that one of the most important benefits it’s to use debugging tools together with tests, making your flow more efficient.
I started using TDD on the command line, and still use it sometimes, but since I started using PHPStorm and decided to try how it handles tests, and that’s amazing! When you add debugging tools – like XDebug – to it everything starts making sense, then you have the feeling you’re on the right path.
PHPStorm has a dedicated interface to run and debug tests, almost in the same window, what makes the process of writing code safer and easier.
修改PHP配置文件 php.ini Xdebug zendextension=C:webserverserverphp-5.6.9extphpxdebug-2.3.3-5.6-vc11-x8664.dll;允许远程IDE调试 xdebug.remoteenable=on;远程IDE所在HOST和PORT xdebug.remotehost=localhost xdebug.remoteport=9000 xdebug.remotehandler=dbgp;开启远程调试自动启动 xdebug.remoteautostart = Off;可以是任意Key,这里设定为PHPSTORM xdebug. For example index.php; After that edit the configuration: Choose a PHP Web Page (On old PHPStorm version it might be called PHP Web Application) on the drop down after you click the green plus. Edit the Configuration Name and add new server. Add new server with Host = localhost, on port 80 with Xdebug.
I’m not gonna teach you how to write tests and even how TDD is good. I’m assuming you already have some tests written and just want to run them in PHPStorm, debugging with XDebug. I’m gonna use the tests from my open source project Corcel.
PHPStorm
Configuring XDebug
First let’s configure XDebug in PHPStorm. We’re assuming here you already have the xdebug
PHP extension installed. In my case, I’m using Laravel Valet, and it runs on the port 9000, the same port XDebug runs by default. So I had to update my php.ini
file to change its port to 9001. My config file is located at /usr/local/etc/php/7.2/conf.d/ext-xdebug.ini
:
When type php -v
in the command line I can saw the XDebug extension enabled:
Then, in PHPStorm (I’m using currently version 2017.3), go to Preferences
and Languages & Frameworks -> PHP -> Debug
. Configure your XDebug port and uncheck some pre-checked options, just to avoid creating unnecessaries break points.
Configuring PHPUnit
First we must tell PHPStorm which PHP version we’re using and where is that PHP binary. This is necessary for code checks in the IDE and for running PHPUnit. Just set that in your IDE’s preferences window. You should set something like this:
Now go to Run -> Edit Configurations
. Here we’re going to create a new configuration related to PHPUnit
, give it the phpuni
name and say PHPStorm we’d like to use the configurations in our phpunit.xml
file.
Running Tests
Now let’s run all tests Corcel has. Go to Run -> Run
and then select phpunit
. This is the name we gave to the configuration we just created. You’ll see a new tab on the bottom of the window with all your tests running:
Running a Single Test
In this case we run all 139 tests. The point here is you can run just one test case or even the last one. In this new tab if you click on a single test case in the left sidebar and Control + Shift + R
you’ll run just that specific test. The same can be used when you’re inside a class and want to run just one test/method. Inside any part of that method, if you press this shortcut you’ll run that test case. If you press the shortcut outside a test case method you’ll run tests for that specific class, all them.
Running the Last Test
If you’re fixing a bug in another class, not the test one, and you want to run that test again to see if it’s passing now. You can press Control + R
shortcut. This tells PHPStorm to run the last run test, only. Then you don’t have to change the current file you are to run the last test case. Very useful!
Just to remember, Control + Shift + R
to run the current test case you are, and Control + R
to run the last run test case.
Debugging while Running Tests
The point here is you enabled XDebug. So you can debug while testing. Let’s take a simple test case from Corcel:
I’m gonna add a break point inside the $comment->isApproved()
method, like this:
New Shortcuts to Debug
You know the shortcuts to run a single current test case (Control + Shift + R
) and the last run test case (Control + R
). If you to go the test case source code and run it you will not stop anywhere, because you’re running the test only.
To run tests with debugging support use Control + Shift + D
for the single test case and Control + D
for the last one, just replacing R
by D
.
If you run the same test with debugging support you’ll stop on that breakpoint, and then the magic starts happening. You will get a lot of information at that specific point, like current variables content and even continuing the executing step by step. You’ll get all that in the same tab you saw your tests running:
Then, debug your code. You have to useful commands/button to press. Here are some examples:
Conclusion
TDD and Debugging are two important steps in development. Once you start using them you cannot stop, but for sure, you’re writing better and safer code, believe me.
I hope this post helped you to start with testing and debugging in PHPStorm and made you feel excited about start using that. If you want to use a project to start testing and debugging you can clone Corcel on your machine and start running its tests in PHPStorm.
Besides debugging the entire application, you can debug separate HTTP Requests. This is helpful when you are actually interested in a specific page that is accessed in a number of steps, but for this or that reason you cannot specify this page as the start page for debugging, for example, because you need to 'come' to this page with certain data.
To debug PHP HTTP requests in PhpStorm, you can use the following methods:
Compose and debug the request via the HTTP client in the code editor, which is the recommended approach.
Use the PHP HTTP Request run configuration. Based on the configuration settings, PhpStorm composes the request to run.
Prepare the debugging engine
Before you start debugging, make sure that you have a debugging engine installed and configured properly. PhpStorm supports debugging with two most popular tools: Xdebug and Zend Debugger. These tools cannot be used simultaneously because they block each other. To avoid this problem, you need to update the corresponding sections in the php.ini file as described in Configure Xdebug and Configure Zend Debugger.
Open the active php.ini file in the editor:
In the Settings/Preferences dialog Ctrl+Alt+S, click PHP.
On the PHP page that opens, click next to the CLI Interpreter field.
In the CLI Interpreters dialog that opens, the Configuration file read-only field shows the path to the active php.ini file. Click Open in Editor.
Set the breakpoints
Breakpoints are source code markers used to trigger actions during a debugging session. Typically, the purpose behind setting a breakpoint is to suspend program execution to allow you to examine program data. However, PhpStorm can use breakpoints as triggers for a variety of different actions. Breakpoints can be set at any time during the debugging process. Your breakpoints don't affect your source files directly, but the breakpoints and their settings are saved with your PhpStorm project so you can reuse them across debugging sessions.
Place the caret at the desired line of the source code.
Breakpoints can be set in the PHP context inside php, html, and files of other types. Line breakpoints can be set only on executable lines, but not on comments, declarations, or empty lines.
Do one of the following:
Click the gutter area at a line where you want to toggle a breakpoint.
From the main menu, choose Run | Toggle Line Breakpoint.
Press Ctrl+F8.
Debug the request via the HTTP client in the code editor
Using the built-in HTTP Client, you can compose, execute, and debug HTTP requests directly from the PhpStorm code editor.
Open an existing HTTP request file, or create a new one: in the File menu, point to New, and then click HTTP Request.
Compose an HTTP request for the query that you need to debug.
Position the caret at the request and press Alt+Enter or click in the editor gutter. From the popup menu, select PHP Debug <host>.
If you have environments defined, select PHP Debug with ... and choose the environment in the popup menu. The selected environment will be used as the default one when executing or debugging the request later.
PhpStorm will automatically add the
XDEBUG_SESSION
cookie to the request, execute it, and stop at the specified breakpoint.
When a request is executed, PhpStorm automatically creates a dedicated temporary HTTP Request run/debug configuration for it. You can save it as a permanent run/debug configuration if necessary.
Install Xdebug Phpstorm Ubuntu 18.04
Press Shift+F10 to run or Shift+F9 to debug the corresponding saved run/debug configuration at any moment without the need to open the request file in the editor. This can be useful if you are working on the web service endpoint implementation in a .php file and want to quickly test it by sending an HTTP request.
Create a debug configuration of the type PHP HTTP Request
PhpStorm comprises the settings specified in this configuration into a PHP HTTP request. Note that using HTTP Client in editor for debugging HTTP requests is a more convenient and recommended approach.
Install Xdebug Phpstorm Windows
Open the Run/Debug Configuration dialog by doing one of the following:
From the main menu, choose Run | Edit Configurations.
Press Alt+Shift+F10, then press 0 to display the Edit Configuration dialog or select the configuration from the popup and press F4.
Click on the toolbar or press Insert. From the list, select the PHP HTTP Request configuration type. The PHP HTTP Request dialog opens.
Specify the configuration name.
In the Server list, specify the debug server configuration to interact with the Web server where the application is executed. Select one of the existing configurations or click Browse and define a debug server configuration in the Servers dialog that opens as described in Create a PHP debug server configuration.
In the URL field, complete the
host
element of the request to debug. Type the path relative to the host specified in the debug server configuration. As you type, PhpStorm composes the URL address on-the-fly and displays it below the field.Specify whether you want to bring any data to the target page. From the Request method list, choose the relevant request type:
To access the page without bringing any data, choose GET.
To access the page with some data saved in variables, choose POST and type the relevant variables in the Request body field.
By default, the Project Encoding is used in requests' encoding if it is not specified explicitly, for example:
header('Content-type: text/html;charset=utf-8');The Project Encoding is specified on the File Encodings page of the Settings/Preferences dialog Ctrl+Alt+S.
In the Query field, type the query string of the request. This string will be appended to the request after the ? symbol.
Click OK, when ready.
Initiate a debugging session and examine the suspended program
To start debugging, click the Debug button on the toolbar.
As soon as the debugger suspends on reaching the first breakpoint, examine the application by analyzing frames. A frame corresponds to an active method or function call and stores the local variables of the called method or function, the arguments to it, and the code context that enables expression evaluation. All currently active frames are displayed on the Frames pane of the Debug tool window, where you can switch between them and analyze the information stored therein in the Variables and Watches panes. For more details, see the section Examining a Suspended Program.
Continue running the program and examine its frames as soon as it is suspended again.
To control the program execution manually, step through the code using the commands under the Run menu or toolbar buttons: Step IntoF7, Step OutShift+F8, Step OverF8, and others. For more details, see Step through the program.
To have the program run automatically up to the next breakpoint, resume the session by choosing Run | Debugging Actions | Resume Program or pressing F9.