PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] Namespaces not working with Symfony and Composer

 January 16, 2022     autoload, composer-php, php     No comments   

Issue

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.



Answered By - prgrm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing