automatix
automatix

Reputation: 14542

How to get configs to the Config service in the testing context in Zend Framework 2?

I have a ZF2 application and want to write some integration tests for in. I'm using PHPUnit v6 (no zend-test). PHPUnit is set up and the Bootstrap class implemented. Everything is/was working.

For normal requests the framework scans the config folders (usually defined in the config/application.config.php: ['module_listener_options']['config_glob_paths']) and merges all the configs for me. But now I'm in the testing context and need some configs from a custom config file (e.g. config/autoload/module/audit-logging.local.php) -- and cannot find out, how to add/merge them to/with the other configs.

How to integrate configs from custom config files and make them available for the application, when running PHPUnit tests?

Upvotes: 0

Views: 373

Answers (1)

localheinz
localheinz

Reputation: 9582

How about creating environment-specific configuration files, for example:

config
├── application.config.php
├── autoload
│   └── global.php
└── environment
    ├── development.php
    ├── production.php
    └── testing.php

and then adjusting config/application.config.php to this:

return [
    'modules' => $modules,
    'module_listener_options' => [
        'config_glob_paths' => [
            __DIR__ . '/autoload/{,*.}{global,local}.php',
            __DIR__ . '/environment/' . (\getenv('APPLICATION_ENVIRONMENT') ?: 'production') . '.php',
        ],
        'module_paths' => [
            __DIR__ . '/../module',
            __DIR__ . '/../vendor',
        ],
    ],
];

Now all you need to do is configure the APPLICATION_ENVIRONMENT environment variable, and set it to testing, for example. Then you can configure the application for specific environments with corresponding configuration files in config/environment.

<phpunit>
    <php>
        <env name="APPLICATION_ENVIRONMENT" value="testing" />
    </php>
    <testsuites>
        <testsuite name="Integration Tests">
            <directory>.</directory>
        </testsuite>
    </testsuites>
</phpunit>

Works fine for me.

Upvotes: 1

Related Questions