5

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.

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

I noticed that when I run:

$ vendor/bin/phpunit tests

then my tests started to work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.