Ilario Engler Ilario Engler - 1 month ago 5
PHP Question

PHPUnit Class not found

Folder structure

/app/lib/Helper.php

/tests/HelperTest.php

/vendor/autoload.php

composer.json

{
"require-dev": {
"phpunit/phpunit": "*"
},

"autoload": {
"psr-4": {
"Datapark\\LPS\\": "app/"
}
},

"autoload-dev": {
"psr-4": {
"Datapark\\LPS\\Tests\\": "tests/"
}
},
}


Helper.php

<?php

namespace lib;

class Helper
{
public function array_get($array, $key, $default = null)
{
// code
}
}


HelperTest.php

<?php

use lib\Helper;

class HelperTest extends \PHPUnit_Framework_TestCase
{
public function test_array_get()
{
$helper = new Helper();

}
}


Command I run on the Server
[Debian 8 / PHP7]


phpunit --bootstrap vendor/autoload.php tests


Error I get


1) HelperTest::test_array_get

Error: Class 'lib\Helper' not found


lib\Helper is loaded via namespace and my IDE (PhpStorm) also recognize it.
Struggling around already a few hours and don't get it to work.

Answer

Your autoload configuration says:

        "Datapark\\LPS\\": "app/"

Which means something like:

classes in app directory have Datapark\LPS\ namespace prefix.

So as an example class in file app/lib/Helper.php should have namespace Datapark\LPS\lib. Therefore you need to change namespace declaration for Helper class to:

namespace Datapark\LPS\lib;

And there is similar issue for your test folder.