When developing PHP applications, two extremely useful tools are Xdebug and PhpUnit. The configuration of these tools, however, can be confusing. This post will help clarify how to set the two up with the PHPStorm IDE and Mamp Pro.
Xdebug
- If you don’t have it yet, install and configure MampPRO. (https://documentation.mamp.info/en/MAMP-PRO-Mac/First-Steps/)
- Mamp Pro should install Xdebug by default, but you will need to set PHPStorms ide key under the [xdebug] setting.
- Set the CLI interpreter in PHPStorm. You will need to add the Mamp Pro Server as below. Also ensure to set the PHP language level to match the level of the Mamp Server.


- Add a remote debug configuration in PHPStorm


- Add a few lines of code to an index.php file and click the telephone icon and set a breakpoint.
- Open your site in your browser and reload. If all goes well the breakpoint should be triggered.
Phpunit
- From your project root run composer require –dev phpunit/phpunit ( check https://phpunit.de/manual/current/en/installation.html for more information)
- Structure your project with a src and tests directory in the root.
- Add a phpunit.xml to the root with the contents:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="true"/>
</php>
</phpunit>
- Add the psr4 autoload config to the composer.json to define a mapping from namespaces to paths, relative to the package root 11
{
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
"YOUR_NAMESPACE\\ROOTS\\": "src/",
"YOUR_NAMESPACE\\ROOTS\\Tests\\": "tests/" } } }
- Add a new PHPUnit configuration in PHPStorm and set the configuration file to the phpunit path and the custom working directory to be the directory of your vendor folder.
- Add a test class to ensure the Unit Tests are working. I pulled this from the PHPUnit documentation. Add this StackTest.php file to your test directory.
<?php
namespace YOUR_NAMESPACE\ROOTS\Tests;
use PHPUnit\Framework\TestCase;
class StackTest extends TestCase
{
public function testPushAndPop()
{
$stack = [];
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
?>
- If you now run the unit tests by clicking the play button next to the configuration you should see a passing test.
- You can also debug the tests. Set a breakpoint anywhere in the test and click the debug button next to the play button.
- We can also run the tests with coverage thanks to XDebug. Click the code coverage button next to the Debug button.

I hope that was useful. If you have any question or suggestions, leave a comment below.