PHPUnit ignores config / autoload / ... files in Zend Framework 2?

In my ZF2 application, I follow standard settings approaches. There is:

  • /config/application.config.php - default application default settings

  • /config/autoload/global.php - default application default settings

  • /config/autoload/local.php - application specific settings for specific applications

  • /config/autoload/MODULENAME.local.php - module-specific module settings

  • /module/MODULENAME/config/module.config.php - default settings for a specific module

Now, when I run PHPUnit in the test folder of the module, I can not use the values ​​from global.php and the values ​​from local (although the configuration files are included - I checked this).

For example: PHPUnit calls my special assistant of the form ContentForEnvironment , which contains this code: $currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment']; .

 /path/to/project/module/Application/test# phpunit PHPUnit 3.7.13 by Sebastian Bergmann. Configuration read from /path/to/project/module/Application/test/phpunit.xml E Time: 0 seconds, Memory: 8.75Mb There was 1 error: 1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed Undefined index: environment /path/to/project/vendor/MyNamespace/library/MyNamespace/View/Helper/ContentForEnvironment.php:32 /path/to/project/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:400 /path/to/project/module/Application/view/layout/layout.phtml:25 /path/to/project/module/Application/view/layout/layout.phtml:25 /path/to/project/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:507 /path/to/project/vendor/zendframework/zendframework/library/Zend/View/View.php:205 /path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php:126 /path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:472 /path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207 /path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/View/Http/DefaultRenderingStrategy.php:136 /path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:472 /path/to/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:207 /path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:332 /path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:285 /path/to/project/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:255 /path/to/project/module/Application/test/ApplicationTest/Controller/IndexControllerTest.php:46 FAILURES! Tests: 1, Assertions: 0, Errors: 1. 

The environment parameter is set to global.php and local.php and works when I run the application in a browser.

When I set this option in the default module configuration file ( module.config.php ), the parameter value can be read.

What is wrong here?


EDIT

/module/Application/test/Bootstrap.php

 <?php namespace ApplicationTest; use Zend\Loader\AutoloaderFactory; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\ArrayUtils; use RuntimeException; error_reporting(E_ALL | E_STRICT); chdir(__DIR__); class Bootstrap { protected static $serviceManager; protected static $config; protected static $bootstrap; public static function init() { // Load the user-defined test configuration file, if it exists; otherwise, load if (is_readable(__DIR__ . '/phpunit.config.php')) { $testConfig = include __DIR__ . '/phpunit.config.php'; } else { $testConfig = include __DIR__ . '/phpunit.config.php.dist'; } $zf2ModulePaths = array(); if (isset($testConfig['module_listener_options']['module_paths'])) { $modulePaths = $testConfig['module_listener_options']['module_paths']; foreach ($modulePaths as $modulePath) { if (($path = static::findParentPath($modulePath)) ) { $zf2ModulePaths[] = $path; } } } $zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR; $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : ''); static::initAutoloader(); // use ModuleManager to load this module and it dependencies $baseConfig = array( 'module_listener_options' => array( 'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths), ), ); $config = ArrayUtils::merge($baseConfig, $testConfig); $serviceManager = new ServiceManager(new ServiceManagerConfig()); $serviceManager->setService('ApplicationConfig', $config); $serviceManager->get('ModuleManager')->loadModules(); static::$serviceManager = $serviceManager; static::$config = $config; } public static function getServiceManager() { return static::$serviceManager; } public static function getConfig() { return static::$config; } protected static function initAutoloader() { $vendorPath = static::findParentPath('vendor'); if (is_readable($vendorPath . '/autoload.php')) { $loader = include $vendorPath . '/autoload.php'; } else { $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false)); if (!$zf2Path) { throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'); } include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; } AutoloaderFactory::factory(array( 'Zend\Loader\StandardAutoloader' => array( 'autoregister_zf' => true, 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__, ), ), )); } protected static function findParentPath($path) { $dir = __DIR__; $previousDir = '.'; while (!is_dir($dir . '/' . $path)) { $dir = dirname($dir); if ($previousDir === $dir) return false; $previousDir = $dir; } return $dir . '/' . $path; } } Bootstrap::init(); 

/module/Application/test/phpunit.config.php

 <?php return array( 'modules' => array( 'Application', ), 'module_listener_options' => array( 'config_glob_paths' => array( '../../../config/autoload/{,*.}{global,local}.php', ), 'module_paths' => array( 'module', 'vendor', ), ), ); 

/module/Application/test/phpunit.xml

 <?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="Bootstrap.php"> <testsuites> <testsuite name="Application Unit Tests"> <directory>./ApplicationTest</directory> </testsuite> </testsuites> <filter> <whitelist> <directory>/path/to/project/module/Application/src/Application/</directory> <!-- <exclude> <directory suffix=".phtml">/path/to/project/module/Application/</directory> <directory suffix=".php">/path/to/project/module/Application/test/</directory> </exclude> --> </whitelist> </filter> </phpunit> 

EDIT

Modified /module/Application/test/Bootstrap.php

 // use ModuleManager to load this module and it dependencies $baseConfig = array( 'module_listener_options' => array( 'module_paths' => $zf2ModulePaths, ), 'modules' => array( 'Application', ) ); // Load the user-defined test configuration file, if it exists; otherwise, load if (is_readable(__DIR__ . '/phpunit.config.php')) { $testConfig = include __DIR__ . '/phpunit.config.php'; } else { $testConfig = include __DIR__ . '/phpunit.config.php.dist'; } $config = ArrayUtils::merge($baseConfig, $testConfig); 

But errors still exist.

+4
source share
2 answers

I had the same problem and a simple line was resolved in the test controller, the setUp method.

 $serviceManager = Bootstrap::getServiceManager(); $this->setApplicationConfig(Bootstrap::getConfig()); 

My test controller extends AbstractHttpControllerTestCase, and I use the download found on the zend website.

+2
source

I'm not sure if this is correct, but try to take a picture. Just comment on this line in your PHPUnit Bootstrap and try running the test.

 $config = ArrayUtils::merge($baseConfig, $testConfig); 

Basically, my hunch is that you crack your $ testConfig into $ baseConfig and combine it again with $ testConfig. This defeats his goal. Please try this and let me know. I will try to run your configuration a bit later.

0
source

Source: https://habr.com/ru/post/1479400/


All Articles