Skip to content

Debugging PHP tests

Mart Sõmermaa edited this page Mar 12, 2024 · 3 revisions

To debug PHP tests in Ubuntu (assuming PHP 8.2 is the default) with Visual Studio Code:

  1. Install Xdebug:

    sudo apt install php8.2-dev
    sudo pecl install xdebug
  2. Enable Xdebug:

    php --ini # verify the INI file location
    sudo su
    echo 'zend_extension=/usr/lib/php/20220829/xdebug.so' >> /etc/php/8.2/cli/conf.d/99-xdebug.ini
  3. Verify that it is enabled:

    $ php --version
    PHP 8.2.16 (cli) (built: Mar  7 2024 08:55:56) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.2.16, Copyright (c) Zend Technologies
        with Zend OPcache v8.2.16, Copyright (c), by Zend Technologies
        with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans
  4. Activate debugging by adding the following lines to /etc/php/8.2/cli/conf.d/99-xdebug.ini:

    xdebug.mode = debug
    xdebug.start_with_request = yes
    
  5. Open VS Code and install the PHP Debug extension.

  6. Activate debugging according to instructions on the extension site.

    1. In your project, go to the debugger and hit the little gear icon and choose PHP. A new launch configuration will be created for you...
  7. Test debugging.

    1. Add a breakpoint in VS Code.
    2. Press the Listen for Xdebug button in Run and debug mode, the debugging session will become active.
    3. Run tests on command-line with composer test.
    4. The breakpoint will be hit in VS Code, you can examine the variables and use the debugging toolbar:
      image
Clone this wiki locally