您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit数据库测试-null字段模拟
发布时间:2021-09-27 13:40:23编辑:雪饮阅读()
在phpunit进行测试数据库的时候,有时候需要模拟null这种值,但是这种值它在xml的数据集上面不好表示,那么可以利用忽略掉某个字段(无论是值或者键)。如:
guestbook-seed.xml:
<?xml version="1.0" ?>
<dataset>
<guestbook id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" />
<guestbook id="2" content="I like it!" created="2010-04-26 12:14:20" />
</dataset>
<dataset>
<guestbook id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" />
<guestbook id="2" content="I like it!" created="2010-04-26 12:14:20" />
</dataset>
这里第二个数据条目就缺少user字段,那么这里就将第二个数据条目的user字段认定为null。
那么运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyAppTestsDatabaseTestCase.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyAppTestsDatabaseTestCase.php:29:
class PHPUnit\DbUnit\Database\DefaultConnection#17 (2) {
protected $connection =>
class PDO#15 (0) {
}
protected $metaData =>
class PHPUnit\DbUnit\Database\Metadata\MySQL#19 (4) {
protected $schemaObjectQuoteChar =>
string(1) "`"
protected $pdo =>
class PDO#15 (0) {
}
protected $schema =>
string(11) "myguestbook"
protected $truncateCommand =>
string(8) "TRUNCATE"
}
}
Time: 111 ms, Memory: 4.00 MB
There was 1 risky test:
1) MyAppTestsDatabaseTestCaseImplement::testConnect
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyAppTestsDatabaseTestCase.php:27
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.
关于null的补充:
flat xml 数据集现在做出一个关键假设,定义表的第一个定义行上的属性定义了该表的列。在前面的例子中,这意味着 “ id ”、“ content ”、“ user ”和 “ created ”是留言簿表的列。对于未定义“ user ”的第二行,将在数据库中插入 NULL。
当从数据集中删除第一个留言簿条目时,只有 “ id ”、“ content ”和 “ created ”将成为留言簿表的列,因为未指定“ user ”。
要在 NULL 值相关时有效地使用 Flat XML 数据集,每个表的第一行不得包含任何 NULL 值,并且只允许连续行省略属性。这可能很尴尬,因为行的顺序是数据库断言的相关因素。
反过来,如果您仅指定 Flat XML 数据集中表列的子集,则所有省略的值都将设置为其默认值。如果省略的列之一被定义为“ NOT NULL DEFAULT NULL ”,这将导致错误。
总之,如果您不需要 NULL 值,我只能建议使用 Flat XML 数据集。
关键字词:phpunit,null