Browse Source

Start of unit tests for Lang class

microsub
J. King 7 years ago
parent
commit
bc6ee434e5
  1. 7
      .gitignore
  2. 46
      tests/TestLang.php
  3. 1
      tests/phpunit.xml
  4. 24
      vendor/JKingWeb/NewsSync/Lang.php
  5. 18
      vendor/JKingWeb/NewsSync/Lang/Exception.php

7
.gitignore

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

46
tests/TestLang.php

@ -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/")));
}
}

1
tests/phpunit.xml

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

24
vendor/JKingWeb/NewsSync/Lang.php

@ -1,6 +1,7 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
use \Webmozart\Glob\Glob;
class Lang {
const PATH = BASE."locale".DIRECTORY_SEPARATOR;
@ -63,9 +64,9 @@ class Lang {
return $msg;
}
static public function list(string $locale = ""): array {
static public function list(string $locale = "", string $path = self::PATH): array {
$out = [];
$files = self::listFiles();
$files = self::listFiles($path);
foreach($files as $tag) {
$out[$tag] = \Locale::getDisplayName($tag, ($locale=="") ? $tag : $locale);
}
@ -85,11 +86,13 @@ class Lang {
}
static protected function listFiles(string $path = self::PATH): array {
$out = glob($path."*.php");
return array_map(function($file) {
$file = substr($file,strrpos($file,DIRECTORY_SEPARATOR)+1);
$out = Glob::glob($path."*.php");
$out = array_map(function($file) {
$file = substr(str_replace(DIRECTORY_SEPARATOR, "/", $file),strrpos($file,"/")+1);
return strtolower(substr($file,0,strrpos($file,".")));
},$out);
natsort($out);
return $out;
}
static protected function load(): bool {
@ -127,7 +130,16 @@ class Lang {
foreach($files as $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(!$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
self::$strings = call_user_func_array("array_replace_recursive", $strings);

18
vendor/JKingWeb/NewsSync/Lang/Exception.php

@ -3,4 +3,22 @@ declare(strict_types=1);
namespace JKingWeb\NewsSync\Lang;
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);
}
}
}
Loading…
Cancel
Save