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

100 lines
3 KiB
PHP
Raw Normal View History

2017-02-08 21:53:02 +00:00
<?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;
2017-02-08 21:59:16 +00:00
static $files;
2017-02-09 21:39:13 +00:00
static $defaultPath;
2017-02-08 21:53:02 +00:00
static function setUpBeforeClass() {
2017-02-09 21:39:13 +00:00
// this is required to keep from having exceptions in Lang::msg() in turn calling Lang::msg() and looping
2017-02-08 21:53:02 +00:00
Lang\Exception::$test = true;
2017-02-09 21:39:13 +00:00
// test files
2017-02-08 21:59:16 +00:00
self::$files = [
'en.php' => '<?php return ["Test.presentText" => "and the Philosopher\'s Stone"];',
2017-02-09 21:39:13 +00:00
'en_ca.php' => '<?php return ["Test.presentText" => "{0} and {1}"];',
'en_us.php' => '<?php return ["Test.presentText" => "and the Sorcerer\'s Stone"];',
2017-02-08 21:59:16 +00:00
'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"];',
2017-02-08 21:59:16 +00:00
// corrupt files
'it.php' => '<?php return 0;',
'zh.php' => '<?php return 0',
'ko.php' => 'DEAD BEEF',
2017-02-09 21:39:13 +00:00
'fr_ca.php' => '',
2017-02-08 21:59:16 +00:00
// unreadable file
'ru.php' => '',
];
self::$vfs = vfsStream::setup("langtest", 0777, self::$files);
2017-02-08 21:53:02 +00:00
self::$path = self::$vfs->url();
// set up a file without read access
chmod(self::$path."/ru.php", 0000);
2017-02-09 21:39:13 +00:00
// make the Lang class use the vfs files
self::$defaultPath = Lang::$path;
Lang::$path = self::$path."/";
2017-02-08 21:53:02 +00:00
}
static function tearDownAfterClass() {
Lang\Exception::$test = false;
2017-02-09 21:39:13 +00:00
Lang::$path = self::$defaultPath;
2017-02-08 21:53:02 +00:00
self::$path = null;
self::$vfs = null;
2017-02-08 21:59:16 +00:00
self::$files = null;
2017-02-09 21:47:33 +00:00
Lang::set("", true);
2017-02-09 21:39:13 +00:00
Lang::set(Lang::DEFAULT, true);
2017-02-08 21:53:02 +00:00
}
function testList() {
2017-02-09 21:39:13 +00:00
$this->assertCount(sizeof(self::$files), Lang::list("en"));
2017-02-08 21:53:02 +00:00
}
2017-02-09 21:39:13 +00:00
/**
* @depends testList
*/
function testSet() {
$this->assertEquals("en", Lang::set("en"));
$this->assertEquals("en_ca", Lang::set("en_ca"));
$this->assertEquals("de", Lang::set("de_ch"));
$this->assertEquals("en", Lang::set("en_gb_hixie"));
$this->assertEquals("en_ca", Lang::set("en_ca_jking"));
$this->assertEquals("en", Lang::set("es"));
$this->assertEquals("", Lang::set(""));
}
/**
* @depends testSet
*/
function testLoadInternalStrings() {
$this->assertEquals("", Lang::set("", true));
$this->assertCount(sizeof(Lang::REQUIRED), Lang::dump());
}
/**
* @depends testLoadInternalStrings
*/
function testLoadDefaultStrings() {
$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
*/
function testLoadMultipleFiles() {
Lang::set(Lang::DEFAULT, true);
$this->assertEquals("ja", Lang::set("ja", true));
$str = Lang::dump();
$this->assertArrayHasKey('Exception.JKingWeb/NewsSync/Exception.uncoded', $str);
$this->assertArrayHasKey('Test.presentText', $str);
$this->assertArrayHasKey('Test.absentText', $str);
}
2017-02-08 21:53:02 +00:00
}