【发布时间】:2017-06-20 08:04:22
【问题描述】:
我目前正在学习如何在 PHPUnit 框架中测试数据库,并遇到了我不想在测试中连接到真实数据库的问题。这是因为当我在另一台计算机上运行测试时,这台计算机可能没有相同的数据库。
我实现了\PHPUnit\DbUnit\TestCaseTrait trait 并设置了以下方法:
/**
* Returns the test database connection.
*
* @return \PHPUnit\DbUnit\Database\Connection
*/
protected function getConnection()
{
$pdo = new PDO('sqlite::memory:');
return $this->createDefaultDBConnection($pdo, ':memory:');
}
/**
* Returns the test dataset.
*
* @return \PHPUnit\DbUnit\DataSet\IDataSet
*/
protected function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/test-dataset.xml');
}
数据集文件存在,并且可以正确找到。
在我的测试中的setUp 方法中,我将对象中的一个变量设置为\PDO 实例。
/**
* @var PDO $databaseServerConnection
*/
private $databaseServerConnection;
public function setUp()
{
$this->databaseServerConnection = $this->getConnection()->getConnection();
}
我希望我现在可以将该 PDO 连接与来自 getDataSet() 方法中的数据集文件的数据一起使用。
对于我自己的尝试,我尝试将它们与以下代码进行比较:
# Specify the tables we want to have in our connection dataset
$tables = ['users'];
# Create the dataset in the connection with the tables
$dataset = $this->getConnection()->createDataSet($tables);
# Query all results from the user table in the connection
$queryTable = $this->getConnection()->createQueryTable(
'users', 'SELECT * FROM users'
);
# Get the raw table data from the dataset file
$expectedTable = $this->getDataSet()->getTable('users');
# Check if theyre equal
$this->assertTablesEqual($queryTable, $expectedTable);
调试时,我注意到$dataset 中的$tables 数组变量只是一个空的。这里是$dataset 变量的var_dump。
class PHPUnit\DbUnit\Database\FilteredDataSet#18 (3) {
protected $tableNames =>
array(1) {
[0] =>
string(5) "users"
}
protected $tables =>
array(0) {
}
protected $databaseConnection =>
class PHPUnit\DbUnit\Database\DefaultConnection#16 (2) {
protected $connection =>
class PDO#15 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\Sqlite#17 (6) {
protected $columns =>
array(0) {
...
}
protected $keys =>
array(0) {
...
}
protected $truncateCommand =>
string(11) "DELETE FROM"
protected $pdo =>
class PDO#15 (0) {
...
}
protected $schema =>
string(8) ":memory:"
protected $schemaObjectQuoteChar =>
string(1) """
}
}
}
另外,$queryTable 变量中的 $data 数组是 null。这里是var_dump 的$queryTable 变量。
class PHPUnit\DbUnit\DataSet\QueryTable#22 (6) {
protected $query =>
string(19) "SELECT * FROM users"
protected $databaseConnection =>
class PHPUnit\DbUnit\Database\DefaultConnection#20 (2) {
protected $connection =>
class PDO#19 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\Sqlite#21 (6) {
protected $columns =>
array(0) {
...
}
protected $keys =>
array(0) {
...
}
protected $truncateCommand =>
string(11) "DELETE FROM"
protected $pdo =>
class PDO#19 (0) {
...
}
protected $schema =>
string(8) ":memory:"
protected $schemaObjectQuoteChar =>
string(1) """
}
}
protected $tableName =>
string(5) "users"
protected $tableMetaData =>
NULL
protected $data =>
NULL
private $other =>
NULL
}
此时$expectedTable 变量内的$data 数组充满了在数据集文件中创建的数据。
class PHPUnit\DbUnit\DataSet\DefaultTable#30 (3) {
protected $tableMetaData =>
class PHPUnit\DbUnit\DataSet\DefaultTableMetadata#34 (3) {
protected $columns =>
array(3) {
[0] =>
string(2) "id"
[1] =>
string(4) "name"
[2] =>
string(5) "email"
}
protected $primaryKeys =>
array(0) {
}
protected $tableName =>
string(5) "users"
}
protected $data =>
array(4) {
[0] =>
array(3) {
'id' =>
string(1) "1"
'name' =>
string(3) "test1"
'email' =>
string(9) "test1@me.nl"
}
[1] =>
array(3) {
'id' =>
string(1) "2"
'name' =>
string(3) "test2"
'email' =>
string(9) "test2@me.nl"
}
[2] =>
array(3) {
'id' =>
string(1) "3"
'name' =>
string(6) "test3"
'email' =>
string(12) "test3@me.nl"
}
[3] =>
array(3) {
'id' =>
string(1) "4"
'name' =>
string(4) "test4"
'email' =>
string(10) "test4@me.nl"
}
}
private $other =>
NULL
}
我还尝试在 getConnection() 方法中对 pdo 连接对象执行 2 个查询,以创建包含其中值的表:
protected function getConnection()
{
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (id PRIMARY KEY, name VARCHAR(50), email VARCHAR(50))");
$pdo->exec("INSERT INTO users (id, name, email) VALUES (20, 'Bas', 'aa@me')");
return $this->createDefaultDBConnection($pdo, ':memory:');
}
我的连接中没有任何可用数据是怎么来的?如何将数据集文件中的数据导入此处以使测试通过?
另外,这样做是一个好习惯吗?
【问题讨论】: