Title: Issue with Loading Module in PHPUnit for Testing: "Class Not Found" Error

Hello everyone,

I’m currently facing an issue with loading a module in PHPUnit for testing purposes. I have a module called “Candidat” in my Laminas (formerly Zend Framework) application, and I’m trying to write unit tests for it.

I’ve followed the recommended approach of setting up a separate PHPUnit test class and using the Laminas Test package. However, when I attempt to access the module’s classes or services within the test class, I receive a “Class not found” error.

This can get tricky or at least it has been for me.

From which directory are you running PhpUnit? Are you trying to unit test the module, at the module level, or are you trying perform integration testing at the application level?

I have PHPUnit installed in the “vendor” directory. I want to test all modules in my application. Inside each module, I have a folder called “test” which contains the test classes and a “bootstrap” file. When I run the tests, the service manager does not seem to work properly.

Have you setup the correct Psr-4 information in composer.json? This is just the first thing that comes to mind.

As an example for the Application Module (the module you reference above is included):

    "autoload-dev": {
        "psr-4": {
            "ApplicationTest\\": "module/Application/test/",
            "CandidatTest\\": "module/Candidat/test/"
        }
    },

You will need an entry per module and run dumpautoload after modifying the configuration. You will also want to make sure you are in development mode.

composer dumpautoload
composer development-enable

Any additional information about the “why/how” of this:

the service manager does not seem to work properly.

Would be very helpful. Errors, response from PHPUnit, the code from the actual test class, a link to the github repo etc. The more information you can provide the better. We have no way of knowing exactly what you are trying to run currently which means all anyone can do is guess what might possibly be misconfigured / omitted etc.

It worked after I added this function to ServiceManagerGrabber

public function __construct($name = ‘’, array $data = , $dataName = ‘’)
{
parent::__construct($name, $data, $dataName);
}

thank you so much

There is no class called ServiceManagerGrabber in the entire framework:

https://github.com/search?q=org%3Alaminas%20ServiceManagerGrabber&type=code

:man_shrugging:t2:

2 Likes

I work with content management systems based on the Laminas framework