1 要使用phpunit 必须先安装Dbunit,执行pear 命令 如下执行安装,安装完后Extension目录将多出Database 目录
sudo pear install phpunit/DbUnit
2 数据库测试主要验证数据库得数据与提供得数据集 是否一致得测试工具类,一般用在业务类,数据层测试等等
2 getConnection 实例化数据库连接对象,getDataSet 初始化数据库结果集,当调用测试时先把dataset.xml 得数据集行导入到数据库中
测试代码如下
<?php
//数据库测试文件DatabaseTest.php
class DatabaseTest extends PHPUnit_Extensions_Database_TestCase
{
protected function getConnection()
{
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', '123456');
return $this->createDefaultDBConnection($pdo, 'cms');
}
protected function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__).'/dataset.xml');
}
public function testDataSet()
{
$this->assertEquals('somesthing','somesthing');
}
}
?>
//数据集文件名dataset.xml
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<xingming id="1" xingming="Hello buddy!" />
<xingming id="2" xingming="I like it!" />
</dataset>
也可以把数据库连接保存在phpunit.xml 文件中
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit>
<php>
<var name="DB_DSN" value="mysql:dbname=deercms;host=localhost" />
<var name="DB_USER" value="root" />
<var name="DB_PASSWD" value="123456" />
<var name="DB_DBNAME" value="deercms" />
</php>
</phpunit>
<?php
//数据库测试文件DatabaseTest.php
class DatabaseTest extends PHPUnit_Extensions_Database_TestCase
{
protected function getConnection()
{
$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
return $this->createDefaultDBConnection($pdo, $GLOBALS['DB_DBNAME']);
}
protected function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__).'/dataset.xml');
}
public function testDataSet()
{
$this->assertEquals('somesthing','somesthing');
}
}
?>
getDataSet 支持多种数据集格式有xml,yml,cvs,数组,组合数据集(Composite DataSet),query(sql) 查询数据集,Database 数据库数据集
xml
protected function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__).'/dataset.xml');
}
yml
protected function getDataSet()
{
return new PHPUnit_Extensions_Database_DataSet_YamlDataSet(
dirname(__FILE__)."/dataset.yml"
);
}
cvs
protected function getDataSet()
{
$dataSet = new PHPUnit_Extensions_Database_DataSet_CsvDataSet();
$dataSet->addTable('xingming', dirname(__FILE__)."/dataset.csv");
return $dataSet;
}
数组
protected function getDataSet()
{
return new MyApp_DbUnit_ArrayDataSet(array(
'xingming' => array(
array('id' => 1, 'xingming' => 'Hello buddy!'),
array('id' => 2, 'xingming' => 'I like it!'),
),
));
}
MyApp_DbUnit_ArrayDataSet 数组数据集类定义
class MyApp_DbUnit_ArrayDataSet extends PHPUnit_Extensions_Database_DataSet_AbstractDataSet
{
/**
* @var array
*/
protected $tables = array();
/**
* @param array $data
*/
public function __construct(array $data)
{
foreach ($data AS $tableName => $rows) {
$columns = array();
if (isset($rows[0])) {
$columns = array_keys($rows[0]);
}
$metaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData($tableName, $columns);
$table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($metaData);
foreach ($rows AS $row) {
$table->addRow($row);
}
$this->tables[$tableName] = $table;
}
}
protected function createIterator($reverse = FALSE)
{
return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($this->tables, $reverse);
}
public function getTable($tableName)
{
if (!isset($this->tables[$tableName])) {
throw new InvalidArgumentException("$tableName is not a table in the current database.");
}
return $this->tables[$tableName];
}
}
query(sql) 查询数据集
protected function getDataSet()
{
// $this->getConnection() 可以换其他数据库连接比如$this->getConnection2(),$this->getConnection3() 等等
$ds = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
//把查询到得数据集 插入到xingming 表中
$ds->addTable('xingming', 'SELECT id, xingming FROM username');
return $ds;
}
Replacement DataSet(替换数据集内容)
protected function getDataSet()
{
$ds = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
$ds->addTable('xingming', 'SELECT id, xingming FROM username');
$rds = new PHPUnit_Extensions_Database_DataSet_ReplacementDataSet($ds);
$rds->addFullReplacement('xxxxhhhhh', "xiexie");
return $rds;
}
组合数据集(Composite DataSet)
public function getDataSet()
{
$ds1 = $this->createFlatXmlDataSet('fixture1.xml');
$ds2 = $this->createFlatXmlDataSet('fixture2.xml');
$compositeDs = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
$compositeDs->addDataSet($ds1);
$compositeDs->addDataSet($ds2);
return $compositeDs;
}
数据库断言Database Assertions API
public function testAddEntry()
{
//$this->getConnection()->getRowCount('guestbook') 统计表guestbook 总函数
$this->assertEquals(2, $this->getConnection()->getRowCount('guestbook'), "Pre-Condition");
$guestbook = new Guestbook();
$guestbook->addEntry("suzy", "Hello world!");
$this->assertEquals(3, $this->getConnection()->getRowCount('guestbook'), "Inserting failed");
}
//数据集表断言
public function testAddEntry()
{
$guestbook = new Guestbook();
$guestbook->addEntry("suzy", "Hello world!");
$queryTable = $this->getConnection()->createQueryTable(
'guestbook', 'SELECT * FROM guestbook'
);
$expectedTable = $this->createFlatXmlDataSet("expectedBook.xml")
->getTable("guestbook");
//验证查询(query)结果集$expectedTable,$queryTable是否相等
$this->assertTablesEqual($expectedTable, $queryTable);
}
//数据集断言
public function testCreateDataSetAssertion()
{
$dataSet = $this->getConnection()->createDataSet(array('guestbook'));
$expectedDataSet = $this->createFlatXmlDataSet('guestbook.xml');
$this->assertDataSetsEqual($expectedDataSet, $dataSet);
}
public function testManualDataSetAssertion()
{
$dataSet = new PHPUnit_Extensions_Database_DataSet_QueryDataSet();
$dataSet->addTable('guestbook', 'SELECT id, content, user FROM guestbook'); // additional tables
$expectedDataSet = $this->createFlatXmlDataSet('guestbook.xml');
$this->assertDataSetsEqual($expectedDataSet, $dataSet);
}
MySQL XML DataSet
This new XML format is specific to the MySQL database server. Support for it was added in PHPUnit 3.5. Files in this format can be generated using the mysqldump
utility. Unlike CSV datasets, which mysqldump
also supports, a single file in this XML format can contain data for multiple tables. You can create a file in this format by invoking mysqldump
like so:
mysqldump --xml -t -u [username] --password=[password] [database] > /path/to/file.xml
This file can be used in your Database TestCase by calling the createMySQLXMLDataSet($filename)
method:
class MyTestCase extends PHPUnit_Extensions_Database_TestCase { public function getDataSet() { return $this->createMySQLXMLDataSet('/path/to/file.xml'); } }