composer phpunit psr-4 autoload class not found
Asked Answered
S

2

10

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.

Sixtieth answered 25/8, 2016 at 20:56 Comment(1)
Try changing the directory name "functional" to "Functional".Lemmy
F
9

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.

Flatfoot answered 26/8, 2016 at 5:1 Comment(1)
stupid mistake :( so obvious. Couldn't see the wood for the trees. I guess I will have to let this question stand as a monument to stupidity. Thanks!Sixtieth
T
2

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
Trinidad answered 23/1, 2018 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.