ZF-9881: Allow registration of XPath namespaces in Zend_Test_PHPUnit_ControllerTestCase

Description

Using a Controller Test Case, it is currently difficult to test XML responses that contain multiple namespaces. As an example, parts of the following XML response document are untestable as-is:


<?xml version="1.0" encoding="utf-8"?>
www.w3.org/2007/app"
         xmlns:atom="http://www.w3.org/2005/Atom">
  My WorkspaceMy Collection

The following test will not work without namespace registration:


public function testIndexActionResponseContainsCorrectWorkspaceTitle()
{
    $this->getRequest()->setMethod('GET');
    $this->dispatch('/');
    $this->assertXpathContentContains(
        '/service/workspace[1]/atom:title',
        'My Workspace'
    );
}

This test will result in the error:

bq. Failed asserting node denoted by /service/workspace[1]/atom:title CONTAINS content "My Workspace"

If I don't use a default namespace in the XML document, then it becomes testable:


<?xml version="1.0" encoding="utf-8"?>
www.w3.org/2007/app"
             xmlns:atom="http://www.w3.org/2005/Atom">
  My WorkspaceMy Collection

The following test will now work with that updated XML:


public function testIndexActionResponseContainsCorrectWorkspaceTitle()
{
    $this->getRequest()->setMethod('GET');
    $this->dispatch('/');
    $this->assertXpathContentContains(
        '/app:service/app:workspace[1]/atom:title',
        'My Workspace'
    );
 }

However, I should be able to have a default namespace and still be able to test my responses. If I were using atom feeds (which I am in an application) then, for the most interoperability, it is recommended that the default namespace be 'http://www.w3.org/2005/Atom'. I shouldn't have to choose between interoperability and testability in this situation.

I will be submitting a patch for review that allows namespace registration within a Controller Test Case, ideally within setUp(), as follows:


$this->registerXpathNamespaces(array(
    'app'   => 'http://www.w3.org/2007/app',
    'atom'  => 'http://www.w3.org/2005/Atom',
));

Comments

Attached is a patch that allows for namespace registration in Controller Tests.