Browse Source

Basic tests for exceptions

microsub
J. King 7 years ago
parent
commit
2c134c76dc
  1. 45
      tests/TestException.php
  2. 2
      tests/bootstrap.php
  3. 1
      tests/phpunit.xml
  4. 4
      vendor/JKingWeb/NewsSync/Exception.php
  5. 1
      vendor/JKingWeb/NewsSync/Lang.php

45
tests/TestException.php

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync;
class TestException extends \PHPUnit\Framework\TestCase {
use TestingHelpers;
static function setUpBeforeClass() {
Lang::set("");
}
static function tearDownAfterClass() {
Lang::set(Lang::DEFAULT);
}
function testBasic() {
$this->assertException("unknown");
throw new Exception("unknown");
}
/**
* @depends testBasic
*/
function testPlain() {
$this->assertException("unknown");
throw new Exception();
}
/**
* @depends testBasic
*/
function testNamespace() {
$this->assertException("fileMissing", "Lang");
throw new Lang\Exception("fileMissing");
}
/**
* @depends testNamespace
*/
function testValues() {
$this->assertException("fileMissing", "Lang");
throw new Lang\Exception("fileMissing", "en");
}
}

2
tests/bootstrap.php

@ -55,6 +55,6 @@ trait LanguageTestingHelpers {
self::$vfs = null;
self::$files = null;
Lang::set("", true);
Lang::set(Lang::DEFAULT, true);
Lang::set(Lang::DEFAULT);
}
}

1
tests/phpunit.xml

@ -12,6 +12,7 @@
<testsuite name="Localization and exceptions">
<file>TestLang.php</file>
<file>TestLangComplex.php</file>
<file>TestException.php</file>
</testsuite>
<testsuite name="Configuration loading and saving">

4
vendor/JKingWeb/NewsSync/Exception.php

@ -41,8 +41,8 @@ class Exception extends \Exception {
public function __construct(string $msgID = "", $vars = null, \Throwable $e = null) {
if($msgID=="") {
$msg = "";
$code = 0;
$msg = "Exception.unknown";
$code = 10000;
} else {
$codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID";
if(!array_key_exists($codeID,self::CODES)) {

1
vendor/JKingWeb/NewsSync/Lang.php

@ -7,6 +7,7 @@ class Lang {
const DEFAULT = "en";
const REQUIRED = [
'Exception.JKingWeb/NewsSync/Exception.uncoded' => 'The specified exception symbol {0} has no code specified in Exception.php',
'Exception.JKingWeb/NewsSync/Exception.unknown' => 'An unknown error has occurred',
'Exception.JKingWeb/NewsSync/Lang/Exception.defaultFileMissing' => 'Default language file "{0}" missing',
'Exception.JKingWeb/NewsSync/Lang/Exception.fileMissing' => 'Language file "{0}" is not available',
'Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable' => 'Insufficient permissions to read language file "{0}"',

Loading…
Cancel
Save