作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想断言 PHP 页面上 XML 元素中的元素被断言为相等。
进行两次测试的最佳方法是什么。一个在第一个 ip 上,一个在第二个 ip 标签上?
将我的脚趾浸入水中片段:
public function testPPTPRangeChecker(){
echo "Loggin in as the adinistrator";
//Login as the administrator
$client = $this->myLoginAs('adminuser','Testing1!');
echo "The test user has successfully logged in as administrator";
$crawler = $client->request('POST', '/config/19/46');
echo "The site has navigated successfully to the config page";
$this->assertTag(
array(
'tag' => 'ip',
'content' => '192.168.1.1'
)
);
<pptpd>
<user>
<name>testuser</name>
<ip>192.168.1.1</ip>
<password>testpass</password>
</user>
<user>
<name>testuser2</name>
<ip>192.168.1.2</ip>
<password>testpass2</password>
</user>
</pptpd>
最佳答案
如果您只需要知道是否存在正确的 IP,您可以使用 xpath。如果您需要确保整个 XML 结构正确,您可以使用 assertEqualXMLStructure。
class MyTest extends \PHPUnit_Framework_TestCase
{
private $expectedXML = <<<EOF
<pptpd>
<user>
<name>testuser</name>
<ip>192.168.1.1</ip>
<password>testpass</password>
</user>
<user>
<name>testuser2</name>
<ip>192.168.1.2</ip>
<password>testpass2</password>
</user>
</pptpd>
EOF;
public function testMy1()
{
$actualXml = $this->expectedXML;
$doc = new \DomDocument();
$doc->loadXML($actualXml);
$xpath = new \DOMXPath($doc);
$this->assertSame(1, $xpath->query('//pptpd/user[ip="192.168.1.1"]')->length);
}
public function testMy2()
{
$actualXml = $this->expectedXML;
$expected = new \DOMDocument();
$expected->loadXML($this->expectedXML);
$actual = new \DOMDocument();
$actual->loadXML($actualXml);
$this->assertEqualXMLStructure(
$expected->firstChild->childNodes->item(1),
$actual->firstChild->childNodes->item(1),
true
);
}
}
关于php - 在 PHPUnit 中为页面上的 XML 元素测试断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363239/
我是一名优秀的程序员,十分优秀!