Namespaces not working with Symfony and Composer - php

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?

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.

Related

autoloading composer can't find class

I was try on a beginners course php. I had a issue about autouploading with composer. I had Fatal error: Uncaught Error: Class 'app\Email' not found in...
My directory structure looks like this
autoloading
-app
Email.php
Person.php
-src
-composer.json
-index.php
in index.php
require_once "vendor/autoload.php";
$email = new app\Email();
$person = new app\Person();
And in composer.json had
"autoload": {
"psr-4": {
"Dung6\\Autoloading\\": "src/",
"App\\": "app/"
}
},
in "App\\": "app/" I was try ./app, /app/,... and I was composer dump-autoload every time I change. But nothing work at all. Some one can check it for me. Thank you!

Composer psr-4 autoload class not found error after changing prefix

i've already been looking for solutions to my problem but could'nt find any so far.
{
"name": "petersil98/thresh",
"version": "1.0.0",
"type": "library",
"autoload": {
"psr-4": {
"Thresh\\": "src/"
}
}
}
This is my public/test.php:
<?php
require_once '../vendor/autoload.php';
use Thresh\Helper\Config;
Config::setPlatform("euw1");
And this is my src/Helper/Config.php:
<?php
namespace Thresh\Helper;
class Config{...}
This is the error i get: Fatal error: Uncaught Error: Class 'Thresh\Helper\Config' not found
First i had my psr-4 autoload registered with the prefix 'src'
"autoload": {
"psr-4": {
"src\\": "src/"
}
}
After changing the psr-4 autoload prefix to Thresh (and updating the namespaces) and running composer dump-autoload it doesnt work anymore
PS: composer dump-autoload returns Generated autoload files containing 0 classes

TestCase in laravel package not found

I'm converting my open source app as a package and it always throws error
PHP Fatal error: Class 'Laracommerce\Tests\TestCase' not found in .... on line 18
Based on other comments on every search I made, I just need to define it in my package composer.json's autoload-dev the location of my tests but still getting the error.
You are calling Laracommerce\Tests namespace but into the composer file you declared Laracommerce\Core\Tests
you need to:
a. change classes namespaces Laracommerce\Core\... to Laracommerce\...
b. or simply modify your composer file like this
"autoload":{
"psr-4": {
"Laracommerce\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Laracommerce\\Tests\\": "tests/"
}
},
Then you need to run composer dump-autoload
Try running a composer install, sometimes I get the same error but it gets fixed after that.

Composer psr-4 autoload not working after deployment

I have my own little MVC framework and I use composer psr-4 autoloading.
On my own computer it works perfectly fine, but when I deployed it to my Ubuntu server it did not work anymore. (it doesn't find any classes anymore) I have tried a lot of things but it just won't work whatever I try...
What I have tried:
composer dump-autoload
composer update
removing everything and uploading again
searching on internet for a couple hours... :(
This is my composer.json:
{
"autoload": {
"psr-4": {
"App\\": "app",
"Core\\": "core",
"Magister\\": "vendor/Magister"
}
},
"require": {
"philo/laravel-blade": "^3.1"
}
}
I just don't get it why it's not working on my server....
I am using an other version of php on my server: 7.1, and I am using 5.6 on my computer, but this shouldn't make any difference right?
How do I fix this problem? I just don't get it why it happens.... :(
EDIT:
My code:
Index.php:
<?php
require "core/app.php";
$app = new \Core\App();
echo $app->start();
app.php:
<?php
namespace Core;
require "./vendor/autoload.php";
class App
{
function start()
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL ^ E_DEPRECATED);
$MC = new Routing();
// This is where it fails. Get the error: "class Core\Routing not found"
Routing.php:
<?php
namespace Core;
Use App\routes;
class Routing
{
private $parameters = [];
public function GetMC($Getroute){
}
}
File structure on server:
I have excluded the vendor map from the tree
okay... I have fixed it.
I have changed my composer.json to this:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Core\\": "core/",
"Magister\\": "vendor/Magister/"
},
"classmap": [
"app/",
"core/",
"vendor/Magister/"
]
},
"require": {
"philo/laravel-blade": "^3.1"
}
}
If you want to use psr-4 you need to capitalize your directories to
app
- Modules
- Controllers
- Views
-- Layouts
...
Please refer to this post as to why your autoloading isn't working.

Using Composer Autoloader with PSR-4

I'm looking at examples, and I cannot get my code to work.
Directory Structure
app
src
company
FileExport
FileExport.php
FileExportInterface.php
Validator
vendor
...
My composer.json
"require": {
"monolog/monolog": "1.9.1",
"ilya/belt": "2.1.1"
},
"autoload": {
"psr-4": {"Company\\": "src"}
}
Namespace is Company\FileExport.
Classes in vendor work fine, but not mine. I've run composer update as well.
Your autoload should look like so
"autoload": {
"psr-4": {"Company\\": "src/company/"}
}

Categories