Search code examples
phpcomposer-phpautoload

Namespaces not working with Symfony and Composer


I must be missing something ; I am trying to run some tests but the classes are never found due to namespace.

Here is my structure.

-app
    -tests
        -Unit
            -TestInterface.php
            -common
                -MyTest.php

Here is my TestInterface.php:

namespace App\Tests\Unit;


interface TestInterface
{

}

Here is my MyTest.php:

namespace App\Tests\Unit\common;

use App\Tests\Unit\TestInterface;

class MyTest implements TestInterface
{

}

Here is the relevant part of composer.json:

  "autoload": {
    "psr-4": {
      "App\\": "src/",
      "spec\\": "spec/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "App\\Tests\\": "tests/"
    }
  },

Here is the error:

PHP Fatal error:  Interface 'App\Tests\Unit\TestInterface' not found

What am I missing here?


Solution

  • Answering myself, the code is fine.

    I was loading PHPUnit from another.phar. Installed PHPUnit via composer:

    composer require --dev phpunit/phpunit ^8
    

    and used it from ./bin and it worked fine.