1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 09:02:41 +00:00
Arsse/tests/Conf/TestConf.php

147 lines
5.2 KiB
PHP
Raw Normal View History

2017-02-06 00:00:57 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
2017-02-06 00:00:57 +00:00
declare(strict_types=1);
2017-03-28 04:12:12 +00:00
namespace JKingWeb\Arsse;
2017-08-29 14:50:31 +00:00
use org\bovigo\vfs\vfsStream;
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @covers \JKingWeb\Arsse\Conf */
class TestConf extends Test\AbstractTest {
2017-08-29 14:50:31 +00:00
public static $vfs;
public static $path;
2017-02-06 00:00:57 +00:00
2017-08-29 14:50:31 +00:00
public function setUp() {
$this->clearData();
2017-02-16 20:29:42 +00:00
self::$vfs = vfsStream::setup("root", null, [
'confGood' => '<?php return Array("lang" => "xx");',
'confNotArray' => '<?php return 0;',
'confCorrupt' => '<?php return 0',
'confNotPHP' => 'DEAD BEEF',
'confEmpty' => '',
'confUnreadable' => '',
'confForbidden' => [],
2017-02-16 20:29:42 +00:00
]);
self::$path = self::$vfs->url()."/";
// set up a file without read or write access
2017-02-16 20:29:42 +00:00
chmod(self::$path."confUnreadable", 0000);
// set up a directory without read or write access
chmod(self::$path."confForbidden", 0000);
2017-02-16 20:29:42 +00:00
}
2017-02-08 15:00:38 +00:00
2017-08-29 14:50:31 +00:00
public function tearDown() {
2017-02-16 20:29:42 +00:00
self::$path = null;
self::$vfs = null;
$this->clearData();
2017-02-16 20:29:42 +00:00
}
2017-08-29 14:50:31 +00:00
public function testLoadDefaultValues() {
2017-02-16 20:29:42 +00:00
$this->assertInstanceOf(Conf::class, new Conf());
}
2017-07-20 22:36:03 +00:00
/** @depends testLoadDefaultValues */
2017-08-29 14:50:31 +00:00
public function testImportFromArray() {
2017-02-16 20:29:42 +00:00
$arr = ['lang' => "xx"];
$conf = new Conf();
$conf->import($arr);
$this->assertEquals("xx", $conf->lang);
}
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromArray */
2017-08-29 14:50:31 +00:00
public function testImportFromFile() {
2017-02-16 20:29:42 +00:00
$conf = new Conf();
$conf->importFile(self::$path."confGood");
$this->assertEquals("xx", $conf->lang);
$conf = new Conf(self::$path."confGood");
$this->assertEquals("xx", $conf->lang);
}
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testImportFromMissingFile() {
2017-02-16 20:29:42 +00:00
$this->assertException("fileMissing", "Conf");
$conf = new Conf(self::$path."confMissing");
}
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testImportFromEmptyFile() {
2017-02-16 20:29:42 +00:00
$this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confEmpty");
}
2017-02-08 21:59:16 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testImportFromFileWithoutReadPermission() {
2017-02-16 20:29:42 +00:00
$this->assertException("fileUnreadable", "Conf");
$conf = new Conf(self::$path."confUnreadable");
}
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testImportFromFileWhichIsNotAnArray() {
2017-02-16 20:29:42 +00:00
$this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confNotArray");
}
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testImportFromFileWhichIsNotPhp() {
2017-02-16 20:29:42 +00:00
$this->assertException("fileCorrupt", "Conf");
// this should not print the output of the non-PHP file
$conf = new Conf(self::$path."confNotPHP");
}
2017-02-06 00:00:57 +00:00
2017-07-20 22:36:03 +00:00
/** @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testImportFromCorruptFile() {
2017-02-16 20:29:42 +00:00
$this->assertException("fileCorrupt", "Conf");
$conf = new Conf(self::$path."confCorrupt");
}
2017-08-29 14:50:31 +00:00
public function testExportToArray() {
$conf = new Conf();
$conf->lang = ["en", "fr"]; // should not be exported: not scalar
$conf->dbSQLite3File = "test.db"; // should be exported: value changed
2017-08-30 03:17:57 +00:00
$conf->userDriver = null; // should be exported: changed value, even when null
$conf->someCustomProperty = "Look at me!"; // should be exported: unknown property
$exp = [
'dbSQLite3File' => "test.db",
2017-08-30 03:17:57 +00:00
'userDriver' => null,
'someCustomProperty' => "Look at me!",
];
$this->assertSame($exp, $conf->export());
$res = $conf->export(true); // export all properties
$this->assertNotSame($exp, $res);
$this->assertArraySubset($exp, $res);
}
2017-08-29 14:50:31 +00:00
/** @depends testExportToArray
* @depends testImportFromFile */
2017-08-29 14:50:31 +00:00
public function testExportToFile() {
$conf = new Conf();
$conf->lang = ["en", "fr"]; // should not be exported: not scalar
$conf->dbSQLite3File = "test.db"; // should be exported: value changed
2017-08-30 03:17:57 +00:00
$conf->userDriver = null; // should be exported: changed value, even when null
$conf->someCustomProperty = "Look at me!"; // should be exported: unknown property
$conf->exportFile(self::$path."confNotArray");
$arr = (include self::$path."confNotArray");
$exp = [
'dbSQLite3File' => "test.db",
2017-08-30 03:17:57 +00:00
'userDriver' => null,
'someCustomProperty' => "Look at me!",
];
$this->assertSame($exp, $arr);
$conf->exportFile(self::$path."confNotArray", true); // export all properties
$arr = (include self::$path."confNotArray");
$this->assertNotSame($exp, $arr);
$this->assertArraySubset($exp, $arr);
}
2017-08-29 14:50:31 +00:00
public function testExportToFileWithoutWritePermission() {
$this->assertException("fileUnwritable", "Conf");
(new Conf)->exportFile(self::$path."confUnreadable");
}
2017-08-29 14:50:31 +00:00
public function testExportToFileWithoutCreatePermission() {
$this->assertException("fileUncreatable", "Conf");
(new Conf)->exportFile(self::$path."confForbidden/conf");
}
2017-02-06 00:00:57 +00:00
}