Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

composer phpunit psr-4 autoload class not found

I am giving composer autoload a try with some phpunit testing classes, and I can't seem to get it to work. When I run phpunit from the command line, I get the following error : "PHP Fatal Error: Class ... not found".

I will give all the structure and file information. I can, so hopefully someone will spot where I went wrong.

Structure (cut down to relevant files):

composer.json
composer.lock
phpunit.xml
vendor/
tests/
    functional/
        BaseTestCase.php
        HomepageTest.php

composer.json

{
"require": {
    "php": ">=5.5.0",
    "slim/slim": "^3.1",
    "slim/php-view": "^2.0",
    "monolog/monolog": "^1.17"
},
"require-dev": {
    "phpunit/phpunit": ">=4.8 < 6.0"
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
}
}

phpunit.xml

<?xml version="1.0" encoding="utf-8" ?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="Initial tests">
            <directory>tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

test/functional/BaseTestCase.php

<?php
namespace Tests\Functional;

use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Environment;

class BaseTestCase extends \PHPUnit_Framework_TestCase
{
  ...

test/functional/HomepageTest.php

<?php
namespace Tests\Functional;

class HomepageTest extends BaseTestCase
{
   ...

I then ran an update to refresh the autoload files

$ composer update

Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

I then tried running phpunit and got a class not found error:

$ vendor/bin/phpunit

PHP Fatal error:  Class 'Tests\Functional\BaseTestCase' not found in <project-root>/tests/functional/HomepageTest.php on line 6

Just to be thorough I tried refreshing the autoload files another way, just in case:

$ composer dump-autoload

Generating autoload files
sfbagency@sfb1:~/clients/ctest/dev$ 

I also checked vendor/composer/autoload_psr4.php to make sure the Test reference is being set, and it is.

...
Tests\\' => array($baseDir . '/tests'),
...

I have Googled like crazy, but have no idea where I am going wrong.

like image 394
Finglish Avatar asked Aug 25 '16 20:08

Finglish


Video Answer


2 Answers

I had a similar problem but without syntax problem. As soon as I added a second ClassTest (ModelTwoTest.php below), the Fixtures.php class was autoloaded. If I remove 1 modelTest, only the remaining ModelTest is autoloaded without Fixtures. Weird behavior.

src/
tests/
      Models/
             ModelOneTest.php
             ModelTwoTest.php <---
      Fixtures/
             Fixtures.php
like image 173
Pierre F Avatar answered Sep 18 '22 02:09

Pierre F


Namespace directories are case sensitive. You have to rename the folder to Functional

As said in the PSR-4 documentation:

The subdirectory name MUST match the case of the sub-namespace names.

like image 27
iRaS Avatar answered Sep 19 '22 02:09

iRaS