26

I wrote some API tests with Codeception's ApiGuy. Now I want to set breakpoints in my PhpStorm 7 for tests debugging, but have no idea how to start debug session after $ vendor/bin/codecept run. I know about --debug option, but it's not exactly what I want.

Do you have any idea? Thanks in advance!

Vitaly Chirkov
  • 1,659
  • 2
  • 17
  • 31

6 Answers6

28

I ran into the same problem. Seems that Codeception comes only with command tool, which cannot be debugged. I finally ended up writing my own PHP runner, which is basically a lite copy of the codeception command executable.

Actually all you would have to do in Linux is to remove the shebang from the codeception tool to run it as a PHP script. But since commands other then codeception run are much less likely to be a subject of debugging, I've prepared separate PHP script. It contains only the run option:

<?php
/**
 * Codeception PHP script runner
 */

require_once dirname(__FILE__).'/../vendor/codeception/codeception/autoload.php';

use Symfony\Component\Console\Application;

$app = new Application('Codeception', Codeception\Codecept::VERSION);
$app->add(new Codeception\Command\Run('run'));

$app->run();

After you get this done you can set up your debugging script like any other in PHPStorm. So go to the Select Run/Debug Configuration > Edit Configurations...:

Edit Configurations...

Now Add New Configuration (Alt + Inssert) > PHP Script. Then name the run configuration and select the file you created above. Remember to add the run argument:

enter image description here

And that's it. Now you can run your tests from within IDE and debug them as ordinary scripts.

Maciej Sz
  • 9,025
  • 6
  • 37
  • 51
  • Wow, thanks a lot, works like a charm! I've specified also custom working directory to my project root to avoid config exception. – Vitaly Chirkov Dec 23 '13 at 10:07
  • This works. For anyone else troubleshooting, try putting the `run.php` file you create in the same directory as your `codeception.yml`. that got it working for me. – Chris May 14 '15 at 12:06
  • You are a freaking genius! Thanks a million! – Paul Preibisch Feb 24 '17 at 05:49
21

I use the codecept.phar file and I find this one does work. just type this command in the console and then run the codeception tests:

export XDEBUG_CONFIG="idekey=session_name remote_host=localhost profiler_enable=1"
Baby Groot
  • 4,609
  • 39
  • 50
  • 67
user3391306
  • 211
  • 2
  • 2
  • 3
    It is if you use a batch script shortcut – Tominator Jul 18 '14 at 13:51
  • This will also work when using a remote server to run the codeception. You just have to specify a different remote_host in that export line. – ROunofF Oct 01 '14 at 02:45
  • In some cases (running a script on one machine, debugging remotely), it may be useful to set export XDEBUG_CONFIG="autostart=on". Every script you invoke will start the debugger with the settings already populated in php/cli/php.ini – tjb Oct 02 '18 at 12:01
5

I would suggest to use

php -dxdebug.remote_enable=1 -dxdebug.remote_autostart=On -dxdebug.idekey=YOUR_KEY -dxdebug.remote_host=YOUR_IP ../vendor/bin/codecept run
Dezigo
  • 3,012
  • 3
  • 29
  • 38
  • Works great. Just place it into "Interpreter options" in the configuration dialog (options part only, without "php " and "../vendor/bin/codecept run"). – asologor Mar 15 '18 at 10:41
2

Alias could be used

alias xon="export XDEBUG_CONFIG=\"profiler_enable=1\""
alias xoff="export XDEBUG_CONFIG=\"profiler_enable=0\""

And then we could use xon in console to start debug and xoff to finish.

http://theaveragedev.com/debug-cli-scripts-with-phpstorm

StalkAlex
  • 767
  • 8
  • 16
1

Another way is to configure PhpStorm, as above, but set bin/cept for File, run for Arguments, and the root of your project as Custom Working Directory.

Matt Setter
  • 2,224
  • 1
  • 19
  • 15
0

I don't think you actually need an extra script to debug Codeception at least in PHPStorm. I don't think you would in other IDEs either. You can set codecept.phar as the "File" in your configuration shown in Maciej Sz's answer and run it as you would any other command line script from PHPStorm, Eclipse, etc.

In PHPStorm you create a new PHP Script debug configuration pointing at codecept.phar as the "File". Set the arguments to be a basic run configuration (e.g. run acceptance --group mygroup --env myenv), but tell it to "Show this page" every time you want to debug. You can then change which test/group/environment you are running each time to run your test with breakpoints.

When you click the debug button from the pop-up the acceptance test starts in the debugger console.

The initial run that hits a break-point will be in the validation phase where Codeception attempts to ensure the code will run before running it. The second run will be when the test is actually using Selenium or whatever you have configured. This will allow you to see what is in memory, step over things in a controlled manner, etc.

cambist
  • 73
  • 2