【问题标题】:The datasource configuration "default" was not found(test CakePHP)未找到数据源配置“默认”(测试 CakePHP)
【发布时间】:2021-09-10 12:25:51
【问题描述】:

我在测试 CakePHP 表时遇到错误。

我自己似乎无法解决,所以我问了一个问题。 谢谢。

[错误内容]

Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "default" was not found.

[发生错误的位置]

/tests/TestCase/Model/Table/UsersTableTest.php

$this->users = $this->getTableLocator()->get('users', $config);

※我什至无法尝试不同的描述

use Cake\Datasource\ConnectionManager;

$connection = ConnectionManager::get('default');
$this->users = $connection->get('users', $config);
//It didn't work even if'default'was'test'

[做了什么]

以下方法在“/src/Controller/UsersController.php”位置正常工作,该位置不是测试位置。

$connection = ConnectionManager::get('default');

[我不明白的]

我不明白为什么在其他类中运行良好的东西在测试中不起作用

[发生了什么]

① “test_aaaa_db”创建了一个独立于生产使用的数据库。 ② 以上内容在“/config/app_local.php”中设置。 DEBUG 也是如此。

'debug' => filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN),


    'Datasources' => [
        'default' => [
            'host' => 'mysql',
            'username' => '***',
            'password' => '***',
            'database' => 'aaaa_db',
        ],
 
        'test' => [
            'host' => 'mysql',
            'username' => 'root',
            'password' => '***',
            'database' => 'test_aaaa_db',
        ],
    ],

③ 我提前将必要的表放入创建的数据库中。 我也有一些记录。

④夹具文件的创建如下。

/tests/Fixture/UsersFixture.php (除了下面的描述没有别的)

<?php
declare(strict_types=1);

namespace App\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
 * UsersFixture
 */
class UsersFixture extends TestFixture
{
    public $import = ['table' => 'users'];    
}

⑤我为表格创建了一个文件

<?php
declare(strict_types=1);

namespace App\Test\TestCase\Model\Table;

use App\Model\Table\usersTable;
// use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\ORM\TableRegistry;
/**
 * App\Model\Table\usersTable Test Case
 */
class usersTableTest extends TestCase
{
    /**
     * Test subject
     *
     * @var \App\Model\Table\usersTable
     */
    protected $users;

    /**
     * Fixtures
     *
     * @var array
     */
    protected $fixtures = [
        'app.Users'
    ];

    /**
     * setUp method
     *
     * @return void
     */
    public function setUp(): void
    {
        parent::setUp();
        $config = $this->getTableLocator()->exists('users') ? [] : ['className' => usersTable::class];
        $this->users = $this->getTableLocator()->get('users', $config);
    }
    
    public function testFindAuth(): void
    {
        $query = $this->users->find('auth')->select(['id']);
        $this->assertInstanceOf('Cake\ORM\Query', $query);
        $result = $query->enableHydration(false)->toArray();
        $expected = [
            ['id' => 1],
            ['id' => 2],
            ['id' => 3],
            ['id' => 15],
            ['id' => 19]
        ];

        $this->assertEquals($expected, $result);
    }
    
    /**
     * tearDown method
     *
     * @return void
     */
    public function tearDown(): void
    {
        unset($this->users);

        parent::tearDown();
    }
}

⑥还包括以下文件。

这里的设置有点不安。 顺便说一句,我通过在html中输出代码覆盖率已经确认没有问题。

/phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
        </whitelist>
    </filter>
  
</phpunit>

/phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    colors="true"
    processIsolation="false"
    stopOnFailure="false"
    bootstrap="tests/bootstrap.php"
    >
    <php>
        <ini name="memory_limit" value="-1"/>
        <ini name="apc.enable_cli" value="1"/>
    </php>

    <!-- Add any additional test suites you want to run here -->
    <testsuites>
        <testsuite name="app">
            <directory>tests/TestCase/</directory>
        </testsuite>
        <!-- Add plugin test suites here. -->
    </testsuites>

    <!-- Setup a listener for fixtures -->
    <listeners>
        <listener class="Cake\TestSuite\Fixture\FixtureInjector">
            <arguments>
                <object class="Cake\TestSuite\Fixture\FixtureManager"/>
            </arguments>
        </listener>
    </listeners>

    <!-- Ignore vendor tests in code coverage reports -->
    <filter>
        <whitelist>
            <directory suffix=".php">src/</directory>
            <directory suffix=".php">plugins/*/src/</directory>
            <exclude>
                <file>src/Console/Installer.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

[我没有做,因为我认为这不好]

  • 为下面的命令键入测试创建实体。 (其实我做了,但是错误内容并没有改变。)
bin/cake bake test Entity user

【问题讨论】:

  • 将您的自定义 PHPUnit 配置 (phpunit.xml) 与应用模板随附的配置 (phpunit.xml.dist) 进行比较,显然缺少与 CakePHP 相关的配置/参考。
  • 感谢您的评论!我想查看 phpunit.xml 等!

标签: php cakephp phpunit


【解决方案1】:

已解决,对不起,

在测试的时候,通过引用“phpunit.xml”似乎失败了,我应该看看“phpunit.xml.dist”。

所以我能够通过不将“phpunit.xml.dist”引用为“aphpunit.xml.dist”来消除错误输出,

我还是“phpunit”的新手,所以我想在未来学习更多。

顺便说一句,如果你有“phpunit.xml”,你会看到“phpunit.xml”,如果你没有,你会看到“phpunit.xml.dist”!

感谢您的提示,ndm!

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多