Skip to content

Commit

Permalink
Clean up test names; add Lang & Exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JKingweb committed Feb 11, 2017
1 parent 91274b9 commit 849294d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 56 deletions.
34 changes: 17 additions & 17 deletions tests/TestConf.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ static function tearDownAfterClass() {
self::$vfs = null;
}

function testConstructor() {
function testLoadDefaultValues() {
$this->assertInstanceOf(Conf::class, new Conf());
}

/**
* @depends testConstructor
* @depends testLoadDefaultValues
*/
function testImportArray() {
function testImportFromArray() {
$arr = ['lang' => "xx"];
$conf = new Conf();
$conf->import($arr);
$this->assertEquals("xx", $conf->lang);
}

/**
* @depends testImportArray
* @depends testImportFromArray
*/
function testImportFile() {
function testImportFromFile() {
$conf = new Conf();
$conf->importFile(self::$path."confGood");
$this->assertEquals("xx", $conf->lang);
Expand All @@ -55,50 +55,50 @@ function testImportFile() {
}

/**
* @depends testImportFile
* @depends testImportFromFile
*/
function testImportFileMissing() {
function testImportFromMissingFile() {
$this->assertException("fileMissing", "Conf");
$conf = new Conf(self::$path."confMissing");
}

/**
* @depends testImportFile
* @depends testImportFromFile
*/
function testImportFileEmpty() {
function testImportFromEmptyFile() {
$this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confEmpty");
}

/**
* @depends testImportFile
* @depends testImportFromFile
*/
function testImportFileUnreadable() {
function testImportFromFileWithoutReadPermission() {
$this->assertException("fileUnreadable", "Conf");
$conf = new Conf(self::$path."confUnreadable");
}

/**
* @depends testImportFile
* @depends testImportFromFile
*/
function testImportFileNotAnArray() {
function testImportFromFileWhichIsNotAnArray() {
$this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confNotArray");
}

/**
* @depends testImportFile
* @depends testImportFromFile
*/
function testImportFileNotPhp() {
function testImportFromFileWhichIsNotPhp() {
$this->assertException("fileCorrupt", "Conf");
// this should not print the output of the non-PHP file
$conf = new Conf(self::$path."confNotPHP");
}

/**
* @depends testImportFile
* @depends testImportFromFile
*/
function testImportFileCorrupt() {
function testImportFromCorruptFile() {
$this->assertException("fileCorrupt", "Conf");
// this should not print the output of the non-PHP file
$conf = new Conf(self::$path."confCorrupt");
Expand Down
39 changes: 32 additions & 7 deletions tests/TestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,57 @@ static function tearDownAfterClass() {
Lang::set(Lang::DEFAULT);
}

function testBasic() {
function testBaseClass() {
$this->assertException("unknown");
throw new Exception("unknown");
}

/**
* @depends testBasic
* @depends testBaseClass
*/
function testPlain() {
function testBaseClassWithoutMessage() {
$this->assertException("unknown");
throw new Exception();
}

/**
* @depends testBasic
* @depends testBaseClass
*/
function testNamespace() {
function testDerivedClass() {
$this->assertException("fileMissing", "Lang");
throw new Lang\Exception("fileMissing");
}

/**
* @depends testNamespace
* @depends testDerivedClass
*/
function testValues() {
function testDerivedClassWithMessageParameters() {
$this->assertException("fileMissing", "Lang");
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");
}

}
14 changes: 7 additions & 7 deletions tests/TestLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class TestLang extends \PHPUnit\Framework\TestCase {
static $files;
static $defaultPath;

function testList() {
function testListLanguages() {
$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_ca", Lang::set("en_ca"));
$this->assertEquals("de", Lang::set("de_ch"));
Expand All @@ -30,7 +30,7 @@ function testSet() {
}

/**
* @depends testSet
* @depends testSetLanguage
*/
function testLoadInternalStrings() {
$this->assertEquals("", Lang::set("", true));
Expand All @@ -40,17 +40,17 @@ function testLoadInternalStrings() {
/**
* @depends testLoadInternalStrings
*/
function testLoadDefaultStrings() {
function testLoadDefaultLanguage() {
$this->assertEquals(Lang::DEFAULT, Lang::set(Lang::DEFAULT, true));
$str = Lang::dump();
$this->assertArrayHasKey('Exception.JKingWeb/NewsSync/Exception.uncoded', $str);
$this->assertArrayHasKey('Test.presentText', $str);
}

/**
* @depends testLoadDefaultStrings
* @depends testLoadDefaultLanguage
*/
function testLoadMultipleFiles() {
function testLoadSupplementaryLanguage() {
Lang::set(Lang::DEFAULT, true);
$this->assertEquals("ja", Lang::set("ja", true));
$str = Lang::dump();
Expand Down
17 changes: 11 additions & 6 deletions tests/TestLangErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,37 @@ function setUp() {
Lang::set("", true);
}

function testLoadFileEmpty() {
function testLoadEmptyFile() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("fr_ca", true);
}

function testLoadFileNotAnArray() {
function testLoadFileWhichDoesNotReturnAnArray() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("it", true);
}

function testLoadFileNotPhp() {
function testLoadFileWhichIsNotPhp() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("ko", true);
}

function testLoadFileCorrupt() {
function testLoadFileWhichIsCorrupt() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("zh", true);
}

function testLoadFileUnreadable() {
function testLoadFileWithooutReadPermission() {
$this->assertException("fileUnreadable", "Lang");
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
unlink(self::$path.Lang::DEFAULT.".php");
$this->assertException("defaultFileMissing", "Lang");
Expand Down
14 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ function assertException(string $msg, string $prefix = "", string $type = "Excep
$this->expectException($class);
$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 {
Expand All @@ -31,6 +44,7 @@ static function setUpBeforeClass() {
'fr.php' => '<?php return ["Test.presentText" => "à l\'école des sorciers"];',
'ja.php' => '<?php return ["Test.absentText" => "賢者の石"];',
'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 [];',
// corrupt files
'it.php' => '<?php return 0;',
Expand Down
29 changes: 13 additions & 16 deletions tests/testLangComplex.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function setUp() {
Lang::set(Lang::DEFAULT, true);
}

function testLoadLazy() {
function testLazyLoad() {
Lang::set("ja");
$this->assertArrayNotHasKey('Test.absentText', Lang::dump());
}

function testLoadCascade() {
function testLoadCascadeOfFiles() {
Lang::set("ja", true);
$this->assertEquals("de", Lang::set("de", true));
$str = Lang::dump();
Expand All @@ -30,54 +30,51 @@ function testLoadCascade() {
}

/**
* @depends testLoadCascade
* @depends testLoadCascadeOfFiles
*/
function testLoadSubtag() {
$this->assertEquals("en_ca", Lang::set("en_ca", true));
}

/**
* @depends testLoadSubtag
*/
function testMessage() {
function testFetchAMessage() {
Lang::set("de", true);
$this->assertEquals('und der Stein der Weisen', Lang::msg('Test.presentText'));
}

/**
* @depends testMessage
* @depends testFetchAMessage
*/
function testMessageNumSingle() {
function testFetchAMessageWithSingleNumericParameter() {
Lang::set("en_ca", true);
$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);
$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']));
}

/**
* @depends testMessage
* @depends testFetchAMessage
*/
function testReloadDefaults() {
function testReloadDefaultStrings() {
Lang::set("de", true);
Lang::set("en", true);
$this->assertEquals('and the Philosopher\'s Stone', Lang::msg('Test.presentText'));
}

/**
* @depends testMessage
* @depends testFetchAMessage
*/
function testReloadGeneralTagAfterSubtag() {
Lang::set("en", true);
Expand Down
5 changes: 2 additions & 3 deletions vendor/JKingWeb/NewsSync/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Exception extends \Exception {

const CODES = [
"Exception.uncoded" => -1,
"Exception.invalid" => 1, // this exception MUST NOT have a message string defined
"Exception.unknown" => 10000,
"Lang/Exception.defaultFileMissing" => 10101,
"Lang/Exception.fileMissing" => 10102,
Expand Down Expand Up @@ -46,9 +47,7 @@ public function __construct(string $msgID = "", $vars = null, \Throwable $e = nu
} else {
$codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID";
if(!array_key_exists($codeID,self::CODES)) {
$code = -1;
$msg = "Exception.".str_replace("\\","/",__CLASS__).".uncoded";
$vars = $msgID;
throw new self("uncoded");
} else {
$code = self::CODES[$codeID];
$msg = "Exception.".str_replace("\\","/",get_called_class()).".$msgID";
Expand Down

0 comments on commit 849294d

Please sign in to comment.