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