Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'milestones/exceptions' of git://git.zendframework.com/z…
Browse files Browse the repository at this point in the history
…f into serializer-exceptions

Conflicts:

	working/README-exceptions.txt
  • Loading branch information
marc-mabe committed Sep 29, 2010
7 parents 196ca18 + 17cbc64 + 8f4a51f + 88ec12d + 31ab35e + 59c83c4 + 3fe17b4 commit d50da4c
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/Config.php
Expand Up @@ -169,7 +169,7 @@ public function __set($name, $value)
}
$this->_count = count($this->_data);
} else {
throw new Exception('Zend_Config is read only');
throw new Exception\InvalidArgumentException('Zend_Config is read only');
}
}

Expand Down Expand Up @@ -236,7 +236,7 @@ public function __unset($name)
$this->_count = count($this->_data);
$this->_skipNextIteration = true;
} else {
throw new Exception('Zend_Config is read only');
throw new Exception\InvalidArgumentException('Zend_Config is read only');
}

}
Expand Down Expand Up @@ -427,7 +427,7 @@ protected function _assertValidExtend($extendingSection, $extendedSection)
$extendedSectionCurrent = $extendedSection;
while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
throw new Exception('Illegal circular inheritance detected');
throw new Exception\RuntimeException('Illegal circular inheritance detected');
}
$extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exception.php
Expand Up @@ -31,4 +31,5 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Exception extends \Zend\Exception {}
interface Exception
{}
9 changes: 9 additions & 0 deletions src/Exception/InvalidArgumentException.php
@@ -0,0 +1,9 @@
<?php

namespace Zend\Config\Exception;

class InvalidArgumentException
extends \InvalidArgumentException
implements \Zend\Config\Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/RuntimeException.php
@@ -0,0 +1,9 @@
<?php

namespace Zend\Config\Exception;

class RuntimeException
extends \RuntimeException
implements \Zend\Config\Exception
{
}
14 changes: 7 additions & 7 deletions src/Ini.php
Expand Up @@ -100,7 +100,7 @@ class Ini extends Config
public function __construct($filename, $section = null, $options = false)
{
if (empty($filename)) {
throw new Exception('Filename is not set');
throw new Exception\InvalidArgumentException('Filename is not set');
}

$allowModifications = false;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function __construct($filename, $section = null, $options = false)
$dataArray = array();
foreach ($section as $sectionName) {
if (!isset($iniArray[$sectionName])) {
throw new Exception("Section '$sectionName' cannot be found in $filename");
throw new Exception\InvalidArgumentException("Section '$sectionName' cannot be found in $filename");
}
$dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);

Expand All @@ -166,7 +166,7 @@ protected function _parseIniFile($filename)

// Check if there was a error while loading file
if ($this->_loadFileErrorStr !== null) {
throw new Exception($this->_loadFileErrorStr);
throw new Exception\RuntimeException($this->_loadFileErrorStr);
}

return $iniArray;
Expand Down Expand Up @@ -203,7 +203,7 @@ protected function _loadIniFile($filename)
break;

default:
throw new Exception("Section '$thisSection' may not extend multiple sections in $filename");
throw new Exception\RuntimeException("Section '$thisSection' may not extend multiple sections in $filename");
}
}

Expand Down Expand Up @@ -234,7 +234,7 @@ protected function _processSection($iniArray, $section, $config = array())
$config = $this->_processSection($iniArray, $value, $config);
}
} else {
throw new Exception("Parent section '$section' cannot be found");
throw new Exception\RuntimeException("Parent section '$section' cannot be found");
}
} else {
$config = $this->_processKey($config, $key, $value);
Expand Down Expand Up @@ -266,11 +266,11 @@ protected function _processKey($config, $key, $value)
$config[$pieces[0]] = array();
}
} elseif (!is_array($config[$pieces[0]])) {
throw new Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
throw new Exception\RuntimeException("Cannot create sub-key for '{$pieces[0]}' as key already exists");
}
$config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
} else {
throw new Exception("Invalid key '$key'");
throw new Exception\RuntimeException("Invalid key '$key'");
}
} else {
$config[$key] = $value;
Expand Down
6 changes: 3 additions & 3 deletions src/Writer/AbstractFileWriter.php
Expand Up @@ -101,11 +101,11 @@ public function write($filename = null, Config\Config $config = null, $exclusive
}

if ($this->_filename === null) {
throw new Config\Exception('No filename was set');
throw new Config\Exception\InvalidArgumentException('No filename was set');
}

if ($this->_config === null) {
throw new Config\Exception('No config was set');
throw new Config\Exception\InvalidArgumentException('No config was set');
}

$configString = $this->render();
Expand All @@ -119,7 +119,7 @@ public function write($filename = null, Config\Config $config = null, $exclusive
$result = @file_put_contents($this->_filename, $configString, $flags);

if ($result === false) {
throw new Config\Exception('Could not write to file "' . $this->_filename . '"');
throw new Config\Exception\RuntimeException('Could not write to file "' . $this->_filename . '"');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Writer/Ini.php
Expand Up @@ -159,7 +159,7 @@ protected function _prepareValue($value)
} elseif (strpos($value, '"') === false) {
return '"' . $value . '"';
} else {
throw new Config\Exception('Value can not contain double quotes "');
throw new Config\Exception\RuntimeException('Value can not contain double quotes "');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Writer/Xml.php
Expand Up @@ -100,7 +100,7 @@ protected function _addBranch(Config\Config $config, \SimpleXMLElement $xml, \Si
$branchType = 'string';
}
} else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
throw new Config\Exception('Mixing of string and numeric keys is not allowed');
throw new Config\Exception\RuntimeException('Mixing of string and numeric keys is not allowed');
}

if ($branchType === 'numeric') {
Expand Down
18 changes: 9 additions & 9 deletions src/Xml.php
Expand Up @@ -70,7 +70,7 @@ class Xml extends Config
public function __construct($xml, $section = null, $options = false)
{
if (empty($xml)) {
throw new Exception('Filename is not set');
throw new Exception\InvalidArgumentException('Filename is not set');
}

$allowModifications = false;
Expand All @@ -95,7 +95,7 @@ public function __construct($xml, $section = null, $options = false)
restore_error_handler();
// Check if there was a error while loading file
if ($this->_loadFileErrorStr !== null) {
throw new Exception($this->_loadFileErrorStr);
throw new Exception\InvalidArgumentException($this->_loadFileErrorStr);
}

if ($section === null) {
Expand All @@ -109,7 +109,7 @@ public function __construct($xml, $section = null, $options = false)
$dataArray = array();
foreach ($section as $sectionName) {
if (!isset($config->$sectionName)) {
throw new Exception("Section '$sectionName' cannot be found in $xml");
throw new Exception\InvalidArgumentException("Section '$sectionName' cannot be found in $xml");
}

$dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
Expand All @@ -118,7 +118,7 @@ public function __construct($xml, $section = null, $options = false)
parent::__construct($dataArray, $allowModifications);
} else {
if (!isset($config->$section)) {
throw new Exception("Section '$section' cannot be found in $xml");
throw new Exception\InvalidArgumentException("Section '$section' cannot be found in $xml");
}

$dataArray = $this->_processExtends($config, $section);
Expand Down Expand Up @@ -146,7 +146,7 @@ public function __construct($xml, $section = null, $options = false)
protected function _processExtends(\SimpleXMLElement $element, $section, array $config = array())
{
if (!isset($element->$section)) {
throw new Exception("Section '$section' cannot be found");
throw new Exception\RuntimeException("Section '$section' cannot be found");
}

$thisSection = $element->$section;
Expand Down Expand Up @@ -202,7 +202,7 @@ protected function _toArray(\SimpleXMLElement $xmlObject)
// Search for local 'const' nodes and replace them
if (count($xmlObject->children(self::XML_NAMESPACE)) > 0) {
if (count($xmlObject->children()) > 0) {
throw new Exception("A node with a 'const' childnode may not have any other children");
throw new Exception\RuntimeException("A node with a 'const' childnode may not have any other children");
}

$dom = dom_import_simplexml($xmlObject);
Expand All @@ -220,13 +220,13 @@ protected function _toArray(\SimpleXMLElement $xmlObject)
switch ($node->localName) {
case 'const':
if (!$node->hasAttributeNS(self::XML_NAMESPACE, 'name')) {
throw new Exception("Misssing 'name' attribute in 'const' node");
throw new Exception\RuntimeException("Misssing 'name' attribute in 'const' node");
}

$constantName = $node->getAttributeNS(self::XML_NAMESPACE, 'name');

if (!defined($constantName)) {
throw new Exception("Constant with name '$constantName' was not defined");
throw new Exception\RuntimeException("Constant with name '$constantName' was not defined");
}

$constantValue = constant($constantName);
Expand All @@ -235,7 +235,7 @@ protected function _toArray(\SimpleXMLElement $xmlObject)
break;

default:
throw new Exception("Unknown node with name '$node->localName' found");
throw new Exception\RuntimeException("Unknown node with name '$node->localName' found");
}
}

Expand Down
14 changes: 7 additions & 7 deletions test/ConfigTest.php
Expand Up @@ -126,14 +126,14 @@ public function testModification()

public function testNoModifications()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'is read only');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'is read only');
$config = new Config($this->_all);
$config->hostname = 'test';
}

public function testNoNestedModifications()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'is read only');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'is read only');
$config = new Config($this->_all);
$config->db->host = 'test';
}
Expand Down Expand Up @@ -195,7 +195,7 @@ public function testArray()

public function testErrorWriteToReadOnly()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'read only');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'read only');
$config = new Config($this->_all);
$config->test = '32';
}
Expand Down Expand Up @@ -268,7 +268,7 @@ public function testUnsetException()

$this->assertTrue(isset($config->hostname)); // top level

$this->setExpectedException('\\Zend\\Config\\Exception', 'is read only');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'is read only');
unset($config->hostname);
}

Expand Down Expand Up @@ -354,7 +354,7 @@ public function testSetReadOnly()
$config->b = 'b';

$config->setReadOnly();
$this->setExpectedException('\\Zend\\Config\\Exception', 'is read only');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'is read only');
$config->c = 'c';
}

Expand Down Expand Up @@ -394,14 +394,14 @@ public function testMergeHonoursAllowModificationsFlagAtAllLevels()
$config2->merge($config);
try {
$config2->key2 = 'no';
} catch (\Zend\Config\Exception $e) {
} catch (\Zend\Config\Exception\RuntimeException $e) {
$this->fail('Unexpected exception at top level has been raised: ' . $e->getMessage());
}
$this->assertEquals('no', $config2->key2);

try {
$config2->key->nested = 'no';
} catch (\Zend\Config\Exception $e) {
} catch (\Zend\Config\Exception\RuntimeException $e) {
$this->fail('Unexpected exception on nested object has been raised: ' . $e->getMessage());
}
$this->assertEquals('no', $config2->key->nested);
Expand Down
18 changes: 9 additions & 9 deletions test/IniTest.php
Expand Up @@ -108,7 +108,7 @@ public function testMultiDepthExtends()

public function testErrorNoExtendsSection()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'cannot be found');
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'cannot be found');
$config = new Ini($this->_iniFileConfig, 'extendserror');
}

Expand All @@ -119,15 +119,15 @@ public function testInvalidKeys()
try {
$config = new Ini($this->_iniFileConfig, $section);
$this->fail('An expected Zend\\Config\\Exception has not been raised');
} catch (\Zend\Config\Exception $expected) {
} catch (\Zend\Config\Exception\RuntimeException $expected) {
$this->assertContains('Invalid key', $expected->getMessage());
}
}
}

public function testZF426()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'Cannot create sub-key for');
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'Cannot create sub-key for');
$config = new Ini($this->_iniFileConfig, 'zf426');
}

Expand Down Expand Up @@ -164,32 +164,32 @@ public function testZF414()

public function testZF415()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'circular inheritance');
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'circular inheritance');
$config = new Ini($this->_iniFileCircularConfig, null);
}

public function testErrorNoFile()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'Filename is not set');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Filename is not set');
$config = new Ini('','');
}

public function testErrorMultipleExensions()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'may not extend multiple sections');
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'may not extend multiple sections');
$config = new Ini($this->_iniFileMultipleInheritanceConfig, 'three');
zend::dump($config);
}

public function testErrorNoSectionFound()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'cannot be found');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'cannot be found');
$config = new Ini($this->_iniFileConfig,'notthere');
}

public function testErrorNoSectionFoundWhenMultipleSectionsProvided()
{
$this->setExpectedException('\\Zend\\Config\\Exception', 'cannot be found');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'cannot be found');
$config = new Ini($this->_iniFileConfig,array('all', 'notthere'));
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public function testZF3196_InvalidIniFile()
try {
$config = new Ini($this->_iniFileInvalid);
$this->fail('An expected Zend\Config\Exception has not been raised');
} catch (\Zend\Config\Exception $expected) {
} catch (\Zend\Config\Exception\RuntimeException $expected) {
$this->assertRegexp('/(Error parsing|syntax error, unexpected)/', $expected->getMessage());
}

Expand Down
6 changes: 3 additions & 3 deletions test/Writer/ArrayWriterTest.php
Expand Up @@ -50,21 +50,21 @@ public function tearDown()
public function testNoFilenameSet()
{
$writer = new ArrayWriter(array('config' => new Config(array())));
$this->setExpectedException('\\Zend\\Config\\Exception', 'No filename was set');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'No filename was set');
$writer->write();
}

public function testNoConfigSet()
{
$writer = new ArrayWriter(array('filename' => $this->_tempName));
$this->setExpectedException('\\Zend\\Config\\Exception', 'No config was set');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'No config was set');
$writer->write();
}

public function testFileNotWritable()
{
$writer = new ArrayWriter(array('config' => new Config(array()), 'filename' => '/../../../'));
$this->setExpectedException('\\Zend\\Config\\Exception', 'Could not write to file');
$this->setExpectedException('Zend\Config\Exception\RuntimeException', 'Could not write to file');
$writer->write();
}

Expand Down

0 comments on commit d50da4c

Please sign in to comment.