Issue with laminas-test unit testing

I’m new to Laminas framework and Unit testing and working with a Laminas framework project and I want to start unit testing in that application. I used laminas-test package and followed their tutorial.
when I execute the vendor/bin/phpunit I’m getting the following error.

1) CompanyTest\Controller\ManageControllerTest::testDirectoryAction
Laminas\ServiceManager\Exception\ServiceNotCreatedException: Service with name "ModuleManager" could not be created. Reason: Parameter to Laminas\ModuleManager\ModuleManager's Laminas\ModuleManager\ModuleManager::setModules method must be an array or implement the Traversable interface

/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-servicemanager/src/ServiceManager.php:652
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-servicemanager/src/ServiceManager.php:218
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-mvc/src/Application.php:263
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:230
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:255
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:344
/var/lxp/sites/cammslearning/production/application/module/Company/test/Controller/ManageControllerTest.php:29

Caused by
Laminas\ModuleManager\Exception\InvalidArgumentException: Parameter to Laminas\ModuleManager\ModuleManager's Laminas\ModuleManager\ModuleManager::setModules method must be an array or implement the Traversable interface

/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-modulemanager/src/ModuleManager.php:262
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-modulemanager/src/ModuleManager.php:68
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-mvc/src/Service/ModuleManagerFactory.php:82
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-servicemanager/src/ServiceManager.php:645
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-servicemanager/src/ServiceManager.php:218
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-mvc/src/Application.php:263
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:230
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:255
/var/lxp/sites/cammslearning/production/application/vendor/laminas/laminas-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:344
/var/lxp/sites/cammslearning/production/application/module/Company/test/Controller/ManageControllerTest.php:29

This is the sample unit test I wrote

<?php
namespace CompanyTest\Controller;

use Company\Controller\ManageController;
use Laminas\Stdlib\ArrayUtils;
use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class ManageControllerTest extends AbstractHttpControllerTestCase {
    protected $traceError = true;

    protected function setUp(): void {
        $configOverrides = [];

        $configs = [
            include __DIR__ . '/../../../../config/application.config.global.php',
            include __DIR__ . '/../../../../config/autoload/testing.global.php',
        ];

        $mergedConfig = ArrayUtils::merge(
            $configs,
            $configOverrides
        );
        $this->setApplicationConfig($mergedConfig);

        parent::setUp();
    }

    public function testDirectoryAction () {
        $this->dispatch('/company/directory');
        $this->assertResponseStatusCode(200);
        $this->assertModuleName('Company');
        $this->assertControllerName(ManageController::class);
        $this->assertControllerClass('ManageController');
        $this->assertMatchedRouteName('company/directory');
    }
}

I also added autoload entry in the composer.json as well.

   "autoload-dev": {
        "psr-4": {
            "ApplicationTest\\": "module/Application/Test/",
            "CompanyTest\\": "module/Company/test/"
        }
    },    

How do I get this running? what are the possible causes for this?

The other relevant files.

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="./vendor/autoload.php" >
    <testsuites>
        <testsuite name="Laminas MVC Application Test Suite">
            <directory>./module/Application/Test</directory>
        </testsuite>
        <testsuite name="Login">
            <directory>./module/Learner/Authentication/test</directory>
        </testsuite>
        <testsuite name="Company">
            <directory>./module/Company/test</directory>
        </testsuite>
    </testsuites>
</phpunit>

Folder structure

.
└── application/
    ├── config/
    │   ├── application.config.global.php
    │   └── autoload/
    │       └── testing.global.php
    ├── module/
    │   └── Company/
    │       ├── Controller/
    │       │   └── ManageController.php
    │       └── test/
    │           └── Controller/
    │               └── ManageControllerTest.php
    ├── composer.json
    └── phpunit.xml

Welcome to the forum :grin:

Is the company module loading as expected during normal execution?

You have to register the company modules in either modules.config.php or application.config.php

Might want to look at the docs for laminas component installer and run a google search for advanced configuration tricks.

I would link you but I’m on mobile.

1 Like

This explains in-depth the why’s and how’s of configuring a laminas application.

This covers how modules/components are correctly setup in a laminas application by the component installer.

The error you are encountering sounds like you do not have an entry in the proper file for the Company module. It requires an entry in the modules array.

The second link addresses your issue here (I think)

1 Like

Hi Tyrsson, Thank you for replying.

Yes, the company module is loading during the normal execution and it is registered in the application.config.global.php.

I’m only getting this error when trying to write and execute a unit test and I get the same error for other modules as well.

I’ll check the laminas-component-installer.

Can you post the code from application.config.global.php?

Where is the module/Application directory?

How did you originally set this project up? Did you start with the skeleton application?