From add5e3f1ea27a176fb493c09372c8375276904c5 Mon Sep 17 00:00:00 2001 From: David Schoenbauer Date: Thu, 1 Dec 2016 14:52:21 -0600 Subject: [PATCH 1/3] Updated Exception Handling and added the remove function --- src/DotNotation/ArrayDotNotation.php | 49 +++++++++-- src/DotNotation/Enum/ExceptionMessage.php | 39 +++++++++ .../Exception/PathNotArrayException.php | 43 ++++++++++ .../Exception/PathNotFoundException.php | 45 ++++++++++ .../Exception/TargetNotArrayException.php | 45 ++++++++++ tests/DotNotation/ArrayDotNotationTest.php | 84 ++++++++++++------- .../Exception/PathNotArrayExceptionTest.php | 64 ++++++++++++++ .../Exception/PathNotFoundExceptionTest.php | 64 ++++++++++++++ .../Exception/TargetNotArrayExceptionTest.php | 64 ++++++++++++++ tests/configuration.xml | 1 + 10 files changed, 463 insertions(+), 35 deletions(-) create mode 100644 src/DotNotation/Enum/ExceptionMessage.php create mode 100644 src/DotNotation/Exception/PathNotArrayException.php create mode 100644 src/DotNotation/Exception/PathNotFoundException.php create mode 100644 src/DotNotation/Exception/TargetNotArrayException.php create mode 100644 tests/DotNotation/Exception/PathNotArrayExceptionTest.php create mode 100644 tests/DotNotation/Exception/PathNotFoundExceptionTest.php create mode 100644 tests/DotNotation/Exception/TargetNotArrayExceptionTest.php diff --git a/src/DotNotation/ArrayDotNotation.php b/src/DotNotation/ArrayDotNotation.php index 8d447db..bacef1f 100644 --- a/src/DotNotation/ArrayDotNotation.php +++ b/src/DotNotation/ArrayDotNotation.php @@ -26,6 +26,9 @@ namespace DSchoenbauer\DotNotation; +use DSchoenbauer\DotNotation\Exception\PathNotArrayException; +use DSchoenbauer\DotNotation\Exception\PathNotFoundException; +use DSchoenbauer\DotNotation\Exception\TargetNotArrayException; use DSchoenbauer\DotNotation\Exception\UnexpectedValueException; /** @@ -122,7 +125,7 @@ protected function recursiveGet($data, $keys, $defaultValue) { * @param string $dotNotation dot notation representation of keys of where to set a value * @param mixed $value any value to be stored with in a key structure of dot notation * @return $this - * @throws UnexpectedValueException if a value in the dot notation path is not an array + * @throws PathNotArrayException if a value in the dot notation path is not an array */ public function set($dotNotation, $value) { $this->recursiveSet($this->_data, explode('.', $dotNotation), $value); @@ -137,7 +140,7 @@ public function set($dotNotation, $value) { * @param array $data data to be traversed * @param array $keys the remaining keys of focus for the data array * @param mixed $value the value to be placed at the final key - * @throws UnexpectedValueException if a value in the dot notation path is not an array + * @throws PathNotArrayException if a value in the dot notation path is not an array */ protected function recursiveSet(array &$data, array $keys, $value) { $key = array_shift($keys); @@ -146,8 +149,8 @@ protected function recursiveSet(array &$data, array $keys, $value) { } else { if (!array_key_exists($key, $data)) { $data[$key] = []; - }elseif (!is_array($data[$key])) { - throw new UnexpectedValueException("Array dot notation path key '$key' is not an array"); + } elseif (!is_array($data[$key])) { + throw new Exception\PathNotArrayException($key); } $this->recursiveSet($data[$key], $keys, $value); } @@ -156,19 +159,53 @@ protected function recursiveSet(array &$data, array $keys, $value) { /** * Merges two arrays together over writing existing values with new values, while adding new array structure to the data * + * @since 1.1.0 * @param string $dotNotation dot notation representation of keys of where to set a value * @param array $value array to be merged with an existing array * @return $this * @throws UnexpectedValueException if a value in the dot notation path is not an array - * @throws UnexpectedValueException if the value in the dot notation target is not an array + * @throws TargetNotArrayException if the value in the dot notation target is not an array */ public function merge($dotNotation, array $value) { $target = $this->get($dotNotation, []); if (!is_array($target)) { - throw new UnexpectedValueException('Array dot notation target key value is not an array. Merge is not possible'); + throw new Exception\TargetNotArrayException($dotNotation); } $this->set($dotNotation, array_merge_recursive($target, $value)); return $this; } + /** + * Removes data from the array + * + * @since 1.1.0 + * @param type $dotNotation + * @return $this + */ + public function remove($dotNotation) { + $this->recursiveRemove($this->_data, explode('.', $dotNotation), $dotNotation); + return $this; + } + + /** + * Transverses the keys array until it reaches the appropriate key in the data array and then deletes the value from the key. + * + * @since 1.1.0 + * @param array $data data to be traversed + * @param array $keys the remaining keys of focus for the data array + * @throws UnexpectedValueException if a value in the dot notation path is not an array + */ + protected function recursiveRemove(array &$data, array $keys) { + $key = array_shift($keys); + if (!array_key_exists($key, $data)) { + throw new PathNotFoundException($key); + } elseif ($key && count($keys) == 0) { //Last Key + unset($data[$key]); + } elseif (!is_array($data[$key])) { + throw new PathNotArrayException($key); + } else { + $this->recursiveRemove($data[$key], $keys); + } + } + } diff --git a/src/DotNotation/Enum/ExceptionMessage.php b/src/DotNotation/Enum/ExceptionMessage.php new file mode 100644 index 0000000..112030e --- /dev/null +++ b/src/DotNotation/Enum/ExceptionMessage.php @@ -0,0 +1,39 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Enum; + +/** + * A collection of messages used inside of exceptions + * + * @author David Schoenbauer + */ +class ExceptionMessage { + + const PATH_NOT_FOUND = "Path key: '%s' is not found"; + const PATH_NOT_ARRAY = "Array dot notation path key '%s' is not an array"; + const TARGET_NOT_ARRAY = "Array dot notation target key '%s' value is not an array."; +} diff --git a/src/DotNotation/Exception/PathNotArrayException.php b/src/DotNotation/Exception/PathNotArrayException.php new file mode 100644 index 0000000..27233e1 --- /dev/null +++ b/src/DotNotation/Exception/PathNotArrayException.php @@ -0,0 +1,43 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Exception; + +use DSchoenbauer\DotNotation\Enum\ExceptionMessage; + +/** + * Description of PathNotArrayException + * + * @author David Schoenbauer + */ +class PathNotArrayException extends UnexpectedValueException implements ExceptionInterface{ + public function __construct($key = "", $message = "", $code = 0, $previous = null) { + if ($message == "" && $key !== "") { + $message = sprintf(ExceptionMessage::PATH_NOT_ARRAY, $key); + } + parent::__construct($message, $code, $previous); + } +} diff --git a/src/DotNotation/Exception/PathNotFoundException.php b/src/DotNotation/Exception/PathNotFoundException.php new file mode 100644 index 0000000..2c767c7 --- /dev/null +++ b/src/DotNotation/Exception/PathNotFoundException.php @@ -0,0 +1,45 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Exception; + +use DSchoenbauer\DotNotation\Enum\ExceptionMessage; + +/** + * Description of PathNotFoundException + * + * @author David Schoenbauer + */ +class PathNotFoundException extends UnexpectedValueException implements ExceptionInterface { + + public function __construct($key = "", $message = "", $code = 0, $previous = null) { + if ($message == "" && $key !== "") { + $message = sprintf(ExceptionMessage::PATH_NOT_FOUND, $key); + } + parent::__construct($message, $code, $previous); + } + +} diff --git a/src/DotNotation/Exception/TargetNotArrayException.php b/src/DotNotation/Exception/TargetNotArrayException.php new file mode 100644 index 0000000..2c262a9 --- /dev/null +++ b/src/DotNotation/Exception/TargetNotArrayException.php @@ -0,0 +1,45 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Exception; + +use DSchoenbauer\DotNotation\Enum\ExceptionMessage; + +/** + * Description of TargetNotAnArrayException + * + * @author David Schoenbauer + */ +class TargetNotArrayException extends UnexpectedValueException implements ExceptionInterface { + + public function __construct($key = "", $message = "", $code = 0, $previous = null) { + if ($message == "" && $key !== "") { + $message = sprintf(ExceptionMessage::TARGET_NOT_ARRAY, $key); + } + parent::__construct($message, $code, $previous); + } + +} diff --git a/tests/DotNotation/ArrayDotNotationTest.php b/tests/DotNotation/ArrayDotNotationTest.php index 5ef2ac9..ca0aaf3 100644 --- a/tests/DotNotation/ArrayDotNotationTest.php +++ b/tests/DotNotation/ArrayDotNotationTest.php @@ -60,15 +60,16 @@ public function testSetDeepLevelNewValue() { $this->assertEquals('newValue', $this->_object->set('LevelA.LevelB.LevelC.levelD', 'newValue')->get('LevelA.LevelB.LevelC.levelD')); $this->assertEquals('someValue2', $this->_object->get('level1.level2'), "existing value compromised"); } - + /** * @expectedException UnexpectedValueException * @expectedExceptionMessage Array dot notation path key 'c' is not an array */ public function testSetNotExistArrayButString() { - $data = ['a'=>['b'=>['c'=>'cValue']]]; - $newData = ['e'=>'dValue']; - $this->_object->setData($data)->set('a.b.c.d',$newData)->getData(); } + $data = ['a' => ['b' => ['c' => 'cValue']]]; + $newData = ['e' => 'dValue']; + $this->_object->setData($data)->set('a.b.c.d', $newData)->getData(); + } public function testSetInitialLevelExistingValue() { $this->assertEquals('levelB', $this->_object->get('levelB')); @@ -87,39 +88,64 @@ public function testSetDataTypeConversion() { $this->assertEquals('newValue', $this->_object->set('levelA', 'newValue')->get('levelA')); $this->assertEquals('someValue2', $this->_object->get('level1.level2'), "existing value compromised"); } - - public function testMergeSimpleArray(){ - $data = ['a'=>['b'=>['c'=>'cValue']]]; - $merge = ['d'=>'dValue']; - $this->assertEquals('dValue',$this->_object->setData($data)->merge('a.b',$merge)->get('a.b.d')); - $this->assertEquals('cValue',$this->_object->get('a.b.c')); + + public function testMergeSimpleArray() { + $data = ['a' => ['b' => ['c' => 'cValue']]]; + $merge = ['d' => 'dValue']; + $this->assertEquals('dValue', $this->_object->setData($data)->merge('a.b', $merge)->get('a.b.d')); + $this->assertEquals('cValue', $this->_object->get('a.b.c')); } - + /** * @expectedException UnexpectedValueException - * @expectedExceptionMessage Array dot notation target key value is not an array. Merge is not possible + * @expectedExceptionMessage Array dot notation target key 'a.b.c' value is not an array. */ - public function testMergeNotAnArray(){ - $data = ['a'=>['b'=>['c'=>'cValue']]]; - $merge = ['d'=>'dValue']; - $this->_object->setData($data)->merge('a.b.c',$merge); + public function testMergeNotAnArray() { + $data = ['a' => ['b' => ['c' => 'cValue']]]; + $merge = ['d' => 'dValue']; + $this->_object->setData($data)->merge('a.b.c', $merge); } - + /** * @expectedException UnexpectedValueException * @expectedExceptionMessage Array dot notation path key 'c' is not an array - */ - public function testMergeNotPresentKeyString(){ - $data = ['a'=>['b'=>['c'=>'cValue']]]; - $merge = ['e'=>'dValue']; - $this->_object->setData($data)->merge('a.b.c.d',$merge)->getData(); - } - - public function testMergeNotPresentKey(){ - $data = ['a'=>['b'=>['c'=>[]]]]; - $merge = ['e'=>'dValue']; - $result = ['a'=>['b'=>['c'=>['d'=>['e'=>'dValue']]]]]; - $this->assertEquals($result, $this->_object->setData($data)->merge('a.b.c.d',$merge)->getData()); + */ + public function testMergeNotPresentKeyString() { + $data = ['a' => ['b' => ['c' => 'cValue']]]; + $merge = ['e' => 'dValue']; + $this->_object->setData($data)->merge('a.b.c.d', $merge)->getData(); + } + + public function testMergeNotPresentKey() { + $data = ['a' => ['b' => ['c' => []]]]; + $merge = ['e' => 'dValue']; + $result = ['a' => ['b' => ['c' => ['d' => ['e' => 'dValue']]]]]; + $this->assertEquals($result, $this->_object->setData($data)->merge('a.b.c.d', $merge)->getData()); + } + + public function testRemove() { + $data = [ + 'levelA' => [], + 'levelB' => 'levelB', + 'level1' => [ + 'level2' => 'someValue2' + ] + ]; + $this->assertEquals($data, $this->_object->remove('levelA.levelB')->getData()); + } + + /** + * @expectedException \DSchoenbauer\DotNotation\Exception\PathNotFoundException + */ + public function testRemovePathNotFound() { + $this->_object->remove('levelA.levelC'); + } + + /** + * @expectedException \DSchoenbauer\DotNotation\Exception\PathNotFoundException + */ + public function testRemovePathNotFoundShort() { + $this->_object->remove('level0'); } } diff --git a/tests/DotNotation/Exception/PathNotArrayExceptionTest.php b/tests/DotNotation/Exception/PathNotArrayExceptionTest.php new file mode 100644 index 0000000..2a12089 --- /dev/null +++ b/tests/DotNotation/Exception/PathNotArrayExceptionTest.php @@ -0,0 +1,64 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Exception; + +use DSchoenbauer\DotNotation\Enum\ExceptionMessage; +use PHPUnit_Framework_TestCase; + +/** + * Description of PathNotFoundExceptionTest + * + * @author David Schoenbauer + */ +class PathNotArrayExceptionTest extends PHPUnit_Framework_TestCase { + + private $_object; + + protected function setUp() { + $this->_object = new PathNotArrayException(); + } + + public function testHasExceptionInterface() { + $this->assertInstanceOf(ExceptionInterface::class, $this->_object); + } + + public function testIsUnexpectedValue() { + $this->assertInstanceOf(UnexpectedValueException::class, $this->_object); + } + + public function testKeyMessage() { + $exc = new PathNotArrayException("Key"); + $message = sprintf(ExceptionMessage::PATH_NOT_ARRAY, "Key"); + $this->assertEquals($message, $exc->getMessage()); + } + + public function testCustomMessage() { + $exc = new PathNotFoundException("","Full Message"); + $this->assertEquals("Full Message", $exc->getMessage()); + } + +} diff --git a/tests/DotNotation/Exception/PathNotFoundExceptionTest.php b/tests/DotNotation/Exception/PathNotFoundExceptionTest.php new file mode 100644 index 0000000..ce2e59d --- /dev/null +++ b/tests/DotNotation/Exception/PathNotFoundExceptionTest.php @@ -0,0 +1,64 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Exception; + +use DSchoenbauer\DotNotation\Enum\ExceptionMessage; +use PHPUnit_Framework_TestCase; + +/** + * Description of PathNotFoundExceptionTest + * + * @author David Schoenbauer + */ +class PathNotFoundExceptionTest extends PHPUnit_Framework_TestCase { + + private $_object; + + protected function setUp() { + $this->_object = new PathNotFoundException(); + } + + public function testHasExceptionInterface() { + $this->assertInstanceOf(ExceptionInterface::class, $this->_object); + } + + public function testIsUnexpectedValue() { + $this->assertInstanceOf(UnexpectedValueException::class, $this->_object); + } + + public function testKeyMessage() { + $exc = new PathNotFoundException("Key"); + $message = sprintf(ExceptionMessage::PATH_NOT_FOUND, "Key"); + $this->assertEquals($message, $exc->getMessage()); + } + + public function testCustomMessage() { + $exc = new PathNotFoundException("","Full Message"); + $this->assertEquals("Full Message", $exc->getMessage()); + } + +} diff --git a/tests/DotNotation/Exception/TargetNotArrayExceptionTest.php b/tests/DotNotation/Exception/TargetNotArrayExceptionTest.php new file mode 100644 index 0000000..44276f7 --- /dev/null +++ b/tests/DotNotation/Exception/TargetNotArrayExceptionTest.php @@ -0,0 +1,64 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace DSchoenbauer\DotNotation\Exception; + +use DSchoenbauer\DotNotation\Enum\ExceptionMessage; +use PHPUnit_Framework_TestCase; + +/** + * Description of PathNotFoundExceptionTest + * + * @author David Schoenbauer + */ +class TargetNotArrayExceptionTest extends PHPUnit_Framework_TestCase { + + private $_object; + + protected function setUp() { + $this->_object = new TargetNotArrayException(); + } + + public function testHasExceptionInterface() { + $this->assertInstanceOf(ExceptionInterface::class, $this->_object); + } + + public function testIsUnexpectedValue() { + $this->assertInstanceOf(UnexpectedValueException::class, $this->_object); + } + + public function testKeyMessage() { + $exc = new TargetNotArrayException("Key"); + $message = sprintf(ExceptionMessage::TARGET_NOT_ARRAY, "Key"); + $this->assertEquals($message, $exc->getMessage()); + } + + public function testCustomMessage() { + $exc = new PathNotFoundException("","Full Message"); + $this->assertEquals("Full Message", $exc->getMessage()); + } + +} diff --git a/tests/configuration.xml b/tests/configuration.xml index fab1006..c0a1753 100644 --- a/tests/configuration.xml +++ b/tests/configuration.xml @@ -49,6 +49,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ../src ../src + ../src/DotNotation/Enum ./vendor ./tests From 08d724d9600e287b4fdbe19fe03fb237a04c5452 Mon Sep 17 00:00:00 2001 From: David Schoenbauer Date: Thu, 1 Dec 2016 15:00:32 -0600 Subject: [PATCH 2/3] Added missing test --- tests/DotNotation/ArrayDotNotationTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/DotNotation/ArrayDotNotationTest.php b/tests/DotNotation/ArrayDotNotationTest.php index ca0aaf3..2818593 100644 --- a/tests/DotNotation/ArrayDotNotationTest.php +++ b/tests/DotNotation/ArrayDotNotationTest.php @@ -147,5 +147,11 @@ public function testRemovePathNotFound() { public function testRemovePathNotFoundShort() { $this->_object->remove('level0'); } + /** + * @expectedException \DSchoenbauer\DotNotation\Exception\PathNotArrayException + */ + public function testRemovePathNotArray() { + $this->_object->remove('levelA.levelB.levelD'); + } } From 6996db680480aa514c4579d0c72c4e6dfb1ba955 Mon Sep 17 00:00:00 2001 From: David Schoenbauer Date: Thu, 1 Dec 2016 15:18:01 -0600 Subject: [PATCH 3/3] updated documentation --- docs/404.html | 9 + ...oenbauer.DotNotation.ArrayDotNotation.html | 121 ++++++- ...uer.DotNotation.Enum.ExceptionMessage.html | 224 ++++++++++++ ...Notation.Exception.ExceptionInterface.html | 11 + ...on.Exception.InvalidArgumentException.html | 8 + ...ation.Exception.PathNotArrayException.html | 253 +++++++++++++ ...ation.Exception.PathNotFoundException.html | 253 +++++++++++++ ...ion.Exception.TargetNotArrayException.html | 253 +++++++++++++ ...on.Exception.UnexpectedValueException.html | 14 + docs/elementlist.js | 2 +- docs/index.html | 14 +- ...mespace-DSchoenbauer.DotNotation.Enum.html | 117 ++++++ ...ce-DSchoenbauer.DotNotation.Exception.html | 22 +- docs/namespace-DSchoenbauer.DotNotation.html | 10 +- docs/namespace-DSchoenbauer.html | 7 +- ...oenbauer.DotNotation.ArrayDotNotation.html | 342 ++++++++++-------- ...uer.DotNotation.Enum.ExceptionMessage.html | 153 ++++++++ ...Notation.Exception.ExceptionInterface.html | 11 +- ...on.Exception.InvalidArgumentException.html | 11 +- ...ation.Exception.PathNotArrayException.html | 157 ++++++++ ...ation.Exception.PathNotFoundException.html | 159 ++++++++ ...ion.Exception.TargetNotArrayException.html | 159 ++++++++ ...on.Exception.UnexpectedValueException.html | 11 +- src/DotNotation/ArrayDotNotation.php | 2 +- 24 files changed, 2154 insertions(+), 169 deletions(-) create mode 100644 docs/class-DSchoenbauer.DotNotation.Enum.ExceptionMessage.html create mode 100644 docs/class-DSchoenbauer.DotNotation.Exception.PathNotArrayException.html create mode 100644 docs/class-DSchoenbauer.DotNotation.Exception.PathNotFoundException.html create mode 100644 docs/class-DSchoenbauer.DotNotation.Exception.TargetNotArrayException.html create mode 100644 docs/namespace-DSchoenbauer.DotNotation.Enum.html create mode 100644 docs/source-class-DSchoenbauer.DotNotation.Enum.ExceptionMessage.html create mode 100644 docs/source-class-DSchoenbauer.DotNotation.Exception.PathNotArrayException.html create mode 100644 docs/source-class-DSchoenbauer.DotNotation.Exception.PathNotFoundException.html create mode 100644 docs/source-class-DSchoenbauer.DotNotation.Exception.TargetNotArrayException.html diff --git a/docs/404.html b/docs/404.html index d2a9a8b..490c40e 100644 --- a/docs/404.html +++ b/docs/404.html @@ -31,6 +31,11 @@

Namespaces