I'm experimenting with using DOM and Xquery to find if a given element
exists in the page body.
I implemented two new "assert" methods, to convert the HTML to DOM and
then evaluate an Xquery expression against it.
Function assertXquery($query, $htmlString) just checks if the Xquery
returned non-empty results.
Function assertXqueryContains($query, $needle, $htmlString) checks if
the string value of the element returned by the Xquery contains the
string in $needle.
I'm hoping to improve this code and post it soon, to accompany my
example application I used for the webinar.
Regards,
Bill Karwin
class GridTest extends PHPUnit_Framework_TestCase
{
...setUp() function omitted...
protected function _xquery($query, $htmlString)
{
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadHTML($htmlString);
$xpath = new DOMXPath($doc);
$results = $xpath->query($query);
$this->assertFalse($results === false, "Xquery '$query'
failed");
return $results;
}
public function assertXqueryContains($query, $needle, $htmlString)
{
$results = $this->_xquery($query, $htmlString);
$this->assertNotEquals(0, $results->length,
"Found no match for xquery '$query'");
$haystack = $results->item(0)->nodeValue;
$this->assertType('string', $haystack);
$this->assertContains($needle, $haystack,
"Found no match for '$needle' contained in '$haystack'");
}
public function assertXquery($query, $htmlString)
{
$results = $this->_xquery($query, $htmlString);
$this->assertNotEquals(0, $results->length,
"Found no match for xquery '$query'");
}
public function testIndexControllerIndexAction()
{
$this->_request->setControllerName('grid');
$this->_request->setActionName('show');
$response = $this->_front->dispatch();
$this->assertFalse($response->isException());
$body = $response->getBody();
// probably should test one thing at a time, but this is just
for proof of concept
$this->assertXqueryContains('//head/title', 'Zend Framework',
$body);
$this->assertXqueryContains('//body/h1', 'Zend Framework',
$body);
$this->assertXqueryContains('//body/h2', 'Tables', $body);
$this->assertXquery('//body/ul', $body);
}
}
> -----Original Message-----
> From: Matthew Weier O'Phinney [mailto:
matthew@...]
> Sent: Friday, June 22, 2007 9:50 AM
> To:
fw-mvc@...
> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
>
> -- chelala <
chelala@...> wrote (on Friday, 22 June
> 2007, 09:38 AM -0700):
> > How to use PHPUnit in my Zend Framework MVC project. Say,
> for testing
> > one controller's action, some model class function of my
> own or a view
> > scripts ???
> >
> > How to prepare those kind of tests ? I do not know too
> much, but I can
> > not imagine how to setup the test, as all the proccess
> happens, from
> > the front controller in the bootstrapper to the end.
>
> It's not too difficult, fortunately. Here's an example skeleton:
>
> class FooControllerTest extends PHPUnit_Framework_TestCase {
> public function setUp()
> {
> $this->front = Zend_Controller_Front::getInstance();
> $this->front->addModuleDirectory('/path/to/modules');
> // path to modules and hence controllers
> $this->front->resetInstance(); // clear out any
> settings from prior runs
> $this->front->returnResponse(true); // don't
> auto-emit the response
> }
>
> public function testIndexPageContents()
> {
> // The URL is primarily used so the request URI and related
> // information can be set:
> $request = new
> Zend_Controller_Request_Http('
http://localhost/');>
> // Because returnResponse() has been set to true, we can grab
> // the response object this way, and not worry about it being
> // auto-emitted:
> $response = $this->front->dispatch($request);
>
> // Now you can test!
> $this->assertFalse($response->isException()); // no
> exceptions!
>
> // test that content contains certain strings
> $this->assertContains('index page', $response->getBody());
>
> // etc...
> }
> }
>
> Hope that will help you get started.
>
> --
> Matthew Weier O'Phinney
> PHP Developer |
matthew@...
> Zend - The PHP Company |
http://www.zend.com/>