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

Start of unit tests for Lang class

This commit is contained in:
J. King 2017-02-08 16:53:02 -05:00
parent ad0f28b8cc
commit bc6ee434e5
5 changed files with 87 additions and 9 deletions

7
.gitignore vendored
View file

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

46
tests/TestLang.php Normal file
View file

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
use \org\bovigo\vfs\vfsStream;
class TestLang extends \PHPUnit\Framework\TestCase {
use TestingHelpers;
static $vfs;
static $path;
const FILES = [
'en.php' => '<?php return ["Test.presentText" => "and the Philosopher\'s Stone"];',
'en-ca.php' => '<?php return [];',
'en-us.php' => '<?php return ["Test.presentText" => "and the Sorcerer\'s Stone"];',
'fr.php' => '<?php return ["Test.presentText" => "à l\'école des sorciers"];',
'ja.php' => '<?php return ["Test.absentText" => "賢者の石"];',
// corrupt files
'it.php' => '<?php return 0;',
'zh.php' => '<?php return 0',
'ko.php' => 'DEAD BEEF',
// empty file
'fr-ca.php' => '',
// unreadable file
'ru.php' => '',
];
static function setUpBeforeClass() {
Lang\Exception::$test = true;
self::$vfs = vfsStream::setup("langtest", 0777, self::FILES);
self::$path = self::$vfs->url();
// set up a file without read access
chmod(self::$path."/ru.php", 0000);
}
static function tearDownAfterClass() {
Lang\Exception::$test = false;
self::$path = null;
self::$vfs = null;
}
function testList() {
$this->assertEquals(sizeof(self::FILES), sizeof(Lang::list("en", "vfs://langtest/")));
}
}

View file

@ -10,6 +10,7 @@
beStrictAboutTestSize="true"> beStrictAboutTestSize="true">
<testsuite name="Base"> <testsuite name="Base">
<file>TestLang.php</file>
<file>TestConf.php</file> <file>TestConf.php</file>
</testsuite> </testsuite>

View file

@ -1,6 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace JKingWeb\NewsSync; namespace JKingWeb\NewsSync;
use \Webmozart\Glob\Glob;
class Lang { class Lang {
const PATH = BASE."locale".DIRECTORY_SEPARATOR; const PATH = BASE."locale".DIRECTORY_SEPARATOR;
@ -63,9 +64,9 @@ class Lang {
return $msg; return $msg;
} }
static public function list(string $locale = ""): array { static public function list(string $locale = "", string $path = self::PATH): array {
$out = []; $out = [];
$files = self::listFiles(); $files = self::listFiles($path);
foreach($files as $tag) { foreach($files as $tag) {
$out[$tag] = \Locale::getDisplayName($tag, ($locale=="") ? $tag : $locale); $out[$tag] = \Locale::getDisplayName($tag, ($locale=="") ? $tag : $locale);
} }
@ -85,11 +86,13 @@ class Lang {
} }
static protected function listFiles(string $path = self::PATH): array { static protected function listFiles(string $path = self::PATH): array {
$out = glob($path."*.php"); $out = Glob::glob($path."*.php");
return array_map(function($file) { $out = array_map(function($file) {
$file = substr($file,strrpos($file,DIRECTORY_SEPARATOR)+1); $file = substr(str_replace(DIRECTORY_SEPARATOR, "/", $file),strrpos($file,"/")+1);
return strtolower(substr($file,0,strrpos($file,"."))); return strtolower(substr($file,0,strrpos($file,".")));
},$out); },$out);
natsort($out);
return $out;
} }
static protected function load(): bool { static protected function load(): bool {
@ -127,7 +130,16 @@ class Lang {
foreach($files as $file) { foreach($files as $file) {
if(!file_exists(self::PATH."$file.php")) throw new Lang\Exception("fileMissing", $file); if(!file_exists(self::PATH."$file.php")) throw new Lang\Exception("fileMissing", $file);
if(!is_readable(self::PATH."$file.php")) throw new Lang\Exception("fileUnreadable", $file); if(!is_readable(self::PATH."$file.php")) throw new Lang\Exception("fileUnreadable", $file);
if(!$strings[] = (@include self::PATH."$file.php")) throw new Lang\Exception("fileCorrupt", $file); try {
ob_start();
$arr = (include self::PATH."$file.php");
} catch(\Throwable $e) {
$arr = null;
} finally {
ob_end_clean();
}
if(!is_array($arr)) throw new Lang\Exception("fileCorrupt", $file);
$strings[] = $arr;
} }
// apply the results and return // apply the results and return
self::$strings = call_user_func_array("array_replace_recursive", $strings); self::$strings = call_user_func_array("array_replace_recursive", $strings);

View file

@ -3,4 +3,22 @@ declare(strict_types=1);
namespace JKingWeb\NewsSync\Lang; namespace JKingWeb\NewsSync\Lang;
class Exception extends \JKingWeb\NewsSync\Exception { class Exception extends \JKingWeb\NewsSync\Exception {
static $test = false; // used during PHPUnit testing only
function __construct(string $msgID = "", $vars = null, \Throwable $e = null) {
if(!self::$test) {
parent::__construct($msgID, $vars, $e);
} else {
$codeID = "Lang/Exception.$msgID";
if(!array_key_exists($codeID,self::CODES)) {
$code = -1;
$msg = "Exception.".str_replace("\\","/",parent::class).".uncoded";
$vars = $msgID;
} else {
$code = self::CODES[$codeID];
$msg = "Exception.".str_replace("\\","/",__CLASS__).".$msgID";
}
\Exception::construct($msg, $code, $e);
}
}
} }