Getting XDebug to run in MAMP and CLI

I wanted to benefit from XDebug clean and comprehensible format during development both while using MAMP on the server and while using PHPUnit from the command line. Many guides may be found around the web but one that explained the existence of two distinct setups I could not find.

Enabling XDebug in MAMP server

I wanted the clear error messages XDebug will send enabled in MAMP and that’s easy once I understood how MAMP works. MAMP will not start php from a static php.ini file but will instead use a template to create a php.ini file any time the server is started.
This means that to have XDebug enabled I need to edit the template MAMP will use to boot its server php.

Editing php.ini template
Editing php.ini template

Original php.ini template file
Original php.ini template file

After that removing the semi-colon preceding the line zend_extension... will do the trick
Modified php.ini template file
Modified php.ini template file

and going in the phpInfo section of MAMP Web Start, after stopping and restarting the server, will show that XDebug is running.
Server version of php with XDebug
Server version of php with XDebug

Can I use coverage now?

When I tried to run

phpunit --colors --coverage-html test/unit/coverage tests/unit/someFileTest.php

after the tests did run I got this error

PHPUnit 3.7.28 by Sebastian Bergmann.

The Xdebug extension is not loaded. No code coverage will be generated.


.....................

Time: 50 ms, Memory: 2.50Mb

Haven’t I just now enabled XDebug?! Turns out no. Command line version of php (I’m talking about MAMP built-in php and not the one that came with the machine, see here) will use another php.ini, a static one, than MAMP server and I did need to enable XDebug in that one too to make it work for PHPUnit too.

Enabling XDebug for CLI version of php too

I’m using version 5.4.19 of php that came with MAMP and hence will edit the file

/Applications/MAMP/bin/php/php5.4.19/conf/php.ini

to remove the same semi-color I’ve removed before from a line that looks exactly the same as before.

I can use coverage now

Now that the CLI version of php that came with MAMP uses XDebug too I can run

phpunit --colors --coverage-html test/unit/coverage tests/unit/someFileTest.php && open tests/unit/coverage/index.html

and will get a nice coverage report

Coverage report generated by PHPUnit
Coverage report generated by PHPUnit