PHP断言()如何正常工作?

时间:2011-07-27 06:06:28

标签: php assertion

我试图做一个简单的PHP断言来验证一个字符串不是空的,但结果令人困惑。

我不知道assert()中的字符串评估功能,但检查了文档并理解了该部分。

我将代码包含在所有测试用例中,然后是输出,然后是问题。当一个案例因语法错误而失败时,我只是将其注释掉并重复测试,以便执行以下情况,并显示每次执行的输出。

代码是关于使用XMLReader定位XML元素,但恰好是我的代码。所以这里首先是代码:

<?php

$xml = <<< XML
<depts  xmlns:apl="urn:my:ns" >
    <apl:dept>Dept One</apl:dept>
    <apl:dept>Dept Tow</apl:dept>
</depts>
XML;


$elmLocal = 'dept';
$elmUrn = "urn:my:ns";

$xr = new XMLReader;
$xr->XML($xml);

// move to the first desired element node
$found = false;
while ($xr->read()) {
    if ($xr->localName === $elmLocal && $xr->namespaceURI === $elmUrn) {
        $found = true;
        break;
    }
}
if (!$found) {
    exit("---Error--- No element found with given Name/NS.\n");
}



$var = $xr->name;

echo "---------------- Test Results ---------------->\n";

echo "----- var    =={$var}==\n";
echo "----- xr name=={$xr->name}==\n\n";


echo "------ assertion 00 ------>\n";
assert ($xr->name);

echo "------ assertion 01 ------>\n";
assert ($var);

echo "------ assertion 02 ------>\n";
assert ('$xr->name!==""');   

echo "------ assertion 03 ------>\n";
assert ('$var!==""');   

echo "------ assertion 04 ------>\n";
assert ("{$xr->name}!==''");   

echo "------ assertion 05 ------>\n";
assert ("{$var}!==''");   

echo "------ assertion 06 ------>\n";
assert(!empty($xr->name));

echo "------ assertion 07 ------>\n";
assert(!empty($var));

echo "------ assertion 08 ------>\n";
assert('!empty($xr->name)');

echo "------ assertion 09 ------>\n";
assert('!empty($var)');

echo "------ assertion 10 ------>\n";
assert("!empty({$xr->name})");

echo "------ assertion 11 ------>\n";
assert("!empty({$var})");



// do some processing
$domNode = $xr->expand(); // DOMNode XMLReader::expand ([ DOMNode $basenode ] )


?>

这里是执行输出 - 再次注意到有关于致命语法失败和重复执行的注释,你可以假设每个断言都是单独测试的:

#tests#php -v
PHP 5.3.3-7+squeeze3 with Suhosin-Patch (cli) (built: Jun 28 2011 13:13:26)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH


#tests#php -q vars.php
---------------- Test Results ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 00 ------>
PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(40) : assert code on line 1
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:40
PHP Catchable fatal error:  assert(): Failure evaluating code:
apl:dept in /shared/tests/vars.php on line 40
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:40

#tests#php -q vars.php
---------------- Test Results ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 01 ------>
PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(43) : assert code on line 1
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:43
PHP Catchable fatal error:  assert(): Failure evaluating code:
apl:dept in /shared/tests/vars.php on line 43
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:43

#tests#php -q vars.php
---------------- Test Results ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 04 ------>
PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(52) : assert code on line 1
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:52
PHP Catchable fatal error:  assert(): Failure evaluating code:
apl:dept!=='' in /shared/tests/vars.php on line 52
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:52

#tests#php -q vars.php
---------------- Test Results ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 05 ------>
PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(55) : assert code on line 1
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:55
PHP Catchable fatal error:  assert(): Failure evaluating code:
apl:dept!=='' in /shared/tests/vars.php on line 55
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:55

#tests#php -q vars.php
---------------- Test Results ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 06 ------>
PHP Warning:  assert(): Assertion failed in /shared/tests/vars.php on line 58
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:58
------ assertion 07 ------>
------ assertion 08 ------>
PHP Warning:  assert(): Assertion "!empty($xr->name)" failed in /shared/tests/vars.php on line 64
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:64
------ assertion 09 ------>
------ assertion 10 ------>
PHP Parse error:  syntax error, unexpected ':', expecting T_PAAMAYIM_NEKUDOTAYIM in /shared/tests/vars.php(70) : assert code on line 1
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:70
PHP Catchable fatal error:  assert(): Failure evaluating code:
!empty(apl:dept) in /shared/tests/vars.php on line 70
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:70

#tests#php -q vars.php
---------------- Test Results ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 06 ------>
PHP Warning:  assert(): Assertion failed in /shared/tests/vars.php on line 58
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:58
------ assertion 07 ------>
------ assertion 08 ------>
PHP Warning:  assert(): Assertion "!empty($xr->name)" failed in /shared/tests/vars.php on line 64
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:64
------ assertion 09 ------>
------ assertion 11 ------>
PHP Parse error:  syntax error, unexpected ':', expecting T_PAAMAYIM_NEKUDOTAYIM in /shared/tests/vars.php(73) : assert code on line 1
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:73
PHP Catchable fatal error:  assert(): Failure evaluating code:
!empty(apl:dept) in /shared/tests/vars.php on line 73
PHP Stack trace:
PHP   1. {main}() /shared/tests/vars.php:0
PHP   2. assert() /shared/tests/vars.php:73

现在有些评论:

案例1,2是可以理解的 - 即字符串评估“功能”。

断言表达式用双引号括起来的所有情况 - 也将它们弄清楚:首先进行变量解析,这导致执行中的语法错误。因此,为了使案例4为例,我们可以这样做:

echo "------ assertion 04 ------>\n";
assert ("'{$xr->name}'!==''");   

现在真正的问题是案例6,7。为什么6在7成功时失败了。情况8,9 - 8失败而9成功。

问候。

2 个答案:

答案 0 :(得分:2)

这不是关于assert()如何工作,而是关于empty()如何工作 当与$object->inaccessibleProp一起使用时,首先尝试与isset()相同的事情,如果这个“test”返回false,则empty()甚至不会尝试访问该元素但返回true。 而且,XMLReader类显然没有按照你需要的方式实现__isset()方法。

<?php
$xml = <<< XML
<depts  xmlns:apl="urn:my:ns" >
    <apl:dept>Dept One</apl:dept>
    <apl:dept>Dept Tow</apl:dept>
</depts>
XML;

$elmLocal = 'dept';
$elmUrn = "urn:my:ns";

$xr = new XMLReader;
$xr->XML($xml);

// move to the first desired element node
$found = false;
while ($xr->read()) {
    if ($xr->localName === $elmLocal && $xr->namespaceURI === $elmUrn) {
        $found = true;
        break;
    }
}
if (!$found) {
    exit("---Error--- No element found with given Name/NS.\n");
}

$var = $xr->name;
var_dump(isset($xr->name));
var_dump(empty($xr->name));
echo $xr->name;

打印

bool(false)
bool(true)
apl:dept

答案 1 :(得分:0)

Now the real issue is with cases 6,7. Why is 6 failing while 7 is succeeding. The same with cases 8,9 - 8 failing while 9 succeeding.

因为它不是标量类型。