yii2-app-advanced: [yii\base\InvalidConfigException] Failed to instantiate component or class "db".

Hi I am unable to connect MongoDB in Unit Testing to work with fixture data. For Unit Testing with codeception, same mongodb connection is working. Please help me to solve this issue.

What steps will reproduce the problem?

To reproduce implement Fixtures feature in Yii2 advanced template with Codeception Unit Testing.

Code file attached as follows. fixture.log UserFixture.php.txt LoginFormTest.php.txt

What is the expected result?

Fixture to work with MongoDb connection in Unit Testing and yii console command.

What do you get instead?

root@1a3d00a6fe60:/var/www/rest.testsite.com# ./vendor/bin/codecept run unit -c common
Codeception PHP Testing Framework v2.3.7
Powered by PHPUnit 6.4.4 by Sebastian Bergmann and contributors.

Common\tests.unit Tests (1) ---------------------------------------------------------------------------------------------------------------------------------------------------------
E LoginFormTest: Fixture user (0.11s)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 1.42 seconds, Memory: 12.00MB

There was 1 error:

---------
1) LoginFormTest: Fixture user
 Test  tests/unit/models/LoginFormTest.php:testFixtureUser

  [yii\base\InvalidConfigException] Failed to instantiate component or class "db".

#1  /var/www/rest.testsite.com/vendor/yiisoft/yii2/di/Instance.php:139
#2  /var/www/rest.testsite.com/vendor/yiisoft/yii2/test/DbFixture.php:42
#3  /var/www/rest.testsite.com/vendor/yiisoft/yii2/base/BaseObject.php:108
#4  yii\base\BaseObject->__construct
#5  /var/www/rest.testsite.com/vendor/yiisoft/yii2/di/Container.php:375
#6  /var/www/rest.testsite.com/vendor/yiisoft/yii2/di/Container.php:156
#7  /var/www/rest.testsite.com/vendor/yiisoft/yii2/BaseYii.php:349
#8  /var/www/rest.testsite.com/vendor/yiisoft/yii2/test/FixtureTrait.php:208
#9  /var/www/rest.testsite.com/vendor/yiisoft/yii2/test/FixtureTrait.php:145
#10 /var/www/rest.testsite.com/vendor/yiisoft/yii2/test/FixtureTrait.php:112

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

Additional info

Q A
Yii version 2.0.13.1
PHP version 7.1
Operating system Ubuntu 16.04.2 LTS

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (4 by maintainers)

Most upvoted comments

I finally found the culprit:

Commenting the resetApplication() call in the _after() function fixes the problem.

I’m not sure if this issue needs to be kept here, since this looks like an issue with Codeception and the Yii2 module/connector.

Is there any clue to what may be happening? If no one is working on this issue I will try to spend some time to fix it.

I reverted back to “codeception/codeception 2.4.0” and the error when away. Seem like an err with codeception.

The change proposed by @VasylDmytruk worked for me. Many thanks 👍

For me helped to add to file _protected/tests/codeception/common/unit.suite.yml config:

modules:
  enabled:
    - Yii2:
        configFile: '../config/common/unit.php'
        part: init

So now file looks:

# Codeception Test Suite Configuration

# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: UnitTester
modules:
  enabled:
    - Yii2:
        configFile: '../config/common/unit.php'
        part: init

After that I ran build command.

@machour I wanted to start doing that before I stopped working today, but I figured it would be better if I start fresh next week…

@alfredleo I kind of have a solution, but for some reason it’s kind of hard to understand why it doesn’t work in the first place.

I hope to send a PR this week, since I kind of need this fixed for a related project.

@XzAeRo please go ahead.