I have a project for which I am trying to run unit tests without success. It is most likely to be related to the autoload.
/path/to/project$ phpunit tests
Fatal error: Class 'Foo\Bar' not found in /path/tests/Foo/BarTest.php on line X
This line is the one that does the new Bar();
I have a phpunit.xml inside the tests directory like this
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true">
<testsuites>
<testsuite name="App">
<directory suffix="Test.php">tests/Foo</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<file>src/bootstrap.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
My structure is src/Foo/Bar.php tests/Foo/BarTest.php
My tests/bootstrap.php
if ( ! file_exists($file = __DIR__.'/../vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Foo', __DIR__ . '../src/');
So I am mapping the Foo namespace to the src directory but PHPUnit seems to not find it.
This autoload is the Composer generated one.