您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit数据库测试-createXMLDataSet
发布时间:2021-09-27 13:59:15编辑:雪饮阅读()
Phpunit使用createXMLDataSet创建数据集比起之前用createFlatXMLDataSet创建数据集将会对于null的处理能力更强,但同时对xml文件的格式有要求,如:
<dataset>
<table name="guestbook">
<column>id</column>
<column>content</column>
<column>user</column>
<column>created</column>
<row>
<value>1</value>
<value>Hello buddy!</value>
<value>joe</value>
<value>2010-04-24 17:15:23</value>
</row>
<row>
<value>2</value>
<value>I like it!</value>
<null />
<value>2010-04-26 12:14:20</value>
</row>
</table>
</dataset>
运行结果如:
use PHPUnit\Framework\TestCase;
use PHPUnit\DbUnit\TestCaseTrait;
abstract class MyAppTestsDatabaseTestCase extends TestCase
{
use TestCaseTrait;
static private $pdo = null;
private $conn = null;
final public function getConnection()
{
if ($this->conn === null) {
if (self::$pdo == null) {
self::$pdo = new PDO( $GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'] );
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
}
return $this->conn;
}
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/myXmlFixture.xml');
}
}
class MyAppTestsDatabaseTestCaseImplement extends MyAppTestsDatabaseTestCase{
public function testConnect(){
$l_conn=$this->getConnection();
var_dump($l_conn);
}
}
关键字词:createXMLDataSet,phpunit