PHPUnit 数据库测试

PHPUnit 数据库测试。

1
$ composer require --dev phpunit/dbunit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use PHPUnit\DbUnit\TestCaseTrait;

class TestExample extends TestCase
{
use TestCaseTrait;

// 设置数据库连接,必须实现
public function getConnection()
{
// $pdo = new PDO();
$pdo = DB::connection();

return $this->createDefaultDBConnection($pdo, ':memory');
}

// 创建数据集,必须实现
protected function getDataSet()
{
// return $this->createFlatXMLDataSet(__DIR__.'/db_flat.xml');

// return $this->createXMLDataSet(__DIR__.'/db.xml');

return $this->createArrayDataSet(
[
'builds' => [
[
'id' => 1,
],
[
'id' => 2,
],
],
]);
}

public function test(){
$this->assertEquals(2, $this->getConnection()->getRowCount('builds'));
}
}

参考链接

0%