Codeception / module-yii2

Codeception module for Yii2 framework
MIT License
16 stars 36 forks source link

[Proposal] make it possible to additionaly configure `configFile` via bootstrap #13

Closed HenryVolkmer closed 4 years ago

HenryVolkmer commented 4 years ago

Proposal

What are you trying to achieve?

my application consists through reuseable Yii2-Modules, packed as independend composer packages, residing in vendor/-Folder. E.g.:

vendor/ts/users
vendor/ts/customizer
vendor/ts/partner
...

Each Yii2-Module utilize the codeception/module-yii2 in order to provide Tests. Further, each Module provides a codeception.yml-Configfile and obviosly Module-specific tests. In order to test an Application against all currently installed Modules, the Application provides also a codeception.yml-Configfile whith a include-Section:

paths:
    log: console/logs
settings:
    colors: true
    memory_limit: 1024M
include:
    - vendor/ts/users
    - vendor/ts/customizer
    - vendor/ts/partner

All Modules configured in the include-Key should be included for testing.

What do you get instead?

Since a particular Module knows nothing about the current deployed Application and config-paths, i CAN NOT provide the configFile-Key in Module's codeception.yml. Therefore, a ModuleConfigException will be thrown:

[Codeception\Exception\ModuleConfigException]                     
  Yii2 module is not configured!                                    

  Options: configFile are required                                  
  Please, update the configuration and set all the required fields

proposed Changes (non breaking):

In current Version of Yii2.php, the configFile-Configuration MUST BE set by each Suite.

I propose to recognisize a Key Yii2ConfigFile (string) in the $_SERVER-var and make it possible to additionaly configure the configFile via codeception's --bootstrap parameter:

run tests:

vendor/bin/codeception run --bootstrap='test/codeception-bootstrap.php'

test/codeception-bootstrap.php:

<?php
// test/codeception-bootstrap.php
$_SERVER['Yii2ConfigFile'] = dirname(__FILE__) . '/codeception-local.php';

codeception-local.php:

<?php
require __DIR__ . '/../common/config/bootstrap.php';
Yii::setAlias('@log', '@test/logs');

return yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/../common/config/main.php',
    require __DIR__ . '/../common/config/main-local.php',
    require __DIR__ . '/config/main-local.php',
    [
        'class' => 'ts\core\base\WebApplication',
        'components' => [
            'request' => [
                'cookieValidationKey' => 'HEkQw2kzuzvChn8TW2opVyft-OYk5X7y',
            ],
        ],
    ]
);
implementation

Required Changes in Yii2.php

    // ...

    protected function validateConfig()
    {
        // Begin of Implementation
        if (isset($_SERVER['Yii2ConfigFile']) && !isset($this->config['configFile'])) {
            $this->config['configFile'] = $_SERVER['Yii2ConfigFile'];
        }
        // End of Implementation

        parent::validateConfig();

        // ...
    }

    // ...

related issues

SamMousa commented 4 years ago

Shouldn't those modules have their own config file? If the code is modular then the tests can be too. That way you can test the modules separately in their own repo.

I have the same issue (but my modules are in a mono repo) so I use a relative path (ie the codeception file does have knowledge of the directory structure of the parent; I agree that this is currently an issue.

I am not a fan of your proposed solution though; since it means module specific adaptations for a generic problem. A cleaner solution, in my opinion would be variable support in the yaml files.

This way you could have a variable defined in your primary codeception file and your codeception.dist.yml in the module can use this variable. Note that the parser we use (symfony/yaml) already supports key merging so extending this to support the included files could be relatively simple

Also there is a --config-option param that might be of use to you.

HenryVolkmer commented 4 years ago

i understand your point, ty for your feedback! This manual also solved this issue.

Btw, i cant find an --config-option param in the Codeception-Docs, can you please provide a Link?

SamMousa commented 4 years ago

Btw, i cant find an --config-option param in the Codeception-Docs, can you please provide a Link?

I can't, just noticed it in the source while replying to your post. So not sure if there are docs, if you can't find them I'm not going to try looking for them either ;-) Glad you have your issue solved!