未找到数据源配置“default”(测试CakePHP)

wb1gzix0  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(121)

测试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
dohp0rv5

dohp0rv51#

解决了,对不起,
在测试时,它似乎通过引用“phpunit.xml”而失败了,而我应该查看“phpunit.xml.dist”。
因此,我能够通过不将“phpunit.xml.dist”引用为“aphpunit.xml.dist”来消除错误输出,
我对“phpunit”还是个新手,所以我想在将来学习更多。
顺便说一句,如果你有“phpunit.xml”,你会看到“phpunit.xml”,如果你没有,你会看到“phpunit.xml.dist”!
谢谢你的提示ndm!

相关问题