Skip to content

PHPStorm Integration

Mark Metcalfe edited this page Oct 10, 2021 · 31 revisions

Performance Improvements

Faster Indexing

PHPStorm can be slow when indexing files, and is especially bad when using WSL2 on Windows. It can be made quicker by excluding some directories that we don't need from indexing.

You can add non-important files and directories to the list of exclusions by copying the contents of this file into your project's main .iml file, which will be located at .idea/PROJECTNAME.iml (e.g ~/my-totara-code/engage/.idea/engage.iml)

This can make the indexing of your project up to 30-40% faster.

Running Unit Tests

Important Note: You will need to have already installed PHPUnit for your site as per these instructions.

Windows Note: Setting up remote interpreters is currently broken on Windows (see #172)

  1. Go to PHPStorm settings (Ctrl+Alt+S), and go to PHP
  2. Under CLI Interpreter, click the ... button
  3. Add an interpreter, selecting From Docker, Vagrant, VM, WSL, Remote...
  4. Select Docker Compose
  5. Under Server, click New and add a new Docker daemon using the relevant type for your OS (E.g. Unix socket on Linux, Docker for Windows on Windows), then click OK

Add Docker daemon window

  1. Add Configuration files, with the files being: (in the following order) docker-dev/docker-compose.yml, and docker-dev/compose/php.yml (where docker-dev is the path of this docker-dev respository on your machine)

Docker compose configuration window

  1. Select a PHP container under Service, e.g. php-7.4-debug
  2. Click OK
  3. Untick Visible only for this project
  4. Set Lifecycle to Connect to existing container
  5. Your CLI interpreter should look something like this:

CLI Interpreters window

  1. Click OK
  2. For CLI Interpreter, select the interpreter you just added and click OK
  3. Under Path mappings, add a mapping for your local totara code directory, where Local Path = LOCAL_SRC and Remote Path = REMOTE_SRC from your docker-dev .env file. (If using Windows & WSL, then the Local Path value must be prefixed with //wsl$/Ubuntu)

Path mappings window

  1. Go to PHP > Test Frameworks
  2. Add a test framework, selecting PHPUnit by Remote Interpreter
  3. Select the interpreter you added and click OK
  4. Set Path to script to be /var/www/totara/src/SITENAME/test/phpunit/vendor/autoload.php, where SITENAME is the name of your site's subdirectory (if you do not have multiple sites set up, this can be omitted). Note: For Totara 12 or older/Moodle, this will need to be /var/www/totara/src/SITENAME/vendor/autoload.php
  5. Set Default configuration file under Test Runner to be /var/www/totara/src/SITENAME/test/phpunit/phpunit.xml, where SITENAME is the name of your site's subdirectory (if you do not have multiple sites set up, this can be ommitted). Note: For Totara 12/Moodle or older, this will need to be /var/www/totara/src/SITENAME/phpunit.xml

Test frameworks window

You should now be able to run unit tests directly from within PHPStorm. If you set up XDebug as documented below, you will also be able to set breakpoints within in your tests too.

PHPStorm Running tests

Debugging via XDebug

Important Note: If you are using Linux, make sure that the correct HOST_IP value is defined in your .env file that is located in the root of your docker-dev repo. You will likely need to set it to 172.17.0.1, otherwise you will not be able to debug requests from the browser. See the .env file for more information.

  1. Go to PHPStorm settings (Ctrl+Alt+S), and go to PHP > Servers
  2. Add a server, with the name being totaraXX (where XX = the PHP version, e.g. 73), the host being totaraXX.debug, the port being 80, and the debugger being Xdebug. If you have multiple sites set up with subdomains, you can set the host to be SITENAME.totaraXX
  3. Tick Shared (next to Name)
  4. Tick Use path mappings, and set the absolute path on the server to be /var/www/totara/src (or /var/www/totara/src/SITENAME if you have multiple sites set up)

PHPStorm Servers configuration

XDebug should now work and PHPStorm will listen and activate breakpoints when the Start Listening for PHP Debug Connections setting is enabled.

You can trigger your breakpoints when visiting http://SITENAME.totaraXX.debug/ pages in your browser, or when running unit tests within PHPStorm (if you are using a totaraXX.debug host), or when running a PHP script while in a php-X.X-debug container.

Viewing Your Databases

  1. In the database tool pane, add a new database and select the DBMS you wish to view (e.g. PostgreSQL or MySQL) under Data Source

PHPStorm Add Database Source

  1. Set the Host to be localhost
  2. Set the Port, User and Password values to be the same as what is listed in this table
  3. Download Drivers if they are missing

PHPStorm Database configuration

  1. Under the Schemas tab, select the database schemas you want to be able to view (or select All schemas_)
  2. Click OK
  3. Click Refresh in the Database panel

You should now be able to view tables and use the database console for the DBMS you added.

Clone this wiki locally