Codeception / Codeception

Full-stack testing PHP framework
http://codeception.com
MIT License
4.77k stars 1.3k forks source link

Database component parameters are overwritten #5794

Closed DiscipleOfShinku closed 4 years ago

DiscipleOfShinku commented 4 years ago

What are you trying to achieve?

To test an Yii2 application with two databases.

What do you get instead?

With one database tests work as expected:

test.php

$db = require __DIR__ . '/test_db.php';
//$db2 = require __DIR__ . '/test_db_2.php';

    'components' => [
        'db' => $db,
        //'db2' => $db2,
    ],

Output

Scenario --
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=db_test'
  [Database] Transaction started
 I am logged in as 1  

Two databases:

test.php

$db = require __DIR__ . '/test_db.php';
$db2 = require __DIR__ . '/test_db_2.php';

    'components' => [
        'db' => $db,
        'db2' => $db2,
    ],

Output

Scenario --
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=db_2_test'
  [Database] Transaction started
 I am logged in as 1
 ERROR  
...
[yii\base\InvalidConfigException] The table does not exist: admin  

Config files: test_db.php

<?php

$db = require __DIR__ . '/db.php';

$db['dsn'] = 'mysql:host=localhost;dbname=db_test';

return $db;

db.php

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=db',
    'username' => 'user',
    'password' => 'password',
    'charset' => 'utf8',
];

test_db_2.php

<?php

$db = require __DIR__ . '/db_2.php';

$db['dsn'] = 'mysql:host=localhost;dbname=db_2_test';

return $db;

db_2.php

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=db_2',
    'username' => 'user',
    'password' => 'password',
    'charset' => 'utf8',
];

Provide test source code if related

    public function _before(FunctionalTester $I) {
        $I->amLoggedInAs(1);
    }

Details

class_name: FunctionalTester
modules:
    enabled:
        - Filesystem
        - Yii2
        - Db:
            dsn: 'mysql:host=localhost;dbname=db_test'
            user: 'root'
            password:
            cleanup: true
            populate: true
            populator: 'mysql -u $user $dbname < tests/_data/dump.sql'
Naktibalda commented 4 years ago

@DiscipleOfShinku You are using 2 years old version of Codeception and Yii2 module is the most actively developed module, so it changed a long in 2 years.

Please upgrade to Codeception 4.0 or at least to 3.1.2 and try again.

DiscipleOfShinku commented 4 years ago

@Naktibalda, I've removed codeception/base and installed Codeception 4.0 and necessary modules. However, now each time I run tests all tables in the test database are dropped.

ERROR 1146 (42S02) at line 11: Table 'db_test.admin' doesn't exist In DbPopulator.php line 108:

I have to do migrate/up after that.

DiscipleOfShinku commented 4 years ago

each time I run tests all tables in the test database are dropped.

It turns out that I misunderstood how cleanup option works.

Thanks!

DiscipleOfShinku commented 4 years ago

OK, I've finally found it. The reason was connected to how include works in PHP.

$db = require __DIR__ . '/test_db.php';
$db2 = require __DIR__ . '/test_db_2.php';

test_db_2.php

$db['dsn'] = 'mysql:host=localhost;dbname=db_2_test';