【问题标题】:datasource configuration "default" was not found in CakePHP 3在 CakePHP 3 中找不到数据源配置“默认”
【发布时间】:2015-11-26 15:31:26
【问题描述】:

我正在使用 CakePHP e PHPUnit 执行测试,但是当我执行 TestCase 时,每个测试方法都会提示此错误:

1) App\Test\TestCase\Model\Table\UsersTableTest::testInitialize Cake\Datasource\Exception\MissingDatasourceConfigException: 未找到数据源配置“默认”。

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'nonstandard_port_number',
        'username' => 'dbusername',
        'password' => 'dbpass',
        'database' => 'dbname',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,

        /**
         * Set identifier quoting to true if you are using reserved words or
         * special characters in your table or column names. Enabling this
         * setting will result in queries built using the Query Builder having
         * identifiers quoted when creating SQL. It should be noted that this
         * decreases performance because each query needs to be traversed and
         * manipulated before being executed.
         */
        'quoteIdentifiers' => false,

        /**
         * During development, if using MySQL < 5.6, uncommenting the
         * following line could boost the speed at which schema metadata is
         * fetched from the database. It can also be set directly with the
         * mysql configuration directive 'innodb_stats_on_metadata = 0'
         * which is the recommended value in production environments
         */
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
    ],
    /**
     * The test connection is used during the test suite.
     */
    'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        //'port' => 'nonstandard_port_number',
        'username' => 'dbusername',
        'password' => 'dbpass',
        'database' => 'dbnametest',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
    ],
],

测试套件:

<?php
namespace App\Test\TestCase\Model\Table;

use App\Model\Table\UsersTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
 * App\Model\Table\UsersTable Test Case
 */
class UsersTableTest extends TestCase
{

    /**
     * Fixtures
     *
     * @var array
     */
    public $fixtures = [
        'app.users',
        'app.user_types',
        'app.bookings',
        'app.stores'
    ];

    /**
     * setUp method
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        $config = TableRegistry::exists('Users') ? [] : ['className' => 'App\Model\Table\UsersTable'];
        $this->Users = TableRegistry::get('Users', $config);
    }

    /**
     * tearDown method
     *
     * @return void
     */
    public function tearDown()
    {
        unset($this->Users);

        parent::tearDown();
    }

    /**
     * Test initialize method
     *
     * @return void
     */
    public function testInitialize()
    {
        $this->markTestIncomplete('Not implemented yet.');
    }

    /**
     * Test validationDefault method
     *
     * @return void
     */
    // Método que deverá testar o método "validationDefault()" de "UsersTable",
    // mas como e quando este método será chamado?
    // A ferramenta seleciona o método a ser executado
    public function testValidationDefault()
    {
        $this->markTestIncomplete('Not implemented yet.');
    }

    /**
     * Test buildRules method
     *
     * @return void
     */
    public function testBuildRules()
    {
        $this->markTestIncomplete('Not implemented yet.');
    }

    public function testFindUserById(){
        $query = $this->Users->find('userById', [
            'conditions' => ['Users.id' => 900000],
            'fields' => ['Users.id', 'Users.email', 'Users.password',
                'Users.username', 'Users.user_type_id', 'Users.created',
                'Users.modified']
        ]);
        $this->assertInstanceOf('Cake\ORM\Query', $query);
        $result = $query->hydrate(false)->toArray();

        $expected = [
            [
                'id' => 900000,
                'email' => 'usuariocomum1@gmail.com',
                'password' => 'usuariocomum1senha',
                'username' => 'usuariocomum1username',
                'user_type_id' => 900000,
                'created' => '2015-07-17 18:46:47',
                'modified' => '2015-07-17 18:46:47'
            ]
        ];

        $this->assertEquals($expected, $result);
    }
}

但是数据源“默认”和“测试”没问题,网站工作正常。

完整的命令行:

c:\xampp\htdocs\PROJETOS\Shopping\vendor\bin>phpunit "C:/xampp/htdocs/PROJETOS/S
hopping/tests/TestCase/Model/Table/UsersTableTest.php"
PHPUnit 4.8.6 by Sebastian Bergmann and contributors.

EEEE

Time: 3.85 seconds, Memory: 3.75Mb

There were 4 errors:

1) App\Test\TestCase\Model\Table\UsersTableTest::testInitialize
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource confi
guration "default" was not found.

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\ORM\TableRegistry.p
hp:192
C:\xampp\htdocs\PROJETOS\Shopping\tests\TestCase\Model\Table\UsersTableTest.php:
35

2) App\Test\TestCase\Model\Table\UsersTableTest::testValidationDefault
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource confi
guration "default" was not found.

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\ORM\TableRegistry.p
hp:192
C:\xampp\htdocs\PROJETOS\Shopping\tests\TestCase\Model\Table\UsersTableTest.php:
35

3) App\Test\TestCase\Model\Table\UsersTableTest::testBuildRules
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource confi
guration "default" was not found.

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\ORM\TableRegistry.p
hp:192
C:\xampp\htdocs\PROJETOS\Shopping\tests\TestCase\Model\Table\UsersTableTest.php:
35

4) App\Test\TestCase\Model\Table\UsersTableTest::testFindUserById
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource confi
guration "default" was not found.

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\ORM\TableRegistry.p
hp:192
C:\xampp\htdocs\PROJETOS\Shopping\tests\TestCase\Model\Table\UsersTableTest.php:
35

FAILURES!
Tests: 4, Assertions: 0, Errors: 4.

我现在进行测试(01/09/2015(9 月 1 日)- 09:56)并且在 [projectName]/logs/cli-error 和 [projectName]/logs/error 中没有出现错误。

注意:CakePHP 版本 3.0.11

【问题讨论】:

  • 除了测试应该使用test 连接(其中default 将是test 的别名),检查你的CLI 错误日志(logs/cli-error.log),错误总是有一个堆栈跟踪应该包含在问题中。另请提及您的确切 CakePHP 版本 (vendor/cakephp/cakephp/VERSION.txt)。
  • @ndm 我在我的问题中添加信息
  • 不要仅仅依赖错误日志中的日期,清空它们,运行测试套件,然后再次检查它们。还要确保文件是可写的。
  • @ndm 我添加了我的 TestFile,我再次运行测试,但两个文件中都没有写入任何内容(我检查并且是可写的)。
  • 听起来很可疑......运行测试的命令是什么样的?

标签: cakephp cakephp-3.0


【解决方案1】:

您没有正确运行测试套件,您在那里所做的事情将导致 PHPUnit 无法获取应用程序根目录中的测试套件配置 (phpunit.xml.dist)

根据文档,您应该从应用程序根目录运行该套件

[...]

通过使用phpunit,您可以运行您的应用程序测试。要运行应用程序的测试,您只需运行:

// composer installs
$ vendor/bin/phpunit

// phar file
php phpunit.phar

来自应用程序的根目录。

[...]

* 强调我的

Cookbook > Testing > Running Tests

所以你应该 CD 到 Shopping 目录,并使用类似的相对路径运行 PHPUnit

cd c:\xampp\htdocs\PROJETOS\Shopping
vendor\bin\phpunit tests\TestCase\Model\Table\UsersTableTest.php

【讨论】:

  • Windows 用户确保您从 windows powershell 运行
  • @i_am_leo 这绝不是必需的。
【解决方案2】:

首先建立一个测试数据库,然后将其添加到您的 app.php 文件中:

 'test' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'YOUR TEST HOST',
            'username' => 'USERNAME',
            'password' => 'PASSWORD',
            'database' => 'YOUR TEST DB',
            'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'quoteIdentifiers' => false,
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        ],

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多