add dataProvider to test buildFileName and fix related errors

This commit is contained in:
Norbert Wagner 2021-01-12 17:53:42 +01:00
parent 20f962b26d
commit 6d3158a5c7
2 changed files with 43 additions and 11 deletions

View File

@ -86,9 +86,9 @@ class Settings implements \Iterator
// prefix() {{{
/** {{{
*///}}}
public function prefix( string $prefix )
public function prefix( ?string $prefix=null )
{
$this->filePrefix = "$prefix.";
$this->filePrefix = (isset($prefix)) ? "$prefix." : '';
return $this;
}// }}}
@ -226,7 +226,7 @@ class Settings implements \Iterator
if ($type === self::SITE) $path .= $this->site.'/';
if (is_string( $type ))
if (is_string( $type ) &! empty( $type ))
{
$mode = "$type.";
$path = $this->pkgPath.$this->confDir;

View File

@ -27,6 +27,7 @@ use PHPUnit\Framework\TestCase;
class SettingsTest extends TestCase
{
public function testConstruct()
{
$cfg = new Settings();
@ -100,17 +101,48 @@ class SettingsTest extends TestCase
$this->assertEquals(42, $cfg->answer);
}
public function testBuildFileName()
/**
* @depends testConstruct
* @dataProvider fileNameData
*/
public function testBuildFileName(string $prefix, $type, string $site, string $expected, Settings $cfg)
{
$path = './config/';
$prefix = '';
$postfix = 'conf.php';
$type = '';
$defaultName = "$path$prefix$type$postfix";
$cfg = new Settings();
$this->assertEquals($defaultName, $cfg->buildFileName());
$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' ],
];
}
}