U
U
usdbuy2021-12-21 07:30:09
PHP
usdbuy, 2021-12-21 07:30:09

Phpunit not finding class from neighbor project?

Hello everyone, please tell me about the class loading

setting

.

namespace Helper;

class Crypt
{
}


composer autoload of main project

"autoload": {
       "psr-4": {        
        "Helper\\": "library/Helper/"        
    }
}


I added a component along the path app/xxx/phpunit-tests/ where I planned to create classes from the main project for unit testing

when running the test php vendor/bin/phpunit cannot find the class Error: Class 'Helper\Crypt' not found

this test is located on path app/xxx/phpunit-tests/test

<?php

use Helper\Crypt;
use PHPUnit\Framework\TestCase;

class CryptTest extends TestCase
{
    public function testSuccess(): void
    {
        $res = Crypt::get()->crypt(42144);

        var_dump($res);
    }
}


composer of the given test environment

{
    "autoload": {
        "psr-4": {
            "Helper\\": "../../library/Helper/"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^9.5"
    }
}


phpunit.xml

<phpunit bootstrap="vendor/autoload.php"
    colors="true"
    verbose="true"
    stopOnFailure="false">
    <testsuites>
        <testsuite name="Tests">
            <directory>test</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">app</directory>
    </whitelist>
    </filter>
</phpunit>


How can I make all classes from this app folder available? I run the tests in the container, the files of the main project and the component are thrown there .

UPD
added screenshots for clarity
y8Uo6Dl.png
6EqCwYQ.png
f4dyWtg.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spaceatmoon, 2021-12-21
@spaceatmoon

Hmm, I don't know why, but the answer was deleted or deleted itself. Daria Motorina what kind of magic?
Basically, you're on the wrong path. You must specify the full path from the root, not relative.

"autoload": {
        "psr-4": {
            "Spaceatmoon\\": "src/",
       "Mue\\": "src/hue/mue"
        }
    },

Then you need to update autoload
php composer.phar dump-autoload

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question