gpt4 book ai didi

PHPUnit 数据库测试和 Travis CI : Can't truncate table

转载 作者:行者123 更新时间:2023-11-29 22:11:04 25 4
gpt4 key购买 nike

我正在编写 PHPUnit 测试来测试 MySQL 数据库填充。我的测试在本地运行良好,但在 Travis CI 上失败并出现以下错误:

PHPUnit_Extensions_Database_Operation_Exception: COMPOSITE[TRUNCATE] operation failed on query: 

TRUNCATE `simple_table`

using args: Array

[SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.simple_table' doesn't exist]

我有一个 DatabaseTestCase 父类和一个 FillCommandTest 测试类。

数据库测试用例

class DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
{
private static $pdo = null;

private $connection = null;

/**
* Construct.
*/
public function __construct()
{
$env = new Environment();

$this->dsn = "mysql:dbname=". $env->get('DB_DATABASE', 'test') . ";host=" . $env->get('DB_HOST', '127.0.0.1');
$this->username = $env->get('DB_USERNAME', 'travis');
$this->password = $env->get('DB_PASSWORD', '');
$this->db_name = $env->get('DB_DATABASE', 'test');
}

/**
* Get database connection.
*
* @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
*/
final public function getConnection()
{
if ($this->connection === null) {
if (self::$pdo == null) {
self::$pdo = new PDO($this->dsn, $this->username, $this->password);
}

$this->connection = $this->createDefaultDBConnection(self::$pdo, $this->db_name);
}

return $this->connection;
}

/**
* Get XML dataset.
*
* @param string $file
* @return string
*/
public function getDataSet($file = 'empty.xml')
{
$file = 'tests/datasets/' . $file;

return $this->createXmlDataSet($file);
}

/**
* Set up the test database table
*
* @param PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection $connection
*/
protected function setUpTable($connection)
{
$pdo = $connection->getConnection();

$sql = "CREATE TABLE IF NOT EXISTS simple_table (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
address VARCHAR(100)
)";

$pdo->exec($sql);
}
}

填充命令测试

class FillCommandTest extends DatabaseTestCase
{
protected $app;

protected $command;

protected $command_tester;

/**
*
*/
public function setUp()
{
$this->app = new Application();
$this->app->add(new FillCommand());
$this->command = $this->app->find('fill');
$this->command_tester = new CommandTester($this->command);

parent::setUp();

$connection = $this->getConnection();
$this->setUpTable($connection);
}

/**
*
*/
public function test_parse_and_fill_simple_table()
{
copy(getcwd() . '/tests/files/FillSimpleTable.php', getcwd() . '/src/Fillers/FillSimpleTable.php');

copy(getcwd() . '/tests/files/simple.txt', getcwd() . '/src/Files/simple.txt');

$this->command_tester->execute(array(
'command' => $this->command->getName(),
'name' => 'simple_table'
));

$actual = $this->getConnection()->createQueryTable('simple_table', 'SELECT * FROM simple_table');

$expected = $this->getDataSet('simple_table.xml')->getTable('simple_table');

$this->assertTablesEqual($expected, $actual);

unlink(getcwd() . '/src/Fillers/FillSimpleTable.php');

unlink(getcwd() . '/src/Files/simple.txt');
}
}

我做错了什么?

最佳答案

从外观上看,您的代码似乎引用了一个名为“test”的数据库。

但我假设由于您正在部署,您的数据库名称应该不同。确保您的 .env DB_DATABASE 变量反射(reflect)了生产服务器上的正确变量。

关于PHPUnit 数据库测试和 Travis CI : Can't truncate table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com