Util_Settings/tests/SettingsTest.php

151 lines
No EOL
3.8 KiB
PHP

<?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
{
public function testConstruct()
{
$cfg = new Settings();
$this->assertNotEmpty($cfg);
return $cfg;
}
/**
* @depends testConstruct
*/
public function testLoadNoFile(Settings $cfg)
{
$this->expectException(\ErrorException::class);
$cfg->load();
}
/**
*/
private function appPath(Settings $cfg, ?string $dir='')
{
if (isset($dir)) $dir .= '/';
$path = dirname(__FILE__).'/cfg/'.$dir;
$cfg->appPath($path);
return $cfg;
}
/**
* @depends testConstruct
*/
public function testLoad(Settings $cfg)
{
$cfg = $this->appPath($cfg)->load();
$this->assertNotEmpty($cfg);
return $cfg;
}
/**
* @depends testLoad
*/
public function testMode(Settings $cfg)
{
$this->assertEquals('test', $cfg->mode);
}
/**
* @depends testConstruct
*/
public function testSiteOverride(Settings $cfg)
{
$cfg = $this->appPath($cfg)->site('site')->load();
$this->assertEquals(42, $cfg->answer);
}
/**
* @depends testConstruct
*/
public function testTestingOverride(Settings $cfg)
{
$cfg = $this->appPath($cfg, 'testingOverride')->load();
$this->assertEquals(42, $cfg->answer);
}
/**
* @depends testConstruct
*/
public function testLocalOverride(Settings $cfg)
{
$cfg = $this->appPath($cfg, 'localOverride')->load();
$this->assertEquals(42, $cfg->answer);
}
/**
* @depends testConstruct
* @dataProvider fileNameData
*/
public function testBuildFileName(string $prefix, $type, string $site, string $expected, Settings $cfg)
{
$path = './config/';
$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' ],
];
}
}
/* jEdit buffer local properties {{{
* :folding=explicit:collapseFolds=1:
}}}*/