lundi 7 novembre 2016

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

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.

Aucun commentaire:

Enregistrer un commentaire