Tuesday, December 17, 2013

Day 19: PHPUnit testing for TYPO3, the success story


Today, with a lot of help from a hacker-in-residence, I figured out how to get my PHPUnit tests for TYPO3 running. The problem stemmed from the fact that, instead of extending PHPUnit_Framework_TestCase, TYPO3 tests extend their own Test class that serves as an autoloader for TYPO3 dependencies. PHPUnit from the command line doesn't recognize that without the help of an executable (included with the TYPO3 source code) called "cli_dispatch.phpsh".

This file can be found in the main "typo3" folder of your TYPO3 installation. To run PHPUnit tests with TYPO3 dependencies - presumably as part of extension development with Extbase - here's what you need to do:

On the command line, navigate to the directory right above where your "typo3" folder is found. On my local machine, that's:

$ cd /var/www/html

Call phpunit as the argument ("cli key") for the cli_dispatch.phpsh file, run as an exectuble. Follow the "phpunit" argument with any phpunit configurations you'd like (configuration file, colors, strict, etc.) and then the file you want to test - just as if you were calling phpunit directly from the command line:

$ ./typo3/cli_dispatch.phpsh phpunit typo3conf/ext/sjr_offers/Tests/Domain/Model/OrganizationTest.php

If you're like me, nothing will happen - you'll just get a new command line without output. To find out what the problem could be, you can call the following code on the command line:

$ ./typo3/cli_dispatch.phpsh phpunit status; echo $?

If you get "1" as output (which means, cryptically, "error"), you still need to change the following configurations in the TYPO3 Install Tool in order to get a better idea of what's going on:

$TYPO3_CONF_VARS['SYS']['displayErrors'] = 2;
$TYPO3_CONF_VARS['SYS']['devIPmask'] = '*';

This will tell TYPO3 to output errors as long as the caller's IP address matches TYPO3's IP address (i.e., if we are in a development environment). Setting the "devIPmask" to "*" tells TYPO3 that all callers' IP addresses should be accepted as "matching".

Thank you, @Christian Weiske, for this tip - you seriously saved my day!

After doing this, you should get a more detailed error of phpunit's problem. For me, it was insufficient write permissions on the typo3temp file, where locks on files are set up and log files are written. After adding some group write permissions on this file, running the "status" command got me a PHPUnit error, which means that PHPUnit is now working - awesome!

Running the code from above again,

$ ./typo3/cli_dispatch.phpsh phpunit typo3conf/ext/sjr_offers/Tests/Domain/Model/OrganizationTest.php

PHPUnit runs through all assertions in the test without problems and gives me a status report. Gods, I'm so happy I could just cry. :)

For now, I'm going to put off running PHPUnit tests from Eclipse on hold, in order to write some tests and have more of that "success experience" today, after the horrible Day of Failure that was yesterday. I'm going to create a new extension with Extension Builder, modelling the same domain model and start-functionality as the "sjr_offers" extension used by the book and import it into TYPO3. Then I will create the "Tests" directory structure in my extension and start implementing tests for each functionality. I can run them through PHPUnit and compare my efforts with the code from "Reliable Extensions". I'd also like to write a configuration file ("phpunit.xml") that loads my command-line configuration for me - without breaking anything!

Stay tuned...

After a lot of googling, I'm going to have to guess that this suggested procedure is a sensible one.

No comments:

Post a Comment

Play nicely!