Class not found issue (Composer, PHPUnit)

4.5k views Asked by At

Getting the following error form PHPUnit:

Fatal error: Class 'FoobarTest\Money\Money'
not found in /www/foobar/tests/FoobarTest/Money/MoneyTest.php on line 11

My structure is like:

/src/Foobar/Money/Money.php (class Money, namespace Foobar\Money)

/tests/FoobarTest/Money/Money.php (class Money, namespace FoobarTest\Money)

Autoloading done through composer:

"autoload": {
    "psr-4": {
        "Foobar\\": "src/"
    },
    "psr-0": {
        "FoobarTest\\": "tests/"
    }
},

Tried with PSR0, PSR2, PSR4, ...

MoneyTest class:

<?php
namespace FoobarTest\Money;


class MoneyTest extends \PHPUnit_Framework_TestCase
{
    // ...

Money class:

<?php
namespace Foobar\Money;


class Money
{
    // ...

Why is it trying to load FoobarTest\Money\Money instead of Foobar\Money\Money ?

1

There are 1 answers

2
zerkms On BEST ANSWER

To help php autoloader (and composer) you must import the target class using

use Foobar\Money\Money;

in your test file.

Also you probably want to give your test file a MoneyTest.php name to match the corresponding class name.