Reputation: 354
I want to assert that the element within an XML element on a page in PHP is asserted as equal.
What's the best way to do two tests. One on the first ip and one on the second ip tag?
Dipping my toes in the water snippet:
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>
Upvotes: 1
Views: 506
Reputation: 3967
If all you need to know is whether the correct IP is present you can use xpath. And if you need to be sure that the whole XML structure is correct you can use 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
);
}
}
Upvotes: 1