I ran into issues using composers psr-4 autoloading feature and PHPUnit. The problems occur on my local machine as well as on TravisCI builds. Unit tests aren't able to find my classes, while normal scripts do.
There is certainly something wrong with any of the configurations, but I cannot figure out what I did wrong. Thanks in advance for your help.
Here's my projects file structure:
- public/
- src/
- http/
- urlhelper.php
- lib.php
- http/
- tests/
- blacktie/
- http/
- urlhelper.test.php
- lib.test.php
- http/
- bootstrap.php
- testcase.php
- blacktie/
- .travis.yml
- composer.json
- init.php
- phpunit.xml
Here's what my composer.json looks like:
{
"require": {
"php": ">=5.4.0",
"nesbot/carbon": "1.*",
"ext-pdo": "*",
"predis/predis": "1.1.*@dev"
},
"require-dev": {
"satooshi/php-coveralls": "dev-master",
"phpunit/phpunit": ">=3.7"
},
"autoload": {
"psr-4": {
"BlackTie\\": "src/blacktie"
}
}
}
The PHPUnit configuration defines a bootstrap, which includes per require_once
a script shared by the main project and the tests to initialise the psr-4 autoloader and common functions: init.php
define('ROOT', __DIR__);
// Composer-Autoloader
require_once "vendor/autoload.php";
// Framework library with common functions
require_once "src/blacktie/lib.php";
Travis' command line says:
PHP Fatal error: Class 'BlackTie\Http\UrlHelper' not found in /home/travis/build/jazzschmidt/black-tie/tests/blacktie/http/urlhelper.test.php on line 38