简体   繁体   English

Codeception测试采用“默认”数据源,而不是CakePHP 3中的“测试”

[英]Codeception Tests take 'default' datasource instead of 'test' in CakePHP 3

I'm able to run UnitTests via Codeception, but when it comes to database and fixtures its confusing... 我可以通过Codeception运行UnitTests,但是当涉及到数据库和固定装置时,它令人困惑...

My codeception.dist.yml 我的codeception.dist.yml

namespace: App\TestSuite\Codeception
paths:
    tests: tests
    output: tmp/tests
    data: tests/Fixture
    support: src/TestSuite/Codeception
    envs: tests/Envs
settings:
    bootstrap: bootstrap.php
    colors: true
    memory_limit: 1024M
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    config:
        Db:
            dsn: 'mysql:host=%DB_HOST_TEST%;dbname=%DB_NAME_TEST%'
            user: '%DB_USER_TEST%'
            password: '%DB_PASSWORD_TEST%'
            dump: tests/_data/dump.sql
            cleanup: true # reload dump between tests
            populate: true # load dump before all tests
            reconnect: true
    enabled:
        - Db
params:
    - env

The dump.sql is imported into the test database on running my tests. 在运行测试时,dump.sql会导入到测试数据库中。 Fixtures are not inserted... 没有插入灯具...

Then in my Test I do something like this (shortened): 然后在我的测试中,我做这样的事情(缩短):

<?php
namespace App\Test\Unit\Model\Behavior;

use App\Model\Behavior\HashableBehavior;
use App\Model\Entity\User;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
 * App\Model\Behavior\HashableBehavior Test Case
 */
class HashableBehaviorTest extends \Codeception\Test\Unit
{
/**
 * Test subject
 *
 * @var \App\Model\Behavior\HashableBehavior
 */
public $Hashable;

public $fixtures = [
    'app.Users',
    'app.mandators'
];

/**
 * Test subject
 *
 * @var \App\Model\Table\UsersTable
 */
public $UsersTable;

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

    $this->Hashable = new HashableBehavior($this->UsersTable);
}

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

    parent::tearDown();
}

/**
 * Test beforeSave method
 *
 * @return void
 * @throws \Exception
 */
public function testBeforeSave()
{
    $user = $this->UsersTable->get(1);
    ...

}

This gives me the first record from my default database and not from the test database. 这给了我默认数据库而不是测试数据库的第一条记录。 I have no Idea what is missing here... Glad for any help! 我不知道这里缺少什么...很高兴能提供帮助!

After checking all the code again I found in tests/bootstrap.php this lines: 再次检查所有代码后,我在tests / bootstrap.php中找到以下行:

if (getenv('db_dsn')) {
    putenv('DATABASE_URL=' . getenv('db_dsn'));
    putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}

Even I could not find the place where the variable DATABASE_TEST_URL is used, adding this to my .env file solved it: 即使我找不到使用变量DATABASE_TEST_URL的位置,也可以将其添加到我的.env文件中来解决它:

export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true&quoteIdentifiers=false&persistent=false"

Variable DATABASE_TEST_URL is in .env but has no effect on this... I think its that cakePHP and Codeception doesn't magically integrate too well. 变量DATABASE_TEST_URL在.env中,但对此没有影响。。。我认为CakePHP和Codeception集成得不是很好。

Still my fixtures are not loaded in UnitTests (class MyTest extends \\Codeception\\Test\\Unit) - in but the question is solved. 仍然没有将我的装置加载到UnitTests中(类MyTest扩展了\\ Codeception \\ Test \\ Unit)-但是问题已解决。

[UPDATE] Fixture loading fixed. [更新]夹具加载已修复。 I have to manually inject the FixtureManager and load my fxtures in setUp function. 我必须手动注入FixtureManager并在setUp函数中加载我的设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM