J. King
7 years ago
8 changed files with 176 additions and 16 deletions
@ -0,0 +1,97 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\NewsSync; |
||||
|
use \org\bovigo\vfs\vfsStream; |
||||
|
|
||||
|
|
||||
|
class TestConf extends \PHPUnit\Framework\TestCase { |
||||
|
use TestingHelpers; |
||||
|
|
||||
|
static $vfs; |
||||
|
|
||||
|
static function setUpBeforeClass() { |
||||
|
$vfs = vfsStream::setup()->url(); |
||||
|
foreach(["confUnreadable","confGood", "confCorrupt", "confNotArray"] as $file) { |
||||
|
touch($vfs."/".$file); |
||||
|
} |
||||
|
chmod($vfs."/confUnreadable", 0000); |
||||
|
$validConf = <<<VALID_CONFIGURATION_FILE |
||||
|
<?php |
||||
|
return Array( |
||||
|
"lang" => "xx" |
||||
|
); |
||||
|
VALID_CONFIGURATION_FILE; |
||||
|
file_put_contents($vfs."/confGood",$validConf); |
||||
|
file_put_contents($vfs."/confNotArray", "<?php return 0;");
|
||||
|
file_put_contents($vfs."/confCorrupt", "<?php return 0");
|
||||
|
file_put_contents($vfs."/confNotPHP", "DEAD BEEF"); |
||||
|
self::$vfs = $vfs; |
||||
|
} |
||||
|
|
||||
|
function testConstruct() { |
||||
|
$this->assertInstanceOf(Conf::class, new Conf()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testConstruct |
||||
|
*/ |
||||
|
function testImportArray() { |
||||
|
$arr = ['lang' => "xx"]; |
||||
|
$conf = new Conf(); |
||||
|
$conf->import($arr); |
||||
|
$this->assertEquals("xx", $conf->lang); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testImportArray |
||||
|
*/ |
||||
|
function testImportFile() { |
||||
|
$conf = new Conf(); |
||||
|
$conf->importFile(self::$vfs."/confGood"); |
||||
|
$this->assertEquals("xx", $conf->lang); |
||||
|
$conf = new Conf(self::$vfs."/confGood"); |
||||
|
$this->assertEquals("xx", $conf->lang); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testImportFile |
||||
|
*/ |
||||
|
function testImportFileMissing() { |
||||
|
$this->assertException("fileMissing", "Conf"); |
||||
|
$conf = new Conf(self::$vfs."/confMissing"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testImportFile |
||||
|
*/ |
||||
|
function testImportFileUnreadable() { |
||||
|
$this->assertException("fileUnreadable", "Conf"); |
||||
|
$conf = new Conf(self::$vfs."/confUnreadable"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testImportFile |
||||
|
*/ |
||||
|
function testImportFileNotAnArray() { |
||||
|
$this->assertException("fileCorrupt", "Conf"); |
||||
|
$conf = new Conf(self::$vfs."/confNotArray"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testImportFile |
||||
|
*/ |
||||
|
function testImportFileNotPHP() { |
||||
|
$this->assertException("fileCorrupt", "Conf"); |
||||
|
// this should not print the output of the non-PHP file |
||||
|
$conf = new Conf(self::$vfs."/confNotPHP"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @depends testImportFile |
||||
|
*/ |
||||
|
function testImportFileCorrupt() { |
||||
|
$this->assertException("fileCorrupt", "Conf"); |
||||
|
// this should not print the output of the non-PHP file |
||||
|
$conf = new Conf(self::$vfs."/confCorrupt"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace JKingWeb\NewsSync; |
||||
|
|
||||
|
const BASE = __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR; |
||||
|
const NS_BASE = __NAMESPACE__."\\"; |
||||
|
|
||||
|
spl_autoload_register(function ($class) { |
||||
|
if($class=="SimplePie") return; |
||||
|
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class); |
||||
|
$file = BASE."vendor".DIRECTORY_SEPARATOR.$file.".php"; |
||||
|
if (file_exists($file)) { |
||||
|
require_once $file; |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
trait TestingHelpers { |
||||
|
function assertException(string $msg, string $prefix = "", string $type = "Exception") { |
||||
|
$class = NS_BASE . ($prefix !== "" ? str_replace("/", "\\", $prefix) . "\\" : "") . $type; |
||||
|
$msgID = ($prefix !== "" ? $prefix . "/" : "") . $type. ".$msg"; |
||||
|
if(array_key_exists($msgID, Exception::CODES)) { |
||||
|
$code = Exception::CODES[$msgID]; |
||||
|
} else { |
||||
|
$code = 0; |
||||
|
} |
||||
|
$this->expectException($class); |
||||
|
$this->expectExceptionCode($code); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ignore_user_abort(true); |
@ -0,0 +1,18 @@ |
|||||
|
<?xml version="1.0"?> |
||||
|
<phpunit |
||||
|
colors="true" |
||||
|
bootstrap="bootstrap.php" |
||||
|
convertErrorsToExceptions="true" |
||||
|
convertNoticesToExceptions="true" |
||||
|
convertWarningsToExceptions="true" |
||||
|
beStrictAboutTestsThatDoNotTestAnything="true" |
||||
|
beStrictAboutOutputDuringTests="true" |
||||
|
beStrictAboutTestSize="true"> |
||||
|
|
||||
|
<testsuite name="Base"> |
||||
|
<file>TestConf.php</file> |
||||
|
</testsuite> |
||||
|
|
||||
|
|
||||
|
|
||||
|
</phpunit> |
Loading…
Reference in new issue