Phpstorm Phpunit



PhpStorm can be setup to use Docker. Thanks to Gary Hockin’s excellent YouTube video Running PHPUnit Tests in PhpStorm with Docker, the setup process can be easily replicated. There is a four stage process: Configure PhpStorm to use Docker. All done, you are ready to run tests directly in PHPStorm. After completing the setup you can now run tests in PHPStorm. If everything is set up correctly you can for example: Right click a phpunit.xml config file and choose Run; Open a test file and run the file or a single test by clicking the green arrow icon; Debugging Tests.

PhpstormFeaturesPhpStorm

Since version 3.7, PHPUnit allows us to install the test runner and optional dependencies using Composer. With PhpStorm 6, this workflow is now supported from the IDE.

Using both the bundled Composer support and PhpStorm’s unit testing support, we can install PHPUnit via Composer without any hassle. Let’s find out how.

Let’s start off with a blank project. We can right-click the project and use the Composer | Init Composer… context menu to enable Composer support for our project. If you don’t have composer.phar anywhere on your system, the Click here to download from getcomposer.org helper will download the latest composer.phar from the official website.

Once initialized, we can use the new PhpStorm 6 Composer support to add dependencies. We can right-click the project and use the Composer | Add dependency… context menu to download PHPUnit into our project. That’s right: PhpStorm 6 comes with a nice UI for searching packages from the Packagist website.

After successfully installing PHPUnit and all dependencies, our project structure is now the following: the vendor folder containing all dependencies brought in using Composer, including PHPUnit.

Just like with PHPUnit installed using PEAR or as a PHAR file, we need to configure PhpStorm with the location of PHPUnit. Under settings, navigate to the PHP | PHPUnit pane. We can now select the Use custom loader option and specify the path to Composer’s generated autoload.php. Optionally we can specify a default PHPUnit configuration file or a PHPUnit bootstrap file to be used when running tests.

Our run configuration will look pretty simple: specify the directory containing tests (or a specific class/method or rely on the PHPUnit configuration file to find tests to run).

We can now invoke this run configuration and have our unit tests running using the PHPUnit version installed through Composer.

Please download the latest build, provide as much feedback for bugs and feature requests here, and leave questions in the comments below or in our forums!

Develop with pleasure!
– JetBrains Web IDE Team

Phpstorm Phpunit Version Not Installed

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.

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

Phpunit

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:

Phpstorm Phpunit Xdebug

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

Phpstorm 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:

Phpstorm Phpunit

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.

Phpstorm Phpunit Memory Limit

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.