您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit数据库状态断言(数据是否一致)
发布时间:2021-09-29 17:10:49编辑:雪饮阅读()
首先有用于建立于测试方法执行之前的数据集的基境的数据如:
1,"Hello buddy!","joe","2010-04-24 17:15:23"
2,"I like it!","nancy","2010-04-26 12:14:20"
<dataset>
<guestbook id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" />
<guestbook id="2" content="I like it!" user="nancy" created="2010-04-26 12:14:20" />
<guestbook id="3" content="Hello world!" user="suzy" created="2010-05-01 21:47:08" />
</dataset>
这些数据用来将mysql中数据拿出来与之比较进行断言的数据。
这种数据表状态断言即是通过的: 像是这样,则是没有办法通过数据库状态断言测试的:
use PHPUnit\DbUnit\DataSet\CsvDataSet;
use PHPUnit\Framework\TestCase;
use PHPUnit\DbUnit\TestCaseTrait;
class GuestbookTest 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()
{
$dataSet = new CsvDataSet();
$dataSet->addTable('guestbook', dirname(__FILE__)."/guestbook.csv");
return $dataSet;
}
public function testAddEntry()
{
$sql="insert into guestbook (content,user,created) VALUES ('Hello world!','suzy','2010-05-01 21:47:08')";
self::$pdo->exec($sql);
$queryTable = $this->getConnection()->createQueryTable(
'guestbook', 'SELECT * FROM guestbook'
);
$expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__)."/expectedBook.xml")
->getTable("guestbook");
$this->assertTablesEqual($expectedTable, $queryTable);
}
}
?>
use PHPUnit\DbUnit\DataSet\CsvDataSet;
use PHPUnit\Framework\TestCase;
use PHPUnit\DbUnit\TestCaseTrait;
class GuestbookTest 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()
{
$dataSet = new CsvDataSet();
$dataSet->addTable('guestbook', dirname(__FILE__)."/guestbook.csv");
return $dataSet;
}
public function testAddEntry()
{
$sql="insert into guestbook (content,user,created) VALUES ('Hello world!','suzy1','2010-05-01 21:47:08')";
self::$pdo->exec($sql);
$queryTable = $this->getConnection()->createQueryTable(
'guestbook', 'SELECT * FROM guestbook'
);
$expectedTable = $this->createFlatXmlDataSet(dirname(__FILE__)."/expectedBook.xml")
->getTable("guestbook");
$this->assertTablesEqual($expectedTable, $queryTable);
}
}
?>
关键字词:phpunit,数据库状态