Skip to content

PHPStorm Integration

Mark Metcalfe edited this page Nov 26, 2024 · 31 revisions

Performance Improvements

Faster Indexing

PHPStorm can be slow when indexing Totara project files due to the sheer amount of code we have. 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 ~/totara-sites/integration/.idea/integration.iml)

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

Running Unit Tests

  1. Install PHPUnit for your site as per these instructions.
  2. Go to PHPStorm settings (Ctrl+Alt+S), and go to PHP
  3. Under CLI Interpreter, click the ... button
  4. Add an interpreter, selecting From Docker, Vagrant, VM, WSL, Remote...
  5. Select Docker Compose
  6. Under Server, click New and add a new Docker daemon using the relevant type for your OS then click OK
    • MacOS: Rancher Desktop, or if you don't have Rancher, Docker Desktop
    • Windows: Docker for Windows
    • Linux: Unix socket
Add Docker daemon window
  1. Add Configuration files, with the files being: (in the following order) totara-docker-dev/docker-compose.yml, and totara-docker-dev/compose/php.yml (where totara-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-8.3-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.
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

  1. Ubuntu & Windows Only: Set the HOST_IP variable in your docker-dev .env file to be 172.17.0.1
  2. Go to PHPStorm settings (Ctrl+Alt+S), and go to PHP > Servers
  3. Add a server, with the name being totaraXX (where XX = the PHP version, e.g. 83), 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
  4. Tick Shared (next to Name)
  5. 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
  2. Set the Host to be localhost

  3. Set the Port, User and Password values to be the same as what is listed in this table

  4. 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