Skip to content

Commit

Permalink
Implemented XInclude support for phpunit.xml.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyS committed Oct 4, 2012
1 parent cb9425b commit b825d33
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 5 deletions.
2 changes: 1 addition & 1 deletion PHPUnit/Util/Configuration.php
Expand Up @@ -188,7 +188,7 @@ class PHPUnit_Util_Configuration
protected function __construct($filename) protected function __construct($filename)
{ {
$this->filename = $filename; $this->filename = $filename;
$this->document = PHPUnit_Util_XML::loadFile($filename); $this->document = PHPUnit_Util_XML::loadFile($filename, FALSE, TRUE);
$this->xpath = new DOMXPath($this->document); $this->xpath = new DOMXPath($this->document);
} }


Expand Down
23 changes: 19 additions & 4 deletions PHPUnit/Util/XML.php
Expand Up @@ -78,10 +78,11 @@ public static function prepareString($string)
* *
* @param string $filename * @param string $filename
* @param boolean $isHtml * @param boolean $isHtml
* @param boolean $xinclude
* @return DOMDocument * @return DOMDocument
* @since Method available since Release 3.3.0 * @since Method available since Release 3.3.0
*/ */
public static function loadFile($filename, $isHtml = FALSE) public static function loadFile($filename, $isHtml = FALSE, $xinclude = FALSE)
{ {
$reporting = error_reporting(0); $reporting = error_reporting(0);
$contents = file_get_contents($filename); $contents = file_get_contents($filename);
Expand All @@ -96,7 +97,7 @@ public static function loadFile($filename, $isHtml = FALSE)
); );
} }


return self::load($contents, $isHtml, $filename); return self::load($contents, $isHtml, $filename, $xinclude);
} }


/** /**
Expand All @@ -105,7 +106,9 @@ public static function loadFile($filename, $isHtml = FALSE)
* *
* If $actual is already a DOMDocument, it is returned with * If $actual is already a DOMDocument, it is returned with
* no changes. Otherwise, $actual is loaded into a new DOMDocument * no changes. Otherwise, $actual is loaded into a new DOMDocument
* as either HTML or XML, depending on the value of $isHtml. * as either HTML or XML, depending on the value of $isHtml. If $isHtml is
* false and $xinclude is true, xinclude is performed on the loaded
* DOMDocument.
* *
* Note: prior to PHPUnit 3.3.0, this method loaded a file and * Note: prior to PHPUnit 3.3.0, this method loaded a file and
* not a string as it currently does. To load a file into a * not a string as it currently does. To load a file into a
Expand All @@ -114,18 +117,21 @@ public static function loadFile($filename, $isHtml = FALSE)
* @param string|DOMDocument $actual * @param string|DOMDocument $actual
* @param boolean $isHtml * @param boolean $isHtml
* @param string $filename * @param string $filename
* @param boolean $xinclude
* @return DOMDocument * @return DOMDocument
* @since Method available since Release 3.3.0 * @since Method available since Release 3.3.0
* @author Mike Naberezny <mike@maintainable.com> * @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com> * @author Derek DeVries <derek@maintainable.com>
* @author Tobias Schlitt <toby@php.net>
*/ */
public static function load($actual, $isHtml = FALSE, $filename = '') public static function load($actual, $isHtml = FALSE, $filename = '', $xinclude = FALSE)
{ {
if ($actual instanceof DOMDocument) { if ($actual instanceof DOMDocument) {
return $actual; return $actual;
} }


$document = new DOMDocument; $document = new DOMDocument;

$internal = libxml_use_internal_errors(TRUE); $internal = libxml_use_internal_errors(TRUE);
$message = ''; $message = '';
$reporting = error_reporting(0); $reporting = error_reporting(0);
Expand All @@ -136,6 +142,15 @@ public static function load($actual, $isHtml = FALSE, $filename = '')
$loaded = $document->loadXML($actual); $loaded = $document->loadXML($actual);
} }


if ('' !== $filename) {
// Necessary for xinclude
$document->documentURI = $filename;
}

if (!$isHtml && $xinclude) {
$document->xinclude();
}

foreach (libxml_get_errors() as $error) { foreach (libxml_get_errors() as $error) {
$message .= $error->message; $message .= $error->message;
} }
Expand Down
55 changes: 55 additions & 0 deletions Tests/Util/ConfigurationTest.php
Expand Up @@ -331,4 +331,59 @@ public function testSeleniumBrowserConfigurationIsReadCorrectly()
$this->configuration->getSeleniumBrowserConfiguration() $this->configuration->getSeleniumBrowserConfiguration()
); );
} }

public function testXincludeInConfiguration()
{
$configurationWithXinclude = PHPUnit_Util_Configuration::getInstance(
dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'configuration_xinclude.xml'
);

$this->assertConfigurationEquals(
$this->configuration,
$configurationWithXinclude
);
}

/**
* Asserts that the values in $actualConfiguration equal $expectedConfiguration.
*
* @param PHPUnit_Util_Configuration $expectedConfiguration
* @param PHPUnit_Util_Configuration $actualConfiguration
* @return void
*/
protected function assertConfigurationEquals( PHPUnit_Util_Configuration $expectedConfiguration, PHPUnit_Util_Configuration $actualConfiguration )
{
$this->assertEquals(
$expectedConfiguration->getFilterConfiguration(),
$actualConfiguration->getFilterConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getGroupConfiguration(),
$actualConfiguration->getGroupConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getListenerConfiguration(),
$actualConfiguration->getListenerConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getLoggingConfiguration(),
$actualConfiguration->getLoggingConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getPHPConfiguration(),
$actualConfiguration->getPHPConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getPHPUnitConfiguration(),
$actualConfiguration->getPHPUnitConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getSeleniumBrowserConfiguration(),
$actualConfiguration->getSeleniumBrowserConfiguration()
);
$this->assertEquals(
$expectedConfiguration->getTestSuiteConfiguration(),
$actualConfiguration->getTestSuiteConfiguration()
);
}
} }
68 changes: 68 additions & 0 deletions Tests/_files/configuration_xinclude.xml
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8" ?>

<phpunit backupGlobals="true"
backupStaticAttributes="false"
bootstrap="/path/to/bootstrap.php"
cacheTokens="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
stopOnFailure="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
strict="false"
verbose="false">
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="configuration.xml"
parse="xml"
xpointer="xpointer(/phpunit/testsuites)" />

<groups>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="configuration.xml"
parse="xml"
xpointer="xpointer(/phpunit/groups/*)" />
</groups>


<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="configuration.xml"
parse="xml"
xpointer="xpointer(/phpunit/filter)" />
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="configuration.xml"
parse="xml"
xpointer="xpointer(/phpunit/listeners)" />
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="configuration.xml"
parse="xml"
xpointer="xpointer(/phpunit/logging)" />

<php>
<includePath>.</includePath>
<includePath>/path/to/lib</includePath>
<ini name="foo" value="bar"/>
<const name="FOO" value="false"/>
<const name="BAR" value="true"/>
<var name="foo" value="false"/>
<env name="foo" value="true"/>
<post name="foo" value="bar"/>
<get name="foo" value="bar"/>
<cookie name="foo" value="bar"/>
<server name="foo" value="bar"/>
<files name="foo" value="bar"/>
<request name="foo" value="bar"/>
</php>

<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="configuration.xml"
parse="xml"
xpointer="xpointer(/phpunit/selenium)" />
</phpunit>

0 comments on commit b825d33

Please sign in to comment.