Ilario Engler
Ilario Engler

Reputation: 2469

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.

Upvotes: 5

Views: 7484

Answers (2)

Riho
Riho

Reputation: 4593

I noticed that when I run:

$ vendor/bin/phpunit tests

then my tests started to work

Upvotes: 5

Jakub Matczak
Jakub Matczak

Reputation: 15656

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.

Upvotes: 7

Related Questions