S
S
ShishMish2020-03-16 11:21:29
PHP
ShishMish, 2020-03-16 11:21:29

Codeception, how to connect to the 2nd database in your own helper?

I am writing my own helper in which I want to implement functions for their subsequent call in tests.
In the config codeception.yml:

paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    enabled:
        - Db
        - PhpBrowser
    config:
        Db:
            dsn: 'mysql:host=localhost;port=3309;dbname=db_1'
            user: 'test_user'
            password: 'passwd'
            databases:
                db2:
                    dsn: 'mysql:host=localhost;port=3309;dbname=db_2'
                    user: 'test_user'
                    password: 'passwd'
        PhpBrowser:
            url: 'http://api.localhost/'


Here is the class code:
<?php

namespace Helper;


use Codeception\Module;

class Universal extends Module
{
    private $db1;
    private $db2;

    public function __construct()
    {
        $container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
        $this->db1 = new Module\Db($container, $this->_getConfig('Db'));
        $this->db1->_getDbh();
        $this->db1->_initialize();
        $this->db2 = new Module\Db($container, $this->_getConfig('db2'));
        $this->db2->_getDbh();
        $this->db2->_initialize();
    }
}


When running codecept build:
PHP Notice:  Undefined index: default in /home/shishmish/Work/gc/vendor/codeception/module-db/src/Codeception/Module/Db.php on line 351
PHP Notice:  Undefined index: dsn in /home/shishmish/Work/gc/vendor/codeception/module-db/src/Codeception/Module/Db.php on line 552
PHP Notice:  Undefined index: user in /home/shishmish/Work/gc/vendor/codeception/module-db/src/Codeception/Module/Db.php on line 552
PHP Notice:  Undefined index: password in /home/shishmish/Work/gc/vendor/codeception/module-db/src/Codeception/Module/Db.php on line 552

In Db.php line 560:
                                                              
  Db: invalid data source name while creating PDO connection

I want to be able to connect to the 1st and 2nd database as needed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-03-16
@Nc_Soft

Somehow you are doing something strange in the config, it would be more logical like this

config:
        Db1:
            dsn: 'mysql:host=localhost;port=3309;dbname=db_1'
            user: 'test_user'
            password: 'passwd'
        Db2:
            dsn: 'mysql:host=localhost;port=3309;dbname=db_1'
            user: 'test_user'
            password: 'passwd'

$this->db1 = new Module\Db($container, $this->_getConfig('Db1'));
$this->db2 = new Module\Db($container, $this->_getConfig('Db2'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question