AutoLoader effect #1: “/Tests” Folder

There are some effects of the Extbase AutoLoader, you shall be aware of when writing unit tests.

Testing the repository, I defined a “RepositoryBaseTestCase” which holds common functionality for all 4 repository tests. Simple. Problem: The autoloader was not able to find this class when I tried to extend it.

class Tx_Csevents_Tests_Unit_Domain_Repository_AppointmentRepositoryTest extends Tx_Csevents_Tests_Unit_Domain_Repository_RepositoryBaseTestCase {
}

The base class “Tx_Csevents_Tests_Unit_Domain_Repository_RepositoryBaseTestCase” is on the same directory, but the autoloader is not able to find it. Whenever I try to run the tests, I get the error message “Fatal error: Class ‘Tx_Csevents_Tests_Unit_Domain_Repository_RepositoryBaseTestCase’ not found in /home/.sdb/var/www/html/dummy-4.5.32/typo3conf/ext/csevents/Tests/Unit/Domain/Repository/AppointmentRepositoryTest.php on line 37” Why is that? Simply, it’s because my unit tests are not stored underneath “/Classes”, but underneath “/Tests” (you see the referred base class on the bottom of the screenshot).

screenshot1The autoloader recongnises classes in the “/Classes” folder only, thus it doesn’t find the “RepositoryBaseTestCase.php” when I refer to it as “Tx_Csevents_Tests_Unit_Domain_Repository_RepositoryBaseTestCase”.

Solution: Help the autoloader and give it a file “/ext_autoload.php”:

screenshot2


<?php

$extensionClassesPath = t3lib_extMgm::extPath('csevents') . 'Classes/';
return array(
    'tx_csevents_tests_unit_domain_repository_repositorybasetestcase' => $extensionClassesPath . '../Tests/Unit/Domain/Repository/RepositoryBaseTestCase.php',
);

?>

Now the autoloader knows where to search for the referred class. Please note that you have to use lowercase letters for the array keys until Typo3 4.5.x including.

Using the “extensionClassesPath” with the trailing “Classes/” directory, which is removed by the “../” in the array again, is a little strange. I just used it, because this annotation is used commonly. It reflects the view of the autoloader, which is using “Classes” as it’s base directory to start searching. You may change this, if you feel better then. 😉

You could have done a simple “require_once” instead of all this stuff, but this is forbidden by the coding guidelines. Thus this is the official and nice solution.

Leave a Reply