TYPO3 CMS - Run Extbase unit tests in PHPStormUpdate 27.06.2014
With TYPO3 6.2 unit- and functional test execution is possible directly through phpunit on command line. So keep in mind, that the steps described below are obsolete, if you use TYPO3 6.2.
TYPO3 CMS is great and since Extbase it is easy to create unit tests for your code. Just create your extension with the extension_builder (or manually), install the extension phpunit from TER, add some tests to your extension and now you´re fine to run your unit tests in the phpunit TYPO3 backend module.
If you want to run your tests directly in an IDE (in this article it´s PhpStorm), there are some things to keep care of, before the IDE is ready to run the tests. This article describes how to configure PhpStorm to run Extbase unit tests directly in the IDE and also shows some common error messages including a possible solution.
- A working TYPO3 installation with at least one page and a TS template
- An Extbase extension with at least one test
- TYPO3 extension "phpunit" installed
- The Extbase unit tests must run in TYPO3´s backend module "phpunit"
- Configure the PHP interpreter and set the PHP home path to:
- Edit your PhpStorm project to use the new PHP interpreter
- Adjust the PHP interpreter script.
The script which comes with the TYPO3 extension "phpunit" seems to contain an error in the CLI path. Adjust this path, so it uses php_ide_testrunner instead of php_ide as the cliKey
- Configure an environment variable in your PhpStorm project
Edit the test configuration for the folder "Tests" of your Extbase extension and set the environment variable ENV_TYPO3_SITE_PATH to the path of your website root
- Create a new TYPO3 Backend user named _cli_phpunit
- Configure include paths
Add the TYPO3 source and the TYPO3 extension phpunit to your PhpStorm projects include paths
This step is optional, but I think it is always nice to have code completion working when coding TYPO3 extensions.
- Now you should be ready to run your Extbase unit tests directly in PhpStorm
TYPO3 6.0 issues
TYPO3 6.0 comes with a new base test class, which actually is´nt recognised by the TYPO3 Extension "phpunit" in TER (version 3.6.11). The problem has already been fixed, but it is not published yet, so you have to get the changes manually and pull a new version of the extension from the GIT repository at git://git.typo3.org/TYPO3v4/Extensions/phpunit.git
Error messages and possible solutions
While working with TYPO3, Extbase, phpunit and PhpStorm, I stumbled across some situations, where the tests were not running because of some misconfigured settings. Below are 2 possible situations with an error message an a description how I fixed them.
If you face the error message "Unable to attach test reporter to test framework or test framework quit unexpectedly", this may indicate, that something is wrong with your local permissions.
Solution 1 - Wrong permissions
I am developing on an Ubuntu 12.04 desktop and apache2 and TYPO3 is installed locally. I have my own user to start the desktop, so my user does not have necessary permissions to access the files owned by the webserver.
First, you should add your local user to the group "www-data" (or the group running your local webserver). Next, you should check the TYPO3 install tool for the following:
[fileCreateMask] = 664
[FolderCreateMask] = 775
This is needed, so you local user can read/write to typo3temp. Finally delete the typo3temp/ folder, so the folder is recreated with the new file- and folder permissions.
Solution 2 - Trailing slash in ENV_TYPO3_SITE_PATH
This error message can also orrur, if the ENV_TYPO3_SITE_PATH contains a trailing slash. So check if the path is correct.
Once I also came across this error message "PHP Fatal error: Cannot redeclare phpunit_autoload() (previously declared in /some/path/script.php)".
The solution for this problem was, that the cliKey in the php interpreter script was set to "phpunit" instead of "phpunit_ide_testrunner"