1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

First set of unit tests for Conf.php

This commit is contained in:
J. King 2017-02-05 19:00:57 -05:00
parent 8f77cbba1e
commit f16e490141
8 changed files with 176 additions and 16 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
#dependencies #dependencies
vendor/simplepie/* vendor/simplepie/*
vendor/JKingWeb/DrUUID/* vendor/JKingWeb/DrUUID/*
vendor/org/bovigo/vfs/*
#temp files #temp files
cache/* cache/*

View file

@ -1,5 +1,7 @@
<?php <?php
return [ return [
'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.defaultFileMissing' => 'Default language file "{0}" missing',
'Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing' => 'Language file "{0}" is not available', '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.fileUnreadable' => 'Insufficient permissions to read language file "{0}"',

97
tests/TestConf.php Normal file
View file

@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
use \org\bovigo\vfs\vfsStream;
class TestConf extends \PHPUnit\Framework\TestCase {
use TestingHelpers;
static $vfs;
static function setUpBeforeClass() {
$vfs = vfsStream::setup()->url();
foreach(["confUnreadable","confGood", "confCorrupt", "confNotArray"] as $file) {
touch($vfs."/".$file);
}
chmod($vfs."/confUnreadable", 0000);
$validConf = <<<VALID_CONFIGURATION_FILE
<?php
return Array(
"lang" => "xx"
);
VALID_CONFIGURATION_FILE;
file_put_contents($vfs."/confGood",$validConf);
file_put_contents($vfs."/confNotArray", "<?php return 0;");
file_put_contents($vfs."/confCorrupt", "<?php return 0");
file_put_contents($vfs."/confNotPHP", "DEAD BEEF");
self::$vfs = $vfs;
}
function testConstruct() {
$this->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");
}
}

31
tests/bootstrap.php Normal file
View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
const BASE = __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR;
const NS_BASE = __NAMESPACE__."\\";
spl_autoload_register(function ($class) {
if($class=="SimplePie") return;
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class);
$file = BASE."vendor".DIRECTORY_SEPARATOR.$file.".php";
if (file_exists($file)) {
require_once $file;
}
});
trait TestingHelpers {
function assertException(string $msg, string $prefix = "", string $type = "Exception") {
$class = NS_BASE . ($prefix !== "" ? str_replace("/", "\\", $prefix) . "\\" : "") . $type;
$msgID = ($prefix !== "" ? $prefix . "/" : "") . $type. ".$msg";
if(array_key_exists($msgID, Exception::CODES)) {
$code = Exception::CODES[$msgID];
} else {
$code = 0;
}
$this->expectException($class);
$this->expectExceptionCode($code);
}
}
ignore_user_abort(true);

18
tests/phpunit.xml Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0"?>
<phpunit
colors="true"
bootstrap="bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true">
<testsuite name="Base">
<file>TestConf.php</file>
</testsuite>
</phpunit>

View file

@ -35,16 +35,23 @@ class Conf {
} }
public function importFile(string $file): self { public function importFile(string $file): self {
if(!file_exists($file)) throw new Conf\Exception("fileMissing"); if(!file_exists($file)) throw new Conf\Exception("fileMissing", $file);
if(!is_readable($file)) throw new Conf\Exception("fileUnreadable"); if(!is_readable($file)) throw new Conf\Exception("fileUnreadable", $file);
$arr = (@include $file); try {
if(!is_array($arr)) throw new Conf\Exception("fileCorrupt"); 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); return $this->import($arr);
} }
public function import(array $arr): self { public function import(array $arr): self {
foreach($arr as $key => $value) { foreach($arr as $key => $value) {
$this->$$key = $value; $this->$key = $value;
} }
return $this; return $this;
} }

View file

@ -5,7 +5,8 @@ namespace JKingWeb\NewsSync;
class Exception extends \Exception { class Exception extends \Exception {
const CODES = [ const CODES = [
"Exception.Misc" => 10000, "Exception.uncoded" => -1,
"Exception.misc" => 10000,
"Lang/Exception.defaultFileMissing" => 10101, "Lang/Exception.defaultFileMissing" => 10101,
"Lang/Exception.fileMissing" => 10102, "Lang/Exception.fileMissing" => 10102,
"Lang/Exception.fileUnreadable" => 10103, "Lang/Exception.fileUnreadable" => 10103,
@ -43,14 +44,16 @@ class Exception extends \Exception {
$msg = ""; $msg = "";
$code = 0; $code = 0;
} else { } else {
$msg = "Exception.".str_replace("\\","/",get_called_class()).".$msgID"; $codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID";
$msg = Lang::msg($msg, $vars);
$codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class()));
if(!array_key_exists($codeID,self::CODES)) { if(!array_key_exists($codeID,self::CODES)) {
$code = 0; $code = -1;
$msg = "Exception.".str_replace("\\","/",__CLASS__).".uncoded";
$vars = $msgID;
} else { } else {
$code = self::CODES[$codeID]; $code = self::CODES[$codeID];
$msg = "Exception.".str_replace("\\","/",get_called_class()).".$msgID";
} }
$msg = Lang::msg($msg, $vars);
} }
parent::__construct($msg, $code, $e); parent::__construct($msg, $code, $e);
} }

View file

@ -6,12 +6,13 @@ class Lang {
const PATH = BASE."locale".DIRECTORY_SEPARATOR; const PATH = BASE."locale".DIRECTORY_SEPARATOR;
const DEFAULT = "en"; const DEFAULT = "en";
const REQUIRED = [ const REQUIRED = [
"Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing" => "Default language file \"{0}\" missing", 'Exception.JKingWeb/NewsSync/Exception.uncoded' => 'The specified exception symbol {0} has no code specified in Exception.php',
"Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing" => "Language file \"{0}\" is not available", 'Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing' => 'Default language file "{0}" missing',
"Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable" => "Insufficient permissions to read language file \"{0}\"", 'Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing' => 'Language file "{0}" is not available',
"Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt" => "Language file \"{0}\" is corrupt or does not conform to expected format", 'Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable' => 'Insufficient permissions to read language file "{0}"',
"Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing" => "Message string \"{msgID}\" missing from all loaded language files ({fileList})", 'Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt' => 'Language file "{0}" is corrupt or does not conform to expected format',
"Exception.JKingWeb/NewsSync/Lang/Exception.stringInvalid" => "Message string \"{msgID}\" is not a valid ICU message string (language files loaded: {fileList})", '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; static protected $requirementsMet = false;