Phil Yu Phil Yu - 1 month ago 13
PHP Question

PHP: Composer auto-load not working

Actually Iam new to PHP. I am running this from a nearly empty folder (actually following along a Lara-casts tutorial: Design a Fluent API with TDD).

My directory structure looks like


  • src


    • Expression.php


  • tests


    • ExpressionTest.php


  • vendor

  • composer.json

  • composer.lock



Inside composer.json:

{
"require-dev": {
"phpunit/phpunit": "^5.2"
},
"autoload": {
"psr-4": {
"": "src/"
}
}
}


Inside ExpressionTest.php:


class ExpressionTest extends PHPUnit_Framework_TestCase
{
/** @test */
public function it_finds_a_string()
{
$regex = Expression::make()->find('www');

$this->assertRegExp($regex, 'www');
}
}


Inside Expression.php

<?php

class Expression
{

}


I then run composer dump-autoload and run phpunit but I still get:

"Fatal error: Class 'Expression' not found in
C:\Users\nobis\code\testing2\tests\ExpressionTest.php on line 8"


Is there something wrong with my syntax? My understanding of Composer is very basic. Thanks in advance.

Answer

PHPUnit does not know about Composer natively, therefore without configuring PHPUnit, it will not know about your autoloader setup.

try running PHPunit with --bootstrap vendor/autoload.php to tell it to load with your autoload file.

If that does not work, check your namespace value in your Composer configuration (i.e. "": "src/" might need to change.)