From 6d3158a5c73204d0be15c258ec9783154eb73880 Mon Sep 17 00:00:00 2001 From: Norbert Wagner Date: Tue, 12 Jan 2021 17:53:42 +0100 Subject: [PATCH] add dataProvider to test buildFileName and fix related errors --- src/Settings.php | 6 +++--- tests/SettingsTest.php | 48 +++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/Settings.php b/src/Settings.php index 7519d5c..533999d 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -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; diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 047e333..6c0a242 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -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' ], + ]; } }