doujiayao8433 2019-05-19 13:58
浏览 28

Silverstripe单元测试夹具文件无法正常工作

I am learning SilverStripe and meanwhile trying to integrate unit testing into my simple project as well. I am trying to use $fixture_file to seed the data. But it is not working.

This is my phpunit.xml

<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
    <testsuite name="Default">
        <directory>app/tests</directory>
    </testsuite>
    <groups>
        <exclude>
            <group>sanitychecks</group>
        </exclude>
    </groups>
</phpunit>

This is my PageTest.php

use SilverStripe\Dev\SapphireTest;

class PageTest extends SapphireTest
{
    static $fixture_file = 'fixtures.yml';

    public function testMyMethod()
    {
        $this->assertEquals(2, Page::MyMethod());
    }
}

This is my fixtures.yml

ContactFormSubmission:
    wai:
        Name: Wai
        Email: iljimae.ic@gmail.com
        Message: ThisIsAMessage

This is my ContactFormSubmission.php

<?php

namespace {

    use SilverStripe\ORM\DataObject;

    class ContactFormSubmission extends DataObject
    {
        private static $db = [
            'Name' => 'Varchar',
            'Email' => 'Varchar',
            'Message' => 'Text',
        ];
    }
}

When I run the test, I got this error.

Time: 1.06 seconds, Memory: 16.00MB

There was 1 error:

1) PageTest::testMyMethod
SilverStripe\ORM\Connect\DatabaseException: Couldn't run query:

CREATE DATABASE "ss_tmpdb_1558273911_5783864" DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci

42000-1044: Access denied for user 'wai'@'%' to database 'ss_tmpdb_1558273911_5783864'

/var/www/vendor/silverstripe/framework/src/ORM/Connect/DBConnector.php:64
/var/www/vendor/silverstripe/framework/src/ORM/Connect/PDOConnector.php:398
/var/www/vendor/silverstripe/framework/src/ORM/Connect/PDOConnector.php:287
/var/www/vendor/silverstripe/framework/src/ORM/Connect/Database.php:143
/var/www/vendor/silverstripe/framework/src/ORM/Connect/Database.php:228
/var/www/vendor/silverstripe/framework/src/ORM/Connect/Database.php:144
/var/www/vendor/silverstripe/framework/src/ORM/Connect/MySQLDatabase.php:381
/var/www/vendor/silverstripe/framework/src/ORM/Connect/DBSchemaManager.php:120
/var/www/vendor/silverstripe/framework/src/ORM/Connect/MySQLSchemaManager.php:231
/var/www/vendor/silverstripe/framework/src/ORM/Connect/Database.php:830
/var/www/vendor/silverstripe/framework/src/ORM/Connect/TempDatabase.php:190
/var/www/vendor/silverstripe/framework/src/Dev/State/FixtureTestState.php:44
/var/www/vendor/silverstripe/framework/src/Dev/State/SapphireTestState.php:68
/var/www/vendor/silverstripe/framework/src/Dev/SapphireTest.php:274
/var/www/vendor/phpunit/phpunit/phpunit:52

I found out that it cannot connect to the database. I try to override the database configuration in phpunit.xml like this.

<env name="SS_DATABASE_NAME" value="test"/>

But it is still trying to connect to the temporary database. How can I define the database for unit test in Silverstripe?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安装svn网络有问题怎么办
    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 latex怎么处理论文引理引用参考文献