Skip to content

Commit

Permalink
Fixed fatal error when calling Configuration::get when constructing w…
Browse files Browse the repository at this point in the history
…ithout a XSD. (fixes #5)

Also clarified intention and usage of Configuration class in docblocks
and added a warning when a valid XSD is not provided.
  • Loading branch information
niels-nijens committed Jul 16, 2015
1 parent 583a45d commit 71b54f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/**
* Configuration
*
* Class to validate and read XML configuration files. Intended to work only with a XML schema file.
*
* @author Niels Nijens <niels@connectholland.nl>
* @package Nijens\Utilities
**/
Expand Down Expand Up @@ -50,11 +52,11 @@ class Configuration
/**
* __construct
*
* Constructs a new Configuration
* Constructs a new Configuration instance
*
* @access public
* @param string|null $defaultConfigurationFile Location of the default XML configuration file
* @param string|null $xsdSchemaFile Location of the XSD file for configuration validation
* @param string|null $xsdSchemaFile Location of the XSD file for configuration validation. Optional so that a default schema can also be provided by a subclass.
* @return void
**/
public function __construct($defaultConfigurationFile = null, $xsdSchemaFile = null)
Expand Down Expand Up @@ -135,6 +137,10 @@ public function getDOMDocument()
**/
public function get($xpathExpression, $alwaysReturnArray = false)
{
if (($this->xpath instanceof DOMXPath) === false) {
return;
}

$nodeList = @$this->xpath->query($xpathExpression);
if ($nodeList instanceof DOMNodeList) {
if ($nodeList->length === 1) {
Expand Down Expand Up @@ -208,6 +214,8 @@ private function validateConfiguration(DOMDocument $dom)
}

libxml_use_internal_errors(false);
} else {
trigger_error('A valid schema file must be provided.', E_USER_WARNING);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testLoadInvalidConfigurationLoadsDefaultConfigurationAfterTrigge
* Tests if Configation::loadConfiguration triggers a E_USER_WARNING when a XML schema is not supplied
*
* @expectedException PHPUnit_Framework_Error_Warning
* @expectedExceptionMessage A valid schema file must be supplied.
* @expectedExceptionMessage A valid schema file must be provided.
*
* @access public
* @return null
Expand Down

0 comments on commit 71b54f8

Please sign in to comment.