2017-02-06 00:00:57 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @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-12-22 03:47:19 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Conf;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-12-22 03:47:19 +00:00
|
|
|
use JKingWeb\Arsse\Conf;
|
2017-03-28 22:50:00 +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 */
|
2017-12-22 03:47:19 +00:00
|
|
|
class TestConf extends \JKingWeb\Arsse\Test\AbstractTest {
|
2017-08-29 14:50:31 +00:00
|
|
|
public static $vfs;
|
|
|
|
public static $path;
|
2017-02-06 00:00:57 +00:00
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
parent::setUp();
|
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' => '',
|
2017-07-27 13:09:39 +00:00
|
|
|
'confForbidden' => [],
|
2017-02-16 20:29:42 +00:00
|
|
|
]);
|
|
|
|
self::$path = self::$vfs->url()."/";
|
2017-07-27 13:09:39 +00:00
|
|
|
// set up a file without read or write access
|
2017-02-16 20:29:42 +00:00
|
|
|
chmod(self::$path."confUnreadable", 0000);
|
2017-07-27 13:09:39 +00:00
|
|
|
// 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
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function tearDown(): void {
|
2017-02-16 20:29:42 +00:00
|
|
|
self::$path = null;
|
|
|
|
self::$vfs = null;
|
2021-02-27 20:24:02 +00:00
|
|
|
parent::tearDown();
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadDefaultValues(): void {
|
2018-11-04 14:16:34 +00:00
|
|
|
$this->assertInstanceOf(Conf::class, new Conf);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
|
2017-07-20 22:36:03 +00:00
|
|
|
/** @depends testLoadDefaultValues */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromArray(): void {
|
2019-01-21 03:40:49 +00:00
|
|
|
$arr = [
|
2020-03-01 20:16:50 +00:00
|
|
|
'lang' => "xx",
|
2019-01-21 03:40:49 +00:00
|
|
|
'purgeFeeds' => "P2D",
|
|
|
|
];
|
2018-11-04 14:16:34 +00:00
|
|
|
$conf = new Conf;
|
2017-02-16 20:29:42 +00:00
|
|
|
$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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromFile(): void {
|
2018-11-04 14:16:34 +00:00
|
|
|
$conf = new Conf;
|
2017-02-16 20:29:42 +00:00
|
|
|
$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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromMissingFile(): void {
|
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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromEmptyFile(): void {
|
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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromFileWithoutReadPermission(): void {
|
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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromFileWhichIsNotAnArray(): void {
|
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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromFileWhichIsNotPhp(): void {
|
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 */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportFromCorruptFile(): void {
|
2017-02-16 20:29:42 +00:00
|
|
|
$this->assertException("fileCorrupt", "Conf");
|
|
|
|
$conf = new Conf(self::$path."confCorrupt");
|
|
|
|
}
|
2017-07-27 13:09:39 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportBogusValue(): void {
|
2019-01-21 03:40:49 +00:00
|
|
|
$arr = [
|
|
|
|
'dbAutoUpdate' => "yes, please",
|
|
|
|
];
|
|
|
|
$conf = new Conf;
|
|
|
|
$this->assertException("typeMismatch", "Conf");
|
|
|
|
$conf->import($arr);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testImportBogusDriver(): void {
|
2019-01-21 03:40:49 +00:00
|
|
|
$arr = [
|
|
|
|
'dbDriver' => "this driver does not exist",
|
|
|
|
];
|
|
|
|
$conf = new Conf;
|
|
|
|
$this->assertException("semanticMismatch", "Conf");
|
|
|
|
$conf->import($arr);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testExportToArray(): void {
|
2018-11-04 14:16:34 +00:00
|
|
|
$conf = new Conf;
|
2017-07-27 13:09:39 +00:00
|
|
|
$conf->lang = ["en", "fr"]; // should not be exported: not scalar
|
2017-08-20 03:56:32 +00:00
|
|
|
$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
|
2019-01-21 14:55:25 +00:00
|
|
|
$conf->serviceFrequency = new \DateInterval("PT1H"); // should be exported (as string): value changed
|
2017-07-27 13:09:39 +00:00
|
|
|
$conf->someCustomProperty = "Look at me!"; // should be exported: unknown property
|
|
|
|
$exp = [
|
2020-03-01 20:16:50 +00:00
|
|
|
'dbSQLite3File' => "test.db",
|
|
|
|
'userDriver' => null,
|
|
|
|
'serviceFrequency' => "PT1H",
|
2017-07-27 13:09:39 +00:00
|
|
|
'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
|
2017-07-27 13:09:39 +00:00
|
|
|
* @depends testImportFromFile */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testExportToFile(): void {
|
2018-11-04 14:16:34 +00:00
|
|
|
$conf = new Conf;
|
2017-07-27 13:09:39 +00:00
|
|
|
$conf->lang = ["en", "fr"]; // should not be exported: not scalar
|
2017-08-20 03:56:32 +00:00
|
|
|
$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
|
2017-07-27 13:09:39 +00:00
|
|
|
$conf->someCustomProperty = "Look at me!"; // should be exported: unknown property
|
|
|
|
$conf->exportFile(self::$path."confNotArray");
|
|
|
|
$arr = (include self::$path."confNotArray");
|
|
|
|
$exp = [
|
2020-03-01 20:16:50 +00:00
|
|
|
'dbSQLite3File' => "test.db",
|
|
|
|
'userDriver' => null,
|
2017-07-27 13:09:39 +00:00
|
|
|
'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);
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:32:28 +00:00
|
|
|
/** @depends testExportToFile */
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testExportToStdout(): void {
|
2018-11-06 17:32:28 +00:00
|
|
|
$conf = new Conf(self::$path."confGood");
|
|
|
|
$conf->exportFile(self::$path."confGood");
|
|
|
|
$this->expectOutputString(file_get_contents(self::$path."confGood"));
|
|
|
|
$conf->exportFile("php://output");
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testExportToFileWithoutWritePermission(): void {
|
2017-07-27 13:09:39 +00:00
|
|
|
$this->assertException("fileUnwritable", "Conf");
|
|
|
|
(new Conf)->exportFile(self::$path."confUnreadable");
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testExportToFileWithoutCreatePermission(): void {
|
2017-07-27 13:09:39 +00:00
|
|
|
$this->assertException("fileUncreatable", "Conf");
|
|
|
|
(new Conf)->exportFile(self::$path."confForbidden/conf");
|
|
|
|
}
|
2017-02-06 00:00:57 +00:00
|
|
|
}
|