Skip to content

Instantly share code, notes, and snippets.

@rmehner
Created March 18, 2010 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmehner/336538 to your computer and use it in GitHub Desktop.
Save rmehner/336538 to your computer and use it in GitHub Desktop.
PHPUnit + Xpath
<?php
require_once 'PHPUnit/Framework.php';
class Xml
{
public function getNavigation()
{
return '<ul id="navigation">
<li><a href="/search">Suche</a></li>
<li><a href="/list">Liste</a></li>
<li><a href="/add">Hinzufügen</a></li>
</ul>';
}
}
class XmlTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->xml = new Xml();
}
/**
* @test
*/
public function It_shouldHaveTheCorrectElementCount()
{
$this->assertSelectCount('#navigation li', 3, $this->xml->getNavigation());
}
/**
* @test
*/
public function It_shouldHaveTheCorrectElementCountByXpath()
{
$xmlString = simplexml_load_string($this->xml->getNavigation());
$this->assertEquals(3, sizeof($xmlString->xpath('/ul[@id="navigation"]/li')));
}
/**
* @test
*/
public function It_shouldHaveTheCorrectElementCountByAssertTag()
{
$matcher = array(
'tag' => 'ul',
'attributes' => array('id' => 'navigation'),
'children' => array(
'count' => 3,
'only' => array('tag' => 'li')
)
);
$this->assertTag($matcher, $this->xml->getNavigation());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment