Using the PHPUnit extension for TYPO3 v4 in PhpStorm
Contents |
Introduction
To use PHPUnit in TYPO3 v4, first install the PHPUnit extension. You can use it on the CLI and the GUI.
Using the builtin PHPUnit test feature with TYPO3 v4 extensions is not possible out of the box, as far as is known. This guide shows how to work around that.
Guide
Step 1
First of all, make sure that you can run unit tests via the CLI.
The general command pattern is:
php --some-php-args /path/to/TYPO3/cli_dispatch.phpsh phpunit --some-phpunit-args path/to/tests/
If that works in your shell, it will work within PhpStorm, too.
If you experience issues, make sure that the extension is installed and the backend user _cli_phpunit was created.
Step 2
In PhpStorm, go to File -> Settings -> External Tools and click on "Add...". On OS X, go to Preferences -> External Tools.
Step 3
- Give your new external tool a name, e.g.: "TYPO3 PHPUnit Test" and a group, e.g.: "phpunit".
- In "Program", enter: $PhpExecutable$.
- In "Parameters", enter: $ProjectFileDir$/typo3/cli_dispatch.phpsh phpunit $FilePath$ and click "OK".
- If you would like to assign your new external tool a shortcut key, go to step 4. Otherwise go to step 5.
$ProjektFileDir$ is the root of the project, the root folder of a TYPO3 website. If you have different project settings, adjust accordingly.
Step 4
- Go to Preferences -> Keymap.
- Search for the new external tool, in this case "TYPO3 PHPUnit Test", and select it.
- Click on "Add Keyboard Shortcut..."
- Assign a key and click "OK".
Step 5
Open a file containing unit tests and execute them by either using the shortcut or selecting the command in the menu: Tools -> "TYPO3 PHPUnit Test"
Using PHPUnit via CLI and MAMP
If you use MAMP together with PHPUnit CLI script, you have to adjust localconf.php, since MAMP uses a non-default MySQL socket. To fix that, edit localconf.php and add the socket after typo3_db host, e.g.:
$typo_db_host = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
Configuration for one testsuite/test only
For running only one test or test suite, you have to add some arguments to your shell command:
Running one testsuite
php --some-php-args /path/to/TYPO3/cli_dispatch.phpsh phpunit classNameOrTestSuiteName path/to/tests/
Running one test
php --filter myFancyTestName /path/to/TYPO3/cli_dispatch.phpsh phpunit classNameOrTestSuiteName path/to/tests/
TODO
Testing one class/method only
There is an feature request for PhpStorm to add two other macros for external tools. One macro returns the class name around the cursors position. The other macro returns the method mname around the cursors position. If this macros will be included you can easily test the current class or even method = test.