Browse Source

Last set of language tests

microsub
J. King 7 years ago
parent
commit
4bc602957c
  1. 2
      tests/TestConf.php
  2. 50
      tests/TestLangErrors.php
  3. 7
      tests/bootstrap.php
  4. 4
      tests/phpunit.xml
  5. 22
      tests/testLangComplex.php
  6. 1
      vendor/JKingWeb/NewsSync/Lang.php

2
tests/TestConf.php

@ -89,7 +89,7 @@ class TestConf extends \PHPUnit\Framework\TestCase {
/**
* @depends testImportFile
*/
function testImportFileNotPHP() {
function testImportFileNotPhp() {
$this->assertException("fileCorrupt", "Conf");
// this should not print the output of the non-PHP file
$conf = new Conf(self::$path."confNotPHP");

50
tests/TestLangErrors.php

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
use \org\bovigo\vfs\vfsStream;
class TestLangErrors extends \PHPUnit\Framework\TestCase {
use TestingHelpers, LanguageTestingHelpers;
static $vfs;
static $path;
static $files;
static $defaultPath;
function setUp() {
Lang::set("", true);
}
function testLoadFileEmpty() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("fr_ca", true);
}
function testLoadFileNotAnArray() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("it", true);
}
function testLoadFileNotPhp() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("ko", true);
}
function testLoadFileCorrupt() {
$this->assertException("fileCorrupt", "Lang");
Lang::set("zh", true);
}
function testLoadFileUnreadable() {
$this->assertException("fileUnreadable", "Lang");
Lang::set("ru", true);
}
function testLoadDefaultMissing() {
// this should be the last test of the series
unlink(self::$path.Lang::DEFAULT.".php");
$this->assertException("defaultFileMissing", "Lang");
Lang::set("fr", true);
}
}

7
tests/bootstrap.php

@ -31,6 +31,7 @@ trait LanguageTestingHelpers {
'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"];',
'vi.php' => '<?php return [];',
// corrupt files
'it.php' => '<?php return 0;',
'zh.php' => '<?php return 0',
@ -40,12 +41,12 @@ trait LanguageTestingHelpers {
'ru.php' => '',
];
self::$vfs = vfsStream::setup("langtest", 0777, self::$files);
self::$path = self::$vfs->url();
self::$path = self::$vfs->url()."/";
// set up a file without read access
chmod(self::$path."/ru.php", 0000);
chmod(self::$path."ru.php", 0000);
// make the Lang class use the vfs files
self::$defaultPath = Lang::$path;
Lang::$path = self::$path."/";
Lang::$path = self::$path;
}
static function tearDownAfterClass() {

4
tests/phpunit.xml

@ -7,12 +7,14 @@
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true">
beStrictAboutTestSize="true"
stopOnError="true">
<testsuite name="Localization and exceptions">
<file>TestLang.php</file>
<file>TestLangComplex.php</file>
<file>TestException.php</file>
<file>TestLangErrors.php</file>
</testsuite>
<testsuite name="Configuration loading and saving">

22
tests/testLangComplex.php

@ -47,7 +47,7 @@ class TestLangComplex extends \PHPUnit\Framework\TestCase {
/**
* @depends testMessage
*/
function testMessageNumMSingle() {
function testMessageNumSingle() {
Lang::set("en_ca", true);
$this->assertEquals('Default language file "en" missing', Lang::msg('Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing', Lang::DEFAULT));
}
@ -66,4 +66,24 @@ class TestLangComplex extends \PHPUnit\Framework\TestCase {
function testMessageNamed() {
$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
*/
function testReloadDefaults() {
Lang::set("de", true);
Lang::set("en", true);
$this->assertEquals('and the Philosopher\'s Stone', Lang::msg('Test.presentText'));
}
/**
* @depends testMessage
*/
function testReloadGeneralTagAfterSubtag() {
Lang::set("en", true);
Lang::set("en_us", true);
$this->assertEquals('and the Sorcerer\'s Stone', Lang::msg('Test.presentText'));
Lang::set("en", true);
$this->assertEquals('and the Philosopher\'s Stone', Lang::msg('Test.presentText'));
}
}

1
vendor/JKingWeb/NewsSync/Lang.php

@ -94,6 +94,7 @@ class Lang {
static protected function listFiles(): array {
$out = glob(self::$path."*.php");
// built-in glob doesn't work with vfsStream (and this other glob doesn't seem to work with Windows paths), so we try both
if(empty($out)) $out = Glob::glob(self::$path."*.php");
$out = array_map(function($file) {
$file = str_replace(DIRECTORY_SEPARATOR, "/", $file);

Loading…
Cancel
Save