数据源配置“默认”在CakePHP 3中找不到 [英] datasource configuration "default" was not found in CakePHP 3

查看:644
本文介绍了数据源配置“默认”在CakePHP 3中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP e PHPUnit来执行测试,但是当我执行TestCase时,会出现每个测试方法的错误:


1)App\Test\TestCase\Model\Table\UsersTableTest :: testInitialize
Cake \Datasource\Exception\MissingDatasourceConfigException:找不到数据源配置default。




 'Datasources'=> [
'default'=> [
'className'=> 'Cake \Database \Connection',
'driver'=> 'Cake\Database\Driver\Mysql',
'persistent'=> false,
'host'=> 'localhost',
/ **
* CakePHP将使用基于驱动程序选择的默认DB端口
* MAMP上的MySQL使用端口8889,MAMP用户将要取消注释
*以下行并相应地设置端口
* /
//'port'=> 'nonstandard_port_number',
'username'=> 'dbusername',
'password'=> 'dbpass',
'database'=> 'dbname',
'encoding'=> 'utf8',
'timezone'=> 'UTC',
'cacheMetadata'=> true,

/ **
*如果您在表或列名称中使用保留字或
*特殊字符,请将标识符引用设置为true。启用此
*设置将导致使用查询生成器创建查询,在创建SQL时引用
*标识符。应该注意,这个
*降低了性能,因为每个查询需要被遍历并且
*在被执行之前被操作。
* /
'quoteIdentifiers'=> false,

/ **
*在开发期间,如果使用MySQL< 5.6,取消注释
*下面的行可以提高从数据库获取模式元数据
*的速度。它也可以直接使用
* mysql配置指令'innodb_stats_on_metadata = 0'
*,这是生产环境中的推荐值
* /
//'init'= > ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
/ **
*测试连接在测试套件期间使用。
* /
'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'],
],
],

TestSuite:

 <?php 
命名空间App\Test\TestCase\Model\Table ;

使用App\Model\Table\UsersTable;
使用Cake\ORM\TableRegistry;
使用Cake \TestSuite\TestCase;

/ **
* App\Model\Table\UsersTable测试用例
* /
类UsersTableTest扩展TestCase
{

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

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

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

parent :: tearDown();
}

/ **
*测试初始化​​方法
*
* @return void
* /
public function testInitialize ()
{
$ this-> markTestIncomplete('还没有实现。
}

/ **
*测试验证默认方法
*
* @return void
* /
//Método que devera testar ométodovalidationDefault()deUsersTable,
// mas como e quando estemétodoseráchamado?
// A ferramenta seleciona ométodoa ser executado
public function testValidationDefault()
{
$ this-> markTestIncomplete('还没有实现。
}

/ **
*测试buildRules方法
*
* @return void
* /
public function testBuildRules ()
{
$ this-> markTestIncomplete('还没有实现。
}

public function testFindUserById(){
$ query = $ this-> Users-> find('userById',[
'conditions' > ['Users.id'=> 900000],
'fields'=> ['Users.id','Users.email','Users.password',
' 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);
}
}

但是数据源default和test

 完整命令行: hopping / tests / TestCase / Model / Table / UsersTableTest.php 
PHPUnit 4.8.6由Sebastian Bergmann和贡献者。

EEEE

时间:3.85秒,内存:3.75Mb

有4个错误:

1)应用程序\Test\TestCase\Model\Table\UsersTableTest :: testInitialize
Cake \Datasource\Exception\MissingDatasourceConfigException:未找到数据源confi
gurationdefault。

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\\\\\\\\hoho\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:未找到数据源confi
gurationdefault。

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\\\\\\\\hoho\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:未找到数据源confi
gurationdefault。

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\\\\\\\\hoho\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:未找到数据源confi
gurationdefault。

C:\xampp\htdocs\PROJETOS\Shopping\vendor\cakephp\cakephp\src\Datasource\Connecti
onManager.php:188
C:\xampp\htdocs\PROJETOS\\\\\hoho\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \xampp\htdocs\PROJETOS\Shopping\tests\TestCase\Model\Table\UsersTableTest.php:
35

失败!
测试:4,断言:0,错误:4.

/ 09/2015(1月9日) - 09:56),并且在此日期的[projectName] / logs / cli-error和[projectName] / logs / error中没有错误。



注意:CakePHP 3.0.11版

解决方案

测试套件正确,您正在做什么会导致PHPUnit不选择存在于应用程序根目录( phpunit.xml.dist )中的测试套件配置



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


..]



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

  // composer installs 
$ vendor / bin / phpunit

// phar文件
php phpunit.phar

从您应用程式的根目录。



[...]


* 强调我



Cookbook>测试>运行测试



因此,您应该将CD插入 Shopping directoy中,并使用相对路径运行PHPUnit

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


I'm using CakePHP e PHPUnit to execute tests but when I execute a TestCase this error is prompted for every test method:

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

'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'],
    ],
],

The TestSuite:

<?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);
    }
}

But the datasource "default" and "test" it's OK, the site works fine.

Full command line:

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.

I test now (01/09/2015 (1st september) - 09:56) and doesn't error in [projectName]/logs/cli-error and [projectName]/logs/error with this date.

NOTE: CakePHP version 3.0.11

解决方案

You're not running the test suite correctly, what you are doing there will cause PHPUnit to not pick up the test suite configuration that lives in your applications root (phpunit.xml.dist)

As per the docs, you should run the suite from your applications root directory

[...]

By using phpunit you can run your application tests. To run your application’s tests you can simply run:

// composer installs
$ vendor/bin/phpunit

// phar file
php phpunit.phar

From your application’s root directory.

[...]

* emphasis mine

Cookbook > Testing > Running Tests

So you should CD into the Shopping directoy, and run PHPUnit using a relative path like

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

这篇关于数据源配置“默认”在CakePHP 3中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆