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

Clean up test names; add Lang & Exception tests

This commit is contained in:
J. King 2017-02-11 13:50:34 -05:00
parent 91274b9bf9
commit 849294d082
7 changed files with 96 additions and 56 deletions

View file

@ -29,14 +29,14 @@ class TestConf extends \PHPUnit\Framework\TestCase {
self::$vfs = null; self::$vfs = null;
} }
function testConstructor() { function testLoadDefaultValues() {
$this->assertInstanceOf(Conf::class, new Conf()); $this->assertInstanceOf(Conf::class, new Conf());
} }
/** /**
* @depends testConstructor * @depends testLoadDefaultValues
*/ */
function testImportArray() { function testImportFromArray() {
$arr = ['lang' => "xx"]; $arr = ['lang' => "xx"];
$conf = new Conf(); $conf = new Conf();
$conf->import($arr); $conf->import($arr);
@ -44,9 +44,9 @@ class TestConf extends \PHPUnit\Framework\TestCase {
} }
/** /**
* @depends testImportArray * @depends testImportFromArray
*/ */
function testImportFile() { function testImportFromFile() {
$conf = new Conf(); $conf = new Conf();
$conf->importFile(self::$path."confGood"); $conf->importFile(self::$path."confGood");
$this->assertEquals("xx", $conf->lang); $this->assertEquals("xx", $conf->lang);
@ -55,50 +55,50 @@ class TestConf extends \PHPUnit\Framework\TestCase {
} }
/** /**
* @depends testImportFile * @depends testImportFromFile
*/ */
function testImportFileMissing() { function testImportFromMissingFile() {
$this->assertException("fileMissing", "Conf"); $this->assertException("fileMissing", "Conf");
$conf = new Conf(self::$path."confMissing"); $conf = new Conf(self::$path."confMissing");
} }
/** /**
* @depends testImportFile * @depends testImportFromFile
*/ */
function testImportFileEmpty() { function testImportFromEmptyFile() {
$this->assertException("fileCorrupt", "Conf"); $this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confEmpty"); $conf = new Conf(self::$path."confEmpty");
} }
/** /**
* @depends testImportFile * @depends testImportFromFile
*/ */
function testImportFileUnreadable() { function testImportFromFileWithoutReadPermission() {
$this->assertException("fileUnreadable", "Conf"); $this->assertException("fileUnreadable", "Conf");
$conf = new Conf(self::$path."confUnreadable"); $conf = new Conf(self::$path."confUnreadable");
} }
/** /**
* @depends testImportFile * @depends testImportFromFile
*/ */
function testImportFileNotAnArray() { function testImportFromFileWhichIsNotAnArray() {
$this->assertException("fileCorrupt", "Conf"); $this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confNotArray"); $conf = new Conf(self::$path."confNotArray");
} }
/** /**
* @depends testImportFile * @depends testImportFromFile
*/ */
function testImportFileNotPhp() { function testImportFromFileWhichIsNotPhp() {
$this->assertException("fileCorrupt", "Conf"); $this->assertException("fileCorrupt", "Conf");
// this should not print the output of the non-PHP file // this should not print the output of the non-PHP file
$conf = new Conf(self::$path."confNotPHP"); $conf = new Conf(self::$path."confNotPHP");
} }
/** /**
* @depends testImportFile * @depends testImportFromFile
*/ */
function testImportFileCorrupt() { function testImportFromCorruptFile() {
$this->assertException("fileCorrupt", "Conf"); $this->assertException("fileCorrupt", "Conf");
// this should not print the output of the non-PHP file // this should not print the output of the non-PHP file
$conf = new Conf(self::$path."confCorrupt"); $conf = new Conf(self::$path."confCorrupt");

View file

@ -14,32 +14,57 @@ class TestException extends \PHPUnit\Framework\TestCase {
Lang::set(Lang::DEFAULT); Lang::set(Lang::DEFAULT);
} }
function testBasic() { function testBaseClass() {
$this->assertException("unknown"); $this->assertException("unknown");
throw new Exception("unknown"); throw new Exception("unknown");
} }
/** /**
* @depends testBasic * @depends testBaseClass
*/ */
function testPlain() { function testBaseClassWithoutMessage() {
$this->assertException("unknown"); $this->assertException("unknown");
throw new Exception(); throw new Exception();
} }
/** /**
* @depends testBasic * @depends testBaseClass
*/ */
function testNamespace() { function testDerivedClass() {
$this->assertException("fileMissing", "Lang"); $this->assertException("fileMissing", "Lang");
throw new Lang\Exception("fileMissing"); throw new Lang\Exception("fileMissing");
} }
/** /**
* @depends testNamespace * @depends testDerivedClass
*/ */
function testValues() { function testDerivedClassWithMessageParameters() {
$this->assertException("fileMissing", "Lang"); $this->assertException("fileMissing", "Lang");
throw new Lang\Exception("fileMissing", "en"); throw new Lang\Exception("fileMissing", "en");
} }
/**
* @depends testBaseClass
*/
function testBaseClassWithUnknownCode() {
$this->assertException("uncoded");
throw new Exception("testThisExceptionMessageDoesNotExist");
}
/**
* @depends testBaseClass
*/
function testBaseClassWithMissingMessage() {
$this->assertException("stringMissing", "Lang");
throw new Exception("invalid");
}
/**
* @depends testBaseClassWithUnknownCode
*/
function testDerivedClassWithMissingMessage() {
$this->assertException("uncoded");
throw new Lang\Exception("testThisExceptionMessageDoesNotExist");
}
} }

View file

@ -12,14 +12,14 @@ class TestLang extends \PHPUnit\Framework\TestCase {
static $files; static $files;
static $defaultPath; static $defaultPath;
function testList() { function testListLanguages() {
$this->assertCount(sizeof(self::$files), Lang::list("en")); $this->assertCount(sizeof(self::$files), Lang::list("en"));
} }
/** /**
* @depends testList * @depends testListLanguages
*/ */
function testSet() { function testSetLanguage() {
$this->assertEquals("en", Lang::set("en")); $this->assertEquals("en", Lang::set("en"));
$this->assertEquals("en_ca", Lang::set("en_ca")); $this->assertEquals("en_ca", Lang::set("en_ca"));
$this->assertEquals("de", Lang::set("de_ch")); $this->assertEquals("de", Lang::set("de_ch"));
@ -30,7 +30,7 @@ class TestLang extends \PHPUnit\Framework\TestCase {
} }
/** /**
* @depends testSet * @depends testSetLanguage
*/ */
function testLoadInternalStrings() { function testLoadInternalStrings() {
$this->assertEquals("", Lang::set("", true)); $this->assertEquals("", Lang::set("", true));
@ -40,7 +40,7 @@ class TestLang extends \PHPUnit\Framework\TestCase {
/** /**
* @depends testLoadInternalStrings * @depends testLoadInternalStrings
*/ */
function testLoadDefaultStrings() { function testLoadDefaultLanguage() {
$this->assertEquals(Lang::DEFAULT, Lang::set(Lang::DEFAULT, true)); $this->assertEquals(Lang::DEFAULT, Lang::set(Lang::DEFAULT, true));
$str = Lang::dump(); $str = Lang::dump();
$this->assertArrayHasKey('Exception.JKingWeb/NewsSync/Exception.uncoded', $str); $this->assertArrayHasKey('Exception.JKingWeb/NewsSync/Exception.uncoded', $str);
@ -48,9 +48,9 @@ class TestLang extends \PHPUnit\Framework\TestCase {
} }
/** /**
* @depends testLoadDefaultStrings * @depends testLoadDefaultLanguage
*/ */
function testLoadMultipleFiles() { function testLoadSupplementaryLanguage() {
Lang::set(Lang::DEFAULT, true); Lang::set(Lang::DEFAULT, true);
$this->assertEquals("ja", Lang::set("ja", true)); $this->assertEquals("ja", Lang::set("ja", true));
$str = Lang::dump(); $str = Lang::dump();

View file

@ -16,32 +16,37 @@ class TestLangErrors extends \PHPUnit\Framework\TestCase {
Lang::set("", true); Lang::set("", true);
} }
function testLoadFileEmpty() { function testLoadEmptyFile() {
$this->assertException("fileCorrupt", "Lang"); $this->assertException("fileCorrupt", "Lang");
Lang::set("fr_ca", true); Lang::set("fr_ca", true);
} }
function testLoadFileNotAnArray() { function testLoadFileWhichDoesNotReturnAnArray() {
$this->assertException("fileCorrupt", "Lang"); $this->assertException("fileCorrupt", "Lang");
Lang::set("it", true); Lang::set("it", true);
} }
function testLoadFileNotPhp() { function testLoadFileWhichIsNotPhp() {
$this->assertException("fileCorrupt", "Lang"); $this->assertException("fileCorrupt", "Lang");
Lang::set("ko", true); Lang::set("ko", true);
} }
function testLoadFileCorrupt() { function testLoadFileWhichIsCorrupt() {
$this->assertException("fileCorrupt", "Lang"); $this->assertException("fileCorrupt", "Lang");
Lang::set("zh", true); Lang::set("zh", true);
} }
function testLoadFileUnreadable() { function testLoadFileWithooutReadPermission() {
$this->assertException("fileUnreadable", "Lang"); $this->assertException("fileUnreadable", "Lang");
Lang::set("ru", true); Lang::set("ru", true);
} }
function testLoadDefaultMissing() { function testLoadSubtagOfMissingLanguage() {
$this->assertException("fileMissing", "Lang");
Lang::set("pt_br", true);
}
function testLoadMissingDefaultLanguage() {
// this should be the last test of the series // this should be the last test of the series
unlink(self::$path.Lang::DEFAULT.".php"); unlink(self::$path.Lang::DEFAULT.".php");
$this->assertException("defaultFileMissing", "Lang"); $this->assertException("defaultFileMissing", "Lang");

View file

@ -17,6 +17,19 @@ trait TestingHelpers {
$this->expectException($class); $this->expectException($class);
$this->expectExceptionCode($code); $this->expectExceptionCode($code);
} }
function assertExc(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;
}
echo $class."\n";
$this->expectException($class);
$this->expectExceptionCode($code);
}
} }
trait LanguageTestingHelpers { trait LanguageTestingHelpers {
@ -31,6 +44,7 @@ trait LanguageTestingHelpers {
'fr.php' => '<?php return ["Test.presentText" => "à l\'école des sorciers"];', 'fr.php' => '<?php return ["Test.presentText" => "à l\'école des sorciers"];',
'ja.php' => '<?php return ["Test.absentText" => "賢者の石"];', 'ja.php' => '<?php return ["Test.absentText" => "賢者の石"];',
'de.php' => '<?php return ["Test.presentText" => "und der Stein der Weisen"];', 'de.php' => '<?php return ["Test.presentText" => "und der Stein der Weisen"];',
'pt_br.php' => '<?php return ["Test.presentText" => "e a Pedra Filosofal"];',
'vi.php' => '<?php return [];', 'vi.php' => '<?php return [];',
// corrupt files // corrupt files
'it.php' => '<?php return 0;', 'it.php' => '<?php return 0;',

View file

@ -16,12 +16,12 @@ class TestLangComplex extends \PHPUnit\Framework\TestCase {
Lang::set(Lang::DEFAULT, true); Lang::set(Lang::DEFAULT, true);
} }
function testLoadLazy() { function testLazyLoad() {
Lang::set("ja"); Lang::set("ja");
$this->assertArrayNotHasKey('Test.absentText', Lang::dump()); $this->assertArrayNotHasKey('Test.absentText', Lang::dump());
} }
function testLoadCascade() { function testLoadCascadeOfFiles() {
Lang::set("ja", true); Lang::set("ja", true);
$this->assertEquals("de", Lang::set("de", true)); $this->assertEquals("de", Lang::set("de", true));
$str = Lang::dump(); $str = Lang::dump();
@ -30,54 +30,51 @@ class TestLangComplex extends \PHPUnit\Framework\TestCase {
} }
/** /**
* @depends testLoadCascade * @depends testLoadCascadeOfFiles
*/ */
function testLoadSubtag() { function testLoadSubtag() {
$this->assertEquals("en_ca", Lang::set("en_ca", true)); $this->assertEquals("en_ca", Lang::set("en_ca", true));
} }
/** function testFetchAMessage() {
* @depends testLoadSubtag
*/
function testMessage() {
Lang::set("de", true); Lang::set("de", true);
$this->assertEquals('und der Stein der Weisen', Lang::msg('Test.presentText')); $this->assertEquals('und der Stein der Weisen', Lang::msg('Test.presentText'));
} }
/** /**
* @depends testMessage * @depends testFetchAMessage
*/ */
function testMessageNumSingle() { function testFetchAMessageWithSingleNumericParameter() {
Lang::set("en_ca", true); Lang::set("en_ca", true);
$this->assertEquals('Default language file "en" missing', Lang::msg('Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing', Lang::DEFAULT)); $this->assertEquals('Default language file "en" missing', Lang::msg('Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing', Lang::DEFAULT));
} }
/** /**
* @depends testMessage * @depends testFetchAMessage
*/ */
function testMessageNumMulti() { function testFetchAMessageWithMultipleNumericParameters() {
Lang::set("en_ca", true); Lang::set("en_ca", true);
$this->assertEquals('Happy Rotter and the Philosopher\'s Stone', Lang::msg('Test.presentText', ['Happy Rotter', 'the Philosopher\'s Stone'])); $this->assertEquals('Happy Rotter and the Philosopher\'s Stone', Lang::msg('Test.presentText', ['Happy Rotter', 'the Philosopher\'s Stone']));
} }
/** /**
* @depends testMessage * @depends testFetchAMessage
*/ */
function testMessageNamed() { function testFetchAMessageWithNamedParameters() {
$this->assertEquals('Message string "Test.absentText" missing from all loaded language files (en)', Lang::msg('Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing', ['msgID' => 'Test.absentText', 'fileList' => 'en'])); $this->assertEquals('Message string "Test.absentText" missing from all loaded language files (en)', Lang::msg('Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing', ['msgID' => 'Test.absentText', 'fileList' => 'en']));
} }
/** /**
* @depends testMessage * @depends testFetchAMessage
*/ */
function testReloadDefaults() { function testReloadDefaultStrings() {
Lang::set("de", true); Lang::set("de", true);
Lang::set("en", true); Lang::set("en", true);
$this->assertEquals('and the Philosopher\'s Stone', Lang::msg('Test.presentText')); $this->assertEquals('and the Philosopher\'s Stone', Lang::msg('Test.presentText'));
} }
/** /**
* @depends testMessage * @depends testFetchAMessage
*/ */
function testReloadGeneralTagAfterSubtag() { function testReloadGeneralTagAfterSubtag() {
Lang::set("en", true); Lang::set("en", true);

View file

@ -6,6 +6,7 @@ class Exception extends \Exception {
const CODES = [ const CODES = [
"Exception.uncoded" => -1, "Exception.uncoded" => -1,
"Exception.invalid" => 1, // this exception MUST NOT have a message string defined
"Exception.unknown" => 10000, "Exception.unknown" => 10000,
"Lang/Exception.defaultFileMissing" => 10101, "Lang/Exception.defaultFileMissing" => 10101,
"Lang/Exception.fileMissing" => 10102, "Lang/Exception.fileMissing" => 10102,
@ -46,9 +47,7 @@ class Exception extends \Exception {
} else { } else {
$codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID"; $codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID";
if(!array_key_exists($codeID,self::CODES)) { if(!array_key_exists($codeID,self::CODES)) {
$code = -1; throw new self("uncoded");
$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 = "Exception.".str_replace("\\","/",get_called_class()).".$msgID";