Blog

Blog

TYPO3 Unit Test in PhpStorm

Disclaimer

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.

Disclaimer

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.

Prerequisites:

  1. PHP installed on your machine
  2. PHP Unit installed on your machine

Configuration:

  1. Make PHP Unit Sources available in your PhpStorm project
  2. Configure the PHP binary for your PhpStorm project
  3. 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:

  1. There is no bootstrap which initializes everything for unit tests
  2. 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

Prerequisites:

  1. A running TYPO3 installation on your machine with phpunit extension installed.
  2. CLI user _cli_phpunit present
  3. 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:

  1. Download the shell script I made and put it in a place which suits you best, rename it tophp and make it executable
  2. Edit the script and fill in the path to the PHP binary on your machine
  3. Configure PhpStorm to use this script as PHP interpreter
  4. 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
  5. Register the new CLI in ext_localconf.php of phpunit and clear the temp_cached files
  6. Include the phpunit extension directory into your PhpStorm project
  7. 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.

Comments appreciated.

Have fun!

Ihr Browser ist veraltet!

Bitte aktualisieren Sie Ihren Browser, um diese Website korrekt dazustellen. Den Browser jetzt aktualisieren

Diese Webseite verwendet Cookies und das Webanalyse-Tool Google Analytics. Durch die Nutzung unserer Seiten erklären Sie sich damit einverstanden.

Eine Widerspruchsmöglichkeit finden Sie in unserer Datenschutzerklärung.
Einverstanden