mrkrstphr / dbunit-fixture-arrays
PHPUnit DbUnit 本地数组基础测试用例
v1.0.0
2014-08-15 18:26 UTC
Requires
- php: >=5.5
- phpunit/dbunit: >=1.3.0@stable
- phpunit/phpunit: >=3.7.0@stable
Requires (Dev)
- squizlabs/php_codesniffer: >=1.5.4@stable
This package is auto-updated.
Last update: 2024-09-11 04:49:55 UTC
README
PHPUnit DbUnit 的本地 PHP 数组基础测试用例提供程序
dbunit-fixture-arrays 提供了将固定数据集(数据集)作为本地 PHP 数组提供给 PHPUnit 的 DbUnit 的功能,这与 DbUnit 当前支持的许多其他驱动程序不同,例如 Yaml、XML、CSV 等。
如何实现?
只需使用 composer 安装
composer require mrkrstphr/dbunit-fixture-arrays:@stable
然后将它作为任何其他数据集格式一样连接到 DbUnit
public function getDataSet() { return new ArrayDataSet([ '/path_to_file.php', '/path_to_other_file.php' ]); }
文件看起来像这样
return [ 'table_name' => [ ['id' => 1, 'name' => 'Row Data 1'], ['id' => 2, 'name' => 'Row Data 2'], ], 'other_table_name' => [ ['id' => 99, 'color' => 'Red'], ['id' => 88, 'color' => 'Blue'], ], ];
为什么?
有时在固定文件中加入一点逻辑是件好事。比如说,你需要一些测试数据,其中包含未来日期,但不超过1年。不不断更新你的固定数据,就没有可靠地做到这一点的方法。
使用 dbunit-fixture-arrays,你可以
return [ 'table_name' => [ ['id' => 1, 'date' => (new DateTime('+1 year'))->format('Y-m-d')], ['id' => 2, 'date' => '2014-05-01' ] ];
其他好处包括,由于不需要解析 Yaml 或 XML,因此测试速度略有提高,内存占用也略有减少——你的结果可能不同。
贡献
建议或错误?提交问题或发起拉取请求!