The following is a proof of concept which works for me with PhpStorm 3.0.2 and 4.0.1 on Mac OSX and should work with no or minor adaptions in other IDEs on all *nix systems. In general this could also work on Windows, however someone needs to adapt the concept and make it work for that platform. After all what I suggest here is a „hack“ and we should think about ways to get rid of dirty tricks to be able execute TYPO3 unit tests in the IDE.
Unit Tests in PhpStorm
PhpStorm has great support for executing unit tests directly in the IDE. Just write your test cases, right click on the test class or a single test or even a complete folder with test cases and select „Run“. PhpStorm executes PHP Unit under the hood and shows the results nicely in a window.
For this to work properly you need to pay attention to some things.
- PHP installed on your machine
- PHP Unit installed on your machine
- Make PHP Unit Sources available in your PhpStorm project
- Configure the PHP binary for your PhpStorm project
- The test case classes must extend the PHPUnit_Framework_TestCase class
If you’ve done so, you’re good to go.
But if you want to execute tests that depend on a framework (e.g. FLOW3), you need at least the autoloader to be working when executing the tests. You can do this by adding aPHP Unit XML Configuration file in the default test run configuration of PhpStorm. In the XML file you can configure a so called bootstrap PHP file which is included before running the tests. In this bootstrap the framework can take care of registering the autoloader and probably do some other things for initialization.
Now there are two problems when you have TYPO3 as your framework:
- There is no bootstrap which initializes everything for unit tests
- The bootstrap of TYPO3 must be executed in a global scope, because it relies on a lot of global variables. PHP Unit however includes the configured bootstrap file in the scope of a function, thus all variables would not be global any more.
Still I wanted this to work integrated nicely in the IDE and not just by assigning a keyboard shortcut to a CLIs script that executes the tests.
Unit Testing TYPO3 in PhpStorm
- A running TYPO3 installation on your machine with phpunit extension installed.
- CLI user _cli_phpunit present
- Extension(s) installed you want to execute the tests for
These prerequisites are a pain and we should really think of a solution to remove these dependencies. I have some ideas how to do that but for now I can live with that, as I have several local TYPO3 installations on my machine anyway.
Here’s what you need to do:
- Download the shell script I made and put it in a place which suits you best, rename it tophp and make it executable
- Edit the script and fill in the path to the PHP binary on your machine
- Configure PhpStorm to use this script as PHP interpreter
- Download the alternative CLI „testrunner“ for the phpunit extension and put it in the Classes/Cli/ directory inside this extension and rename it to TestRunnerIde.php
- Register the new CLI in ext_localconf.php of phpunit and clear the temp_cached files
- Include the phpunit extension directory into your PhpStorm project
- Configure the default run configuration for unit tests and set the environment variableENV_TYPO3_SITE_PATH to the path of your local TYPO3 installation
Now you can right click on single tests, test case classes or even a whole directory of test cases and select run to execute the tests.