From f16e490141300003f4259b77ce76932dcb949e1b Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 5 Feb 2017 19:00:57 -0500 Subject: [PATCH] First set of unit tests for Conf.php --- .gitignore | 1 + locale/en.php | 2 + tests/TestConf.php | 97 ++++++++++++++++++++++++++ tests/bootstrap.php | 31 ++++++++ tests/phpunit.xml | 18 +++++ vendor/JKingWeb/NewsSync/Conf.php | 17 +++-- vendor/JKingWeb/NewsSync/Exception.php | 13 ++-- vendor/JKingWeb/NewsSync/Lang.php | 13 ++-- 8 files changed, 176 insertions(+), 16 deletions(-) create mode 100644 tests/TestConf.php create mode 100644 tests/bootstrap.php create mode 100644 tests/phpunit.xml diff --git a/.gitignore b/.gitignore index 4ea01fca..118ab2a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ #dependencies vendor/simplepie/* vendor/JKingWeb/DrUUID/* +vendor/org/bovigo/vfs/* #temp files cache/* diff --git a/locale/en.php b/locale/en.php index 16d74813..b1678365 100644 --- a/locale/en.php +++ b/locale/en.php @@ -1,5 +1,7 @@ 'The specified exception symbol {0} has no code specified in Exception.php', + 'Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing' => 'Default language file "{0}" missing', 'Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing' => 'Language file "{0}" is not available', 'Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable' => 'Insufficient permissions to read language file "{0}"', diff --git a/tests/TestConf.php b/tests/TestConf.php new file mode 100644 index 00000000..8f3783ef --- /dev/null +++ b/tests/TestConf.php @@ -0,0 +1,97 @@ +url(); + foreach(["confUnreadable","confGood", "confCorrupt", "confNotArray"] as $file) { + touch($vfs."/".$file); + } + chmod($vfs."/confUnreadable", 0000); + $validConf = << "xx" +); +VALID_CONFIGURATION_FILE; + file_put_contents($vfs."/confGood",$validConf); + file_put_contents($vfs."/confNotArray", "assertInstanceOf(Conf::class, new Conf()); + } + + /** + * @depends testConstruct + */ + function testImportArray() { + $arr = ['lang' => "xx"]; + $conf = new Conf(); + $conf->import($arr); + $this->assertEquals("xx", $conf->lang); + } + + /** + * @depends testImportArray + */ + function testImportFile() { + $conf = new Conf(); + $conf->importFile(self::$vfs."/confGood"); + $this->assertEquals("xx", $conf->lang); + $conf = new Conf(self::$vfs."/confGood"); + $this->assertEquals("xx", $conf->lang); + } + + /** + * @depends testImportFile + */ + function testImportFileMissing() { + $this->assertException("fileMissing", "Conf"); + $conf = new Conf(self::$vfs."/confMissing"); + } + + /** + * @depends testImportFile + */ + function testImportFileUnreadable() { + $this->assertException("fileUnreadable", "Conf"); + $conf = new Conf(self::$vfs."/confUnreadable"); + } + + /** + * @depends testImportFile + */ + function testImportFileNotAnArray() { + $this->assertException("fileCorrupt", "Conf"); + $conf = new Conf(self::$vfs."/confNotArray"); + } + + /** + * @depends testImportFile + */ + function testImportFileNotPHP() { + $this->assertException("fileCorrupt", "Conf"); + // this should not print the output of the non-PHP file + $conf = new Conf(self::$vfs."/confNotPHP"); + } + + /** + * @depends testImportFile + */ + function testImportFileCorrupt() { + $this->assertException("fileCorrupt", "Conf"); + // this should not print the output of the non-PHP file + $conf = new Conf(self::$vfs."/confCorrupt"); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..d4d768c2 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,31 @@ +expectException($class); + $this->expectExceptionCode($code); + } +} + +ignore_user_abort(true); \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 00000000..4e40ffef --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,18 @@ + + + + + TestConf.php + + + + + \ No newline at end of file diff --git a/vendor/JKingWeb/NewsSync/Conf.php b/vendor/JKingWeb/NewsSync/Conf.php index 4cb6b72d..382324d0 100644 --- a/vendor/JKingWeb/NewsSync/Conf.php +++ b/vendor/JKingWeb/NewsSync/Conf.php @@ -35,16 +35,23 @@ class Conf { } public function importFile(string $file): self { - if(!file_exists($file)) throw new Conf\Exception("fileMissing"); - if(!is_readable($file)) throw new Conf\Exception("fileUnreadable"); - $arr = (@include $file); - if(!is_array($arr)) throw new Conf\Exception("fileCorrupt"); + if(!file_exists($file)) throw new Conf\Exception("fileMissing", $file); + if(!is_readable($file)) throw new Conf\Exception("fileUnreadable", $file); + try { + ob_start(); + $arr = (@include $file); + } catch(\Throwable $e) { + $arr = null; + } finally { + ob_end_clean(); + } + if(!is_array($arr)) throw new Conf\Exception("fileCorrupt", $file); return $this->import($arr); } public function import(array $arr): self { foreach($arr as $key => $value) { - $this->$$key = $value; + $this->$key = $value; } return $this; } diff --git a/vendor/JKingWeb/NewsSync/Exception.php b/vendor/JKingWeb/NewsSync/Exception.php index 8d946d99..c85c32cd 100644 --- a/vendor/JKingWeb/NewsSync/Exception.php +++ b/vendor/JKingWeb/NewsSync/Exception.php @@ -5,7 +5,8 @@ namespace JKingWeb\NewsSync; class Exception extends \Exception { const CODES = [ - "Exception.Misc" => 10000, + "Exception.uncoded" => -1, + "Exception.misc" => 10000, "Lang/Exception.defaultFileMissing" => 10101, "Lang/Exception.fileMissing" => 10102, "Lang/Exception.fileUnreadable" => 10103, @@ -43,14 +44,16 @@ class Exception extends \Exception { $msg = ""; $code = 0; } else { - $msg = "Exception.".str_replace("\\","/",get_called_class()).".$msgID"; - $msg = Lang::msg($msg, $vars); - $codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())); + $codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID"; if(!array_key_exists($codeID,self::CODES)) { - $code = 0; + $code = -1; + $msg = "Exception.".str_replace("\\","/",__CLASS__).".uncoded"; + $vars = $msgID; } else { $code = self::CODES[$codeID]; + $msg = "Exception.".str_replace("\\","/",get_called_class()).".$msgID"; } + $msg = Lang::msg($msg, $vars); } parent::__construct($msg, $code, $e); } diff --git a/vendor/JKingWeb/NewsSync/Lang.php b/vendor/JKingWeb/NewsSync/Lang.php index 0d8ad6ed..7fbb5cf2 100644 --- a/vendor/JKingWeb/NewsSync/Lang.php +++ b/vendor/JKingWeb/NewsSync/Lang.php @@ -6,12 +6,13 @@ class Lang { const PATH = BASE."locale".DIRECTORY_SEPARATOR; const DEFAULT = "en"; const REQUIRED = [ - "Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing" => "Default language file \"{0}\" missing", - "Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing" => "Language file \"{0}\" is not available", - "Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable" => "Insufficient permissions to read language file \"{0}\"", - "Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt" => "Language file \"{0}\" is corrupt or does not conform to expected format", - "Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing" => "Message string \"{msgID}\" missing from all loaded language files ({fileList})", - "Exception.JKingWeb/NewsSync/Lang/Exception.stringInvalid" => "Message string \"{msgID}\" is not a valid ICU message string (language files loaded: {fileList})", + 'Exception.JKingWeb/NewsSync/Exception.uncoded' => 'The specified exception symbol {0} has no code specified in Exception.php', + 'Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing' => 'Default language file "{0}" missing', + 'Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing' => 'Language file "{0}" is not available', + 'Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable' => 'Insufficient permissions to read language file "{0}"', + 'Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt' => 'Language file "{0}" is corrupt or does not conform to expected format', + 'Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing' => 'Message string "{msgID}" missing from all loaded language files ({fileList})', + 'Exception.JKingWeb/NewsSync/Lang/Exception.stringInvalid' => 'Message string "{msgID}" is not a valid ICU message string (language files loaded: {fileList})', ]; static protected $requirementsMet = false;