PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Why is the "default" datasource connection missing when running tests?

 March 09, 2022     cakephp, cakephp-3.0, phpunit, testing     No comments   

Issue

I'm writing tests with fixtures as described here.

My bootstrap for tests:

if (!getenv('db_dsn')) {
    putenv('db_dsn=sqlite:///:memory:');
}

ConnectionManager::config('test', ['url' => getenv('db_dsn')]);
ConnectionManager::config('test_custom_i18n_datasource', ['url' => getenv('db_dsn')]);

My code:

use Cake\TestSuite\TestCase;

class BackupExportTest extends TestCase
{
    public $fixtures = ['core.articles', 'core.comments'];

    public function setUp()
    {
        parent::setUp();
        $this->Articles = \Cake\ORM\TableRegistry::get('Articles');
    }

    public function testMyFunction()
    {
        $query = $this->Articles->find('all');
    }
}

Now, running the test, this is the exception:

PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 62 ms, Memory: 4.00MB

There was 1 error:

1) MysqlBackup\Test\TestCase\Utility\BackupExportTest::testMyFunction
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "default" was not found.

/home/mirko/Libs/Plugins/cakephp-mysql-backup/vendor/cakephp/cakephp/src/Datasource/ConnectionManager.php:196
/home/mirko/Libs/Plugins/cakephp-mysql-backup/vendor/cakephp/cakephp/src/ORM/Locator/TableLocator.php:175
/home/mirko/Libs/Plugins/cakephp-mysql-backup/vendor/cakephp/cakephp/src/ORM/TableRegistry.php:110
/home/mirko/Libs/Plugins/cakephp-mysql-backup/tests/TestCase/Utility/BackupExportTest.php:38

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

So, it seems \Cake\ORM\TableRegistry::get() searchs for default connection. Why? How to solve?


EDIT

phpunit.xml.dist is here:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    colors="true"
    processIsolation="false"
    stopOnFailure="false"
    syntaxCheck="false"
    bootstrap="./tests/bootstrap.php"
    >

    <testsuites>
        <testsuite name="cakephp-mysql-backup Test Cases">
            <directory>./tests/TestCase</directory>
        </testsuite>
    </testsuites>

    <!-- configure code coverage -->
    <filter>
        <whitelist>
            <directory suffix=".php">./src/</directory>
        </whitelist>
    </filter>
</phpunit>

The exception is thrown by running phpunit or phpunit tests/TestCase/Utility/BackupExportTest.php, via nix terminal. phpunit is on /usr/bin/phpunit and istalled via deb package.


Solution

Normally you'd better require PHPUnit as a dependency and run it via vendor/bin/phpunit, however the actual problem most likely is that you haven't configure the fixture listener.

Quote from the docs:

Before you can use fixtures you should double check that your phpunit.xml contains the fixture listener:

<!-- Setup a listener for fixtures -->
<listeners>
        <listener
        class="\Cake\TestSuite\Fixture\FixtureInjector"
        file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
                <arguments>
                        <object class="\Cake\TestSuite\Fixture\FixtureManager" />
                </arguments>
        </listener>
</listeners>

The fixture manager is responsible for creating proper connection aliases, ie mapping non-test connection requests to test* connections, like default to test.

See also

  • Cookbook > Testing > Test Connections
  • Cookbook > Testing > Creating Tests for Plugins


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing