2021-01-06 16:50:32 +00:00
|
|
|
<?php
|
|
|
|
/* {{{ Copyright and License Notice
|
|
|
|
*
|
|
|
|
* Copyright © 2021 RaBe Websolutions
|
|
|
|
*
|
|
|
|
* This file is part of rabe/Util-Settings.
|
|
|
|
*
|
|
|
|
* rabe/Util-Settings is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* rabe/Util-Settings is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with rabe/Util-Settings. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*///}}}
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace rabe\Util\tests;
|
|
|
|
|
|
|
|
use rabe\Util\Settings;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class SettingsTest extends TestCase
|
|
|
|
{
|
2021-01-12 15:16:44 +00:00
|
|
|
public function testConstruct()
|
|
|
|
{
|
|
|
|
$cfg = new Settings();
|
|
|
|
$this->assertNotEmpty($cfg);
|
|
|
|
}
|
2021-01-14 07:58:14 +00:00
|
|
|
|
2021-01-12 15:16:44 +00:00
|
|
|
/**
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
public function testLoadNoFile()
|
2021-01-07 14:32:41 +00:00
|
|
|
{
|
2021-01-14 07:58:14 +00:00
|
|
|
$cfg = new Settings();
|
2021-01-14 16:48:41 +00:00
|
|
|
$this->expectException(\Exception::class);
|
2021-01-12 15:16:44 +00:00
|
|
|
$cfg->load();
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:48:41 +00:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function testReadOnly()
|
|
|
|
{
|
|
|
|
$cfg = new Settings();
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$cfg->answer = 42;
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:16:44 +00:00
|
|
|
/**
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
private function appPath(Settings $cfg, string $dir='')
|
2021-01-12 15:16:44 +00:00
|
|
|
{
|
|
|
|
if (isset($dir)) $dir .= '/';
|
|
|
|
|
|
|
|
$path = dirname(__FILE__).'/cfg/'.$dir;
|
|
|
|
$cfg->appPath($path);
|
|
|
|
|
|
|
|
return $cfg;
|
2021-01-07 14:32:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 15:16:44 +00:00
|
|
|
/**
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
public function testLoad()
|
2021-01-07 14:32:41 +00:00
|
|
|
{
|
2021-01-14 07:58:14 +00:00
|
|
|
$cfg = new Settings();
|
2021-01-12 15:16:44 +00:00
|
|
|
$cfg = $this->appPath($cfg)->load();
|
2021-01-07 14:32:41 +00:00
|
|
|
$this->assertNotEmpty($cfg);
|
|
|
|
return $cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testLoad
|
|
|
|
*/
|
|
|
|
public function testMode(Settings $cfg)
|
|
|
|
{
|
2021-01-14 07:58:14 +00:00
|
|
|
$this->assertEquals('default', $cfg->testFiles);
|
|
|
|
$this->assertEquals('prod', $cfg->mode);
|
2021-01-07 14:32:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 10:53:56 +00:00
|
|
|
/**
|
|
|
|
* @depends testLoad
|
|
|
|
*/
|
|
|
|
public function testNestedSettings(Settings $cfg)
|
|
|
|
{
|
|
|
|
$nested = $cfg->nested;
|
|
|
|
$this->assertIsObject($nested);
|
|
|
|
$this->assertIsIterable($nested);
|
|
|
|
$this->assertEquals(1, $nested->level);
|
2021-01-14 12:53:02 +00:00
|
|
|
$this->assertEquals(2, $nested->nested->level);
|
2021-01-14 10:53:56 +00:00
|
|
|
$this->assertEquals('default', $nested->testFiles);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:48:41 +00:00
|
|
|
/**
|
|
|
|
* @depends testLoad
|
|
|
|
*/
|
|
|
|
public function testNestedArray(Settings $cfg)
|
|
|
|
{
|
|
|
|
$nested = $cfg->nested->toArray();
|
|
|
|
$this->assertIsArray($nested);
|
|
|
|
$this->assertIsIterable($nested);
|
|
|
|
$this->assertEquals(1, $nested['level']);
|
|
|
|
$this->assertEquals(2, $nested['nested']['level']);
|
|
|
|
$this->assertEquals('default', $nested['testFiles']);
|
|
|
|
}
|
|
|
|
|
2021-01-14 12:53:02 +00:00
|
|
|
/**
|
|
|
|
* @depends testLoad
|
|
|
|
*/
|
|
|
|
public function testSettingNotFound(Settings $cfg)
|
|
|
|
{
|
|
|
|
$this->assertEquals('default', $cfg->testFiles);
|
|
|
|
$this->expectException(\OutOfRangeException::class);
|
|
|
|
$cfg->keyDoesNotExist;
|
|
|
|
}
|
|
|
|
|
2021-01-11 15:38:35 +00:00
|
|
|
/**
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
public function testSiteOverride()
|
2021-01-11 15:38:35 +00:00
|
|
|
{
|
2021-01-14 07:58:14 +00:00
|
|
|
$cfg = new Settings();
|
2021-01-12 15:16:44 +00:00
|
|
|
$cfg = $this->appPath($cfg)->site('site')->load();
|
2021-01-14 07:58:14 +00:00
|
|
|
|
|
|
|
$this->assertEquals('default', $cfg->testFiles);
|
2021-01-14 16:48:41 +00:00
|
|
|
$this->assertEquals('site', $cfg->testFile);
|
2021-01-11 15:38:35 +00:00
|
|
|
$this->assertEquals(42, $cfg->answer);
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:16:44 +00:00
|
|
|
/**
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
public function testTestingOverride()
|
2021-01-11 10:58:40 +00:00
|
|
|
{
|
2021-01-14 07:58:14 +00:00
|
|
|
$cfg = new Settings();
|
|
|
|
$cfg = $this->appPath($cfg, 'testing')->load();
|
|
|
|
|
|
|
|
$this->assertEquals('testing', $cfg->testFiles);
|
2021-01-11 10:58:40 +00:00
|
|
|
$this->assertEquals(42, $cfg->answer);
|
2021-01-14 17:30:36 +00:00
|
|
|
|
|
|
|
return $cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testTestingOverride
|
|
|
|
*/
|
|
|
|
public function testTestingNumArrayOverride(Settings $cfg)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
0 => 'Index0_test',
|
|
|
|
1 => 'Index1_default',
|
|
|
|
2 => 'Index2_test',
|
|
|
|
3 => 'Index3_local',
|
|
|
|
4 => 'Index4_local',
|
|
|
|
5 => 'Index5_local',
|
|
|
|
6 => 'Index6_default',
|
|
|
|
7 => 'Index7_default',
|
|
|
|
10 => 'Index10_test'
|
|
|
|
];
|
|
|
|
|
|
|
|
$numArray = $cfg->numArray;
|
|
|
|
|
|
|
|
foreach ($numArray as $key => $value)
|
|
|
|
{
|
|
|
|
$this->assertEquals($data[$key], $value);
|
|
|
|
}
|
2021-01-11 10:58:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 15:16:44 +00:00
|
|
|
/**
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
public function testLocalOverride()
|
2021-01-11 10:58:40 +00:00
|
|
|
{
|
2021-01-14 07:58:14 +00:00
|
|
|
$cfg = new Settings();
|
2021-01-12 15:16:44 +00:00
|
|
|
$cfg = $this->appPath($cfg, 'localOverride')->load();
|
2021-01-11 10:58:40 +00:00
|
|
|
$this->assertEquals(42, $cfg->answer);
|
|
|
|
}
|
|
|
|
|
2021-01-12 16:53:42 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider fileNameData
|
|
|
|
*/
|
2021-01-14 07:58:14 +00:00
|
|
|
public function testBuildFileName(string $prefix, $type, string $site, string $expected)
|
2021-01-06 16:50:32 +00:00
|
|
|
{
|
|
|
|
$path = './config/';
|
|
|
|
|
2021-01-14 07:58:14 +00:00
|
|
|
$cfg = new Settings();
|
2021-01-12 16:53:42 +00:00
|
|
|
$cfg->appPath('./');
|
|
|
|
if ($prefix !== '') $cfg->prefix($prefix);
|
|
|
|
if ($site !== '')
|
|
|
|
{
|
|
|
|
$cfg->prefix();
|
|
|
|
$cfg->site($site);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($path.$expected, $cfg->buildFileName($type));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fileNameData()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// prefix, type, 'site', 'expected'
|
|
|
|
['', '', '', 'conf.php' ],
|
|
|
|
['', 'default', '', 'default.conf.php' ],
|
|
|
|
['', 'testing', '', 'testing.conf.php' ],
|
|
|
|
['MyPrefix', '', '', 'MyPrefix.conf.php' ],
|
|
|
|
['MyPrefix', 'default', '', 'MyPrefix.default.conf.php' ],
|
|
|
|
['MyPrefix', 'testing', '', 'MyPrefix.testing.conf.php' ],
|
|
|
|
['_', '', '', '_.conf.php' ],
|
|
|
|
['_', 'default', '', '_.default.conf.php' ],
|
|
|
|
['_', 'testing', '', '_.testing.conf.php' ],
|
|
|
|
['1', '', '', '1.conf.php' ],
|
|
|
|
['1', 'default', '', '1.default.conf.php' ],
|
|
|
|
['1', 'testing', '', '1.testing.conf.php' ],
|
|
|
|
['other', '', '', 'other.conf.php' ],
|
|
|
|
['other', 'default', '', 'other.default.conf.php' ],
|
|
|
|
['other', 'testing', '', 'other.testing.conf.php' ],
|
|
|
|
|
|
|
|
['', 0x01, '13', '13/conf.php' ],
|
|
|
|
['', 0x01, 'a_site', 'a_site/conf.php' ],
|
|
|
|
];
|
2021-01-06 16:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* jEdit buffer local properties {{{
|
2021-01-07 16:41:37 +00:00
|
|
|
* :folding=explicit:collapseFolds=1:
|
2021-01-06 16:50:32 +00:00
|
|
|
}}}*/
|