Autoloading classes in PHPUnit using Composer and autoload.php - php

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test.
I would like to have all my classes autoloaded into each unit test without having to use include or require at the top of my test but this is proving to be difficult!
This is what my directory structure looks like (a trailing / slash indicates a directory, not a file):
* composer.json
* composer.lock
* composer.phar
* lib/
* returning.php
* tests/
* returningTest.php
* vendor/
* bin/
* phpunit
* composer/
* phpunit/
* symfony/
* autoload.php
My composer.json file includes the following:
"require": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
}
My returning.php class file includes the following:
<?php
class Returning {
public $var;
function __construct(){
$this->var = 1;
}
}
?>
My returningTest.php test file includes the following:
<?php
class ReturningTest extends PHPUnit_Framework_TestCase
{
protected $obj = null;
protected function setUp()
{
$this->obj = new Returning;
}
public function testExample()
{
$this->assertEquals(1, $this->obj->var);
}
protected function tearDown()
{
}
}
?>
However, when I run ./vendor/bin/phpunit tests from the command-line, I get the following error:
PHP Fatal error: Class 'Returning' not found in
/files/code/php/db/tests/returningTest.php on line 8
I noticed that composer produced an autoload.php file in vendor/autoload.php but not sure if this is relevant for my problem.
Also, in some other answers on Stack Overflow people have mentioned something about using PSR-0 in composer and the namespace command in PHP, but I have not been successful in using either one.
Please help! I just want to autoload my classes in PHPUnit so I can just use them to create objects without worrying about include or require.
Update: 14th of August 2013
I have now created an Open Source project called PHPUnit Skeleton to help you get up and running with PHPUnit testing easily for your project.

Well, at first. You need to tell the autoloader where to find the php file for a class. That's done by following the PSR-0 standard.
The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a Acme\Tests\ReturningTest class. There are some great namespace tutorials out there, just search and read. Please note that namespacing is not something that came into PHP for autoloading, it's something that can be used for autoloading.
Composer comes with a standard PSR-0 autoloader (the one in vendor/autoload.php). In your case you want to tell the autoloader to search for files in the lib directory. Then when you use ReturningTest it will look for /lib/ReturningTest.php.
Add this to your composer.json:
{
...
"autoload": {
"psr-0": { "": "lib/" }
}
}
More information in the documentation.
Now the autoloader can find your classes you need to let PHPunit know there is a file to execute before running the tests: a bootstrap file. You can use the --bootstrap option to specify where the bootstrap file is located:
$ ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php
However, it's nicer to use a PHPunit configuration file:
<!-- /phpunit.xml.dist -->
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
Now, you can run the command and it will automatically detect the configuration file:
$ ./vendor/bin/phpunit
If you put the configuration file into another directory, you need to put the path to that directory in the command with the -c option.

[Update2] Another simpler alternative approach is to use the autoload-dev directive in composer.json (reference). The benefit is that you don't need to maintain two bootstrap.php (one for prod, one for dev) just in order to autoload different classes.
{
"autoload": {
"psr-4": { "MyLibrary\\": "src/" }
},
"autoload-dev": {
"psr-4": { "MyLibrary\\Tests\\": "tests/" }
}
}
[Update] Wouter J's answer is more complete. But mine can help people who want to set up PSR-0 autoloading in tests/ folder.
Phpunit scans all files with this pattern *Test.php. So we don't need to autoload them ourselves. But we still want to autoload other supporting classes under tests/ such as fixture/stub or some parent classes.
An easy way is to look at how Composer project itself is setting up the phpunit test. It's actually very simple. Note the line with "bootstrap".
reference: https://github.com/composer/composer/blob/master/phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Composer Test Suite">
<directory>./tests/Composer/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>slow</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/Composer/</directory>
<exclude>
<file>./src/Composer/Autoload/ClassLoader.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
reference: https://github.com/composer/composer/blob/master/tests/bootstrap.php
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman#naderman.de>
* Jordi Boggiano <j.boggiano#seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
error_reporting(E_ALL);
$loader = require __DIR__.'/../src/bootstrap.php';
$loader->add('Composer\Test', __DIR__);
The last line above is autoloading phpunit test classes under the namespace Composer\Test.

None of these answers were what I was looking for. Yes PHPUnit loads test files, but not stubs/fixtures. Chaun Ma's answer doesn't cut it because running vendor/bin/phpunit already includes the autoload, so there's no way to get an instance of the autoloader to push more paths to it's stack at that point.
I eventually found this in the docs:
If you need to search for a same prefix in multiple directories, you
can specify them as an array as such:
{
"autoload": {
"psr-0": { "Monolog\\": ["src/", "lib/"] }
}
}

There is a really simple way to set up phpunit with autoloading and bootstap. Use phpunit's --generate-configuration option to create your phpunit.xml configuration in a few seconds-:
vendor/bin/phpunit --generate-configuration
(Or just phpunit --generate-configuration if phpunit is set in your PATH). This option has been available from version phpunit5 and upwards.
This option will prompt you for your bootstrap file (vendor/autoload.php), tests and source directories. If your project is setup with composer defaults (see below directory structure) the default options will be all you need. Just hit RETURN three times!
project-dir
-- src
-- tests
-- vendor
You get a default phpunit.xml which is good to go. You can of course edit to include any specialisms (e.g. colors="true") you require-:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

If you are using PHPUnit 7 you can make your classes from src/ folder to autoload in tests like this:
Ensure that your composer.json file looks similar to this:
{
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^7"
}
}
To apply changes in composer.json run command:
composer install
Finally you can run tests in tests/ folder:
./vendor/bin/phpunit tests/

Related

PHPUnit Class not found error with Composer [duplicate]

I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test.
I would like to have all my classes autoloaded into each unit test without having to use include or require at the top of my test but this is proving to be difficult!
This is what my directory structure looks like (a trailing / slash indicates a directory, not a file):
* composer.json
* composer.lock
* composer.phar
* lib/
* returning.php
* tests/
* returningTest.php
* vendor/
* bin/
* phpunit
* composer/
* phpunit/
* symfony/
* autoload.php
My composer.json file includes the following:
"require": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
}
My returning.php class file includes the following:
<?php
class Returning {
public $var;
function __construct(){
$this->var = 1;
}
}
?>
My returningTest.php test file includes the following:
<?php
class ReturningTest extends PHPUnit_Framework_TestCase
{
protected $obj = null;
protected function setUp()
{
$this->obj = new Returning;
}
public function testExample()
{
$this->assertEquals(1, $this->obj->var);
}
protected function tearDown()
{
}
}
?>
However, when I run ./vendor/bin/phpunit tests from the command-line, I get the following error:
PHP Fatal error: Class 'Returning' not found in
/files/code/php/db/tests/returningTest.php on line 8
I noticed that composer produced an autoload.php file in vendor/autoload.php but not sure if this is relevant for my problem.
Also, in some other answers on Stack Overflow people have mentioned something about using PSR-0 in composer and the namespace command in PHP, but I have not been successful in using either one.
Please help! I just want to autoload my classes in PHPUnit so I can just use them to create objects without worrying about include or require.
Update: 14th of August 2013
I have now created an Open Source project called PHPUnit Skeleton to help you get up and running with PHPUnit testing easily for your project.
Well, at first. You need to tell the autoloader where to find the php file for a class. That's done by following the PSR-0 standard.
The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a Acme\Tests\ReturningTest class. There are some great namespace tutorials out there, just search and read. Please note that namespacing is not something that came into PHP for autoloading, it's something that can be used for autoloading.
Composer comes with a standard PSR-0 autoloader (the one in vendor/autoload.php). In your case you want to tell the autoloader to search for files in the lib directory. Then when you use ReturningTest it will look for /lib/ReturningTest.php.
Add this to your composer.json:
{
...
"autoload": {
"psr-0": { "": "lib/" }
}
}
More information in the documentation.
Now the autoloader can find your classes you need to let PHPunit know there is a file to execute before running the tests: a bootstrap file. You can use the --bootstrap option to specify where the bootstrap file is located:
$ ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php
However, it's nicer to use a PHPunit configuration file:
<!-- /phpunit.xml.dist -->
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
Now, you can run the command and it will automatically detect the configuration file:
$ ./vendor/bin/phpunit
If you put the configuration file into another directory, you need to put the path to that directory in the command with the -c option.
[Update2] Another simpler alternative approach is to use the autoload-dev directive in composer.json (reference). The benefit is that you don't need to maintain two bootstrap.php (one for prod, one for dev) just in order to autoload different classes.
{
"autoload": {
"psr-4": { "MyLibrary\\": "src/" }
},
"autoload-dev": {
"psr-4": { "MyLibrary\\Tests\\": "tests/" }
}
}
[Update] Wouter J's answer is more complete. But mine can help people who want to set up PSR-0 autoloading in tests/ folder.
Phpunit scans all files with this pattern *Test.php. So we don't need to autoload them ourselves. But we still want to autoload other supporting classes under tests/ such as fixture/stub or some parent classes.
An easy way is to look at how Composer project itself is setting up the phpunit test. It's actually very simple. Note the line with "bootstrap".
reference: https://github.com/composer/composer/blob/master/phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Composer Test Suite">
<directory>./tests/Composer/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>slow</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/Composer/</directory>
<exclude>
<file>./src/Composer/Autoload/ClassLoader.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
reference: https://github.com/composer/composer/blob/master/tests/bootstrap.php
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman#naderman.de>
* Jordi Boggiano <j.boggiano#seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
error_reporting(E_ALL);
$loader = require __DIR__.'/../src/bootstrap.php';
$loader->add('Composer\Test', __DIR__);
The last line above is autoloading phpunit test classes under the namespace Composer\Test.
None of these answers were what I was looking for. Yes PHPUnit loads test files, but not stubs/fixtures. Chaun Ma's answer doesn't cut it because running vendor/bin/phpunit already includes the autoload, so there's no way to get an instance of the autoloader to push more paths to it's stack at that point.
I eventually found this in the docs:
If you need to search for a same prefix in multiple directories, you
can specify them as an array as such:
{
"autoload": {
"psr-0": { "Monolog\\": ["src/", "lib/"] }
}
}
There is a really simple way to set up phpunit with autoloading and bootstap. Use phpunit's --generate-configuration option to create your phpunit.xml configuration in a few seconds-:
vendor/bin/phpunit --generate-configuration
(Or just phpunit --generate-configuration if phpunit is set in your PATH). This option has been available from version phpunit5 and upwards.
This option will prompt you for your bootstrap file (vendor/autoload.php), tests and source directories. If your project is setup with composer defaults (see below directory structure) the default options will be all you need. Just hit RETURN three times!
project-dir
-- src
-- tests
-- vendor
You get a default phpunit.xml which is good to go. You can of course edit to include any specialisms (e.g. colors="true") you require-:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
If you are using PHPUnit 7 you can make your classes from src/ folder to autoload in tests like this:
Ensure that your composer.json file looks similar to this:
{
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^7"
}
}
To apply changes in composer.json run command:
composer install
Finally you can run tests in tests/ folder:
./vendor/bin/phpunit tests/

Composer autoloader can't find PHPUnit when running tests

I am updating a legacy PHP project to use composer, and implementing PHPUnit. unfortunately I have run into a few issues. When running PHPUnit
Fatal error: Class 'PHPUnit_Framework_TestCase' not found
composer.json
{
"require": {
"phpunit/phpunit": "^8.0",
"phpoffice/phpspreadsheet": "^1.6"
},
"autoload": {
"psr-4": {"Biz\\": "src/php/Classes"}
}
}
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="vendor/autoload.php"
verbose="true">
<testsuites>
<testsuite name="Random Tests">
<directory>./src/test/random/*Test.php files</directory>
</testsuite>
</testsuites>
</phpunit>
Directory structure
Command line being executed
$ ./vendor/bin/phpunit ./src/test/random/SampleTest.php
I am running it using git-bash. executing from visual studio code gives the same result. I have read, implemented the issue as described in Autoloading classes in PHPUnit using Composer and autoload.php
Test Case
<?php
class SampleTest extends \PHPUnit_Framework_TestCase {
public function testUserClassInheritance(){
global $mysqlConn;
echo "testing";
$this->assertTrue(true);
$user = new Bruger;
}
}
PHPUnit_Framework_TestCase does not exist in PHPUnit version 8, which is your minimum specified version. As of (I think) PHPUnit version 5, it's using namespaces, so your test case should be named \PHPUnit\Framework\TestCase.
You can downgrade your PHPUnit requirement to an older version, or (preferably) update your tests to meet the new naming style.

PHPUnit test error, cannot find class

I'm new in PHPUnit and unit-testing, so I was install PHPUnit and phar via composer and everything had been going fine until I was try to start my simple test. I'm using PhpStorm where I can see all classes were autoload, but when I trying to start my test I got an error:
Fatal error: Class 'PharIo\Manifest\Simple' not found in C:\xampp\htdocs\mydocs\
I don't understand why he is looking for It in folder upper than PHPUnit is exists ?
I was trying to configure autoload section in composer.json and checking settings in phpunit.xml but nothing works.
Add:
I have to reinstall PHPUnit without PharIO, so now I have a little bit of progress, now I have a situation where I can test my class if I make require_once line with a name of the tested class. It looks like:
require_once '../src/Simple.php';
class SimpleTest extends PHPUnit_Framework_TestCase
{
public function testAdd() {
$sum = new Simple();
$this->assertEquals(5, $sum->add(2, 3));
}
}
So my simple class is:
class Simple {
public function add($a, $b) {
return (int) $a + (int) $b;
}
}
But, of course, I want to use namespaces. I try to make some changes based on this question: Autoloading classes in PHPUnit using Composer and autoload.php (I was try even use that repo for test, but an error is still exists) but nothing works for me. I was try to edit my autoload section in the composer.json like this
"autoload": {
"psr-4": {
"app\\": "src/"
}
},
But an error is still exists, another words autoload cannot see It. I was create phpunit.xml and phpunit.dist.xml with a same settings
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
and I made tests/bootstrap.php too with
require_once '../vendor/autoload.php';
I know this is an old question, but maybe you need to do
composer dump-autoload for composer to generate the map of classes.
I wasted 30mins trying to understand why PHPUnit was giving me:
Cannot stub or mock class or interface XXX because it doesn't exists
You should specify the script with autoloading classes.
You can either specify the file with autoloading in XML-file, as suggested in the other answer, or just by specifying --bootstrap option in your command to run tests:
phpunit --bootstrap vendor/autoload.php tests
Composer's autoload relies on configuration located in the vendor/autoload.php file which needs to be loaded at some point in your execution thread. You application already includes this and that's why it works, but the tests use a different entry point so you need to configure it with a file called phpunit.xml.dist.
Assuming your file structure is something like:
app/
src/
tests/
bootstrap.php <- create it in your test folder
vendor/
...
composer.json
composer.lock
phpunit.xml.dist <- create it if does not exist
You can see the various options here, but for a basic config, you can use this.
File phpunit.xml.dist:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php">
</phpunit>
File tests/bootstrap.php:
require_once '../vendor/autoload.php';
You should run phpunit from the root.

I can't test with phpunit: Cannot open file "autoload.php"

Config :
PHPUNIT: 4.5.0
PHP : 5.4.12
Server: Wamp
Composer: version 1.0-dev 2015-02-17 21:55:44
composer.json :
{
"require-dev": {
"phpunit/phpunit": "4.5.*"
}
}
autoload.php :
<?php
date_default_timezone_set("Europe/Paris");
require __DIR__.'/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
'Hangman' => __DIR__.'/src',
'Symfony' => __DIR__.'/vendor',
));
$loader->register();
phpunit.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="autoload.php"
>
<testsuites>
<testsuite name="hangman">
<directory>tests/Hangman/Tests</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory>vendor</directory>
</blacklist>
</filter>
</phpunit>
Problem :
I executed : phpunit --bootstrap autoload.php tests
My Error : Cannot open file "autoload.php"
Can you help me ?
I am working with PHPUnit 7 and basis on that I have mentioned solution in the below which is tested and working 100%
If you are windows user then type following on cmd:
"vendor/bin/phpunit" --bootstrap ./vendor/autoload.php ./tests/EmailTest
Make sure I have updated ./ before vendor and tests.
You could give bootstrap="vendor/autoload.php" a try in your phpunit.xml. Then PHPUnit and your Tests would use the Composer Autoloader.
Or you could require the Composer Autoloader (in addition to Symfony's UCL) in your autoload.php by adding require 'vendor/autoload.php';. This results in two autoloaders being registered.
Then run:
composer update - to fetch dependencies and rebuild the autoloading files
phpunit - to execute a test run
You don't need to run phpunit with --bootstrap, because the directive is already set in your phpunit.xml.
I think your folder layout is not right.
You start with this:
c:\wamp\www\yourproject
\src
\tests
\- phpunit.xml.dist
\vendor
\composer.json
add phpunit to the require-dev section of your composer.json
add "bin-dir" : "bin", so that phpunit.bat lives in c:\wamp\www\yourproject\bin\phpunit.bat and not in c:\wamp\www\yourproject\vendor\bin\phpunit.bat
composer install or update
exec c:\wamp\www\yourproject\bin\phpunit.bat -c c:\wamp\www\yourproject\tests\phpunit.xml.dist

'Class not found' when using namespaces in PHPUnit

I'm new to PHPUnit and am having some trouble setting it up to access my PHP files. The directory structure I'm using for my app is this:
./phpunit.xml
./lib/Application/
-> Dir1/File1.php (namespace = Application\Dir1)
-> Dir1/File2.php
-> Dir2/File1.php (namespace = Application\Dir2)
./tests/Application/Tests
-> Test1.php (namespace = Application\Tests)
-> Test2.php
In my PhpUnit.xml, I have:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="false">
<testsuites>
<testsuite name="Application">
<directory>./tests/Application/Tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
<log type="json" target="/tmp/phpunit-logfile.json"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>
</phpunit>
And in one of my test files, I open with:
namespace Application\Tests;
use Application\Dir1\File1;
class MyTest extends File1 {}
But it keeps on saying:
Class 'Application\Dir1\File1' not found
Where am I going wrong?
If you installed PHPUnit using Composer then you can use Composers autoloader. The easiest way to do so would be to add:
"autoload":{
"psr-0":{
"your-app-directory":""
}
}
to composer.json
Even if you use use, you still have to include the file, either by using include, require, include_once, or require_once, or by using spl_autoload_register to include the file, like so:
spl_autoload_register(function ($class)
{
include '\lib\\' . $class . 'php';
});
When you then try to use Application\Dir1\File1 the script will automatically run include '\lib\Application\Dir1\File1.php'
I had the same issue.
I'm using composer as well and the only thing that solved it for me was the following:
add to your composer.json file in the autoload section a class map section with your root namespace
"autoload": {
"classmap": ["namespaceRoot/"]
}
execute composer dump-autoload command in order to recreate your autoload files (with all the class mappings!)
I found this really useful class autoloader by Jonathan Wage which allows PHPUnit tests to access namespaces from different directories. In my bootstrap.php, I just specified the location and associated module namespace:
require_once 'SplClassLoader.php';
$classLoader = new SplClassLoader('Application', dirname(__FILE__) . '/../lib');
$classLoader->register();

Categories