我正在学习SilverStripe,同时尝试将单元测试也集成到我的简单项目中。我正在尝试使用$ fixture_file播种数据。但这不起作用。
这是我的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>
这是我的PageTest.php
use SilverStripe\Dev\SapphireTest;
class PageTest extends SapphireTest
{
static $fixture_file = 'fixtures.yml';
public function testMyMethod()
{
$this->assertEquals(2, Page::MyMethod());
}
}
这是我的Fixtures.yml
ContactFormSubmission:
wai:
Name: Wai
Email: iljimae.ic@gmail.com
Message: ThisIsAMessage
这是我的ContactFormSubmission.php
<?php
namespace {
use SilverStripe\ORM\DataObject;
class ContactFormSubmission extends DataObject
{
private static $db = [
'Name' => 'Varchar',
'Email' => 'Varchar',
'Message' => 'Text',
];
}
}
运行测试时,出现此错误。
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
我发现它无法连接到数据库。我试图像这样覆盖phpunit.xml中的数据库配置。
<env name="SS_DATABASE_NAME" value="test"/>
但是它仍在尝试连接到临时数据库。如何在Silverstripe中定义用于单元测试的数据库?