Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 262
Ilario Engler Avatar asked May 13 '16 06:05

Ilario Engler


2 Answers

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.

like image 183
Jakub Matczak Avatar answered Sep 19 '22 16:09

Jakub Matczak


I noticed that when I run:

$ vendor/bin/phpunit tests

then my tests started to work

like image 40
Riho Avatar answered Sep 21 '22 16:09

Riho