diff --git a/src/FeatureToggler.php b/src/FeatureToggler.php index eebcb89..82d13d5 100644 --- a/src/FeatureToggler.php +++ b/src/FeatureToggler.php @@ -34,15 +34,14 @@ public function __construct($configFile) */ public function isEnabled($key, $defaultValue = false) { - - $config = $this->config->get($key, $defaultValue); - - if (is_array($config) && $config !== $defaultValue) { - if (array_key_exists($defaultValue, $config)) { - return $config[$defaultValue]; + try { + if ($this->config->has($key)) { + return (bool)$this->config->get($key, $defaultValue); + } else { + return $defaultValue; } - return false; + } catch (Exception $e) { + return $defaultValue; } - return $config; } } diff --git a/tests/FeatureTogglerTest.php b/tests/FeatureTogglerTest.php index 789cb22..89baf1e 100644 --- a/tests/FeatureTogglerTest.php +++ b/tests/FeatureTogglerTest.php @@ -21,7 +21,7 @@ class FeatureTogglerTest extends PHPUnit_Framework_TestCase public static function setUpBeforeClass() { - self::$toggler = new FeatureToggler('src/config/config.php'); + self::$toggler = new FeatureToggler('tests/test_mass.php'); } /** @@ -31,15 +31,96 @@ public static function setUpBeforeClass() */ public function testIsEnabled($a, $b) { - $this->assertEquals($b, $a); -// $this->assertEquals($b, self::$toggler->isEnabled($a)); + $this->assertEquals($b, self::$toggler->isEnabled($a)); + } + + /** + * @param $a + * @param $b + * @param $c + * @dataProvider isEnabledProviderWithDefault + */ + public function testIsEnabledWithDefault($a, $b, $c) + { + $this->assertEquals($c, self::$toggler->isEnabled($a, $b)); + } + + /** + * @expectedException Davispeixoto\FeatureToggler\FeatureTogglerException + */ + public function testConstructorException() + { + $toggler = new FeatureToggler('non-existent/path/or/file.php'); + $toggler->isEnabled('foo'); } public function isEnabledProvider() { return [ - [true, true], - [false, false] + ['feature1', TRUE], + ['feature2', FALSE], + ['feature3', TRUE], + ['feature4', TRUE], + ['feature5', FALSE], + ['feature6', FALSE], + ['feature7', FALSE], + ['feature8', TRUE], + ['feature9', TRUE], + ['feature10', FALSE], + ['feature11', TRUE], + ['feature12', TRUE], + ['feature13', FALSE], + ['feature14.a', TRUE], + ['feature14.b', FALSE], + ['feature14.c', TRUE], + ['feature14.d', TRUE], + ['feature14.e', FALSE], + ['feature14.f', FALSE], + ['feature14.g', FALSE], + ['feature14.h', TRUE], + ['feature14.i', TRUE], + ['feature14.j', FALSE], + ['feature14.k', TRUE], + ['feature14.l', TRUE], + ['feature14.m', FALSE], + ['xptoFeat', FALSE], + ['feature1.a', FALSE] + ]; + } + + public function isEnabledProviderWithDefault() + { + return [ + ['feature1', FALSE, TRUE], + ['feature2', TRUE, FALSE], + ['feature3', FALSE, TRUE], + ['feature4', FALSE, TRUE], + ['feature5', TRUE, FALSE], + ['feature6', TRUE, FALSE], + ['feature7', TRUE, FALSE], + ['feature8', FALSE, TRUE], + ['feature9', FALSE, TRUE], + ['feature10', TRUE, FALSE], + ['feature11', FALSE, TRUE], + ['feature12', FALSE, TRUE], + ['feature13', TRUE, FALSE], + ['feature14.a', FALSE, TRUE], + ['feature14.b', TRUE, FALSE], + ['feature14.c', FALSE, TRUE], + ['feature14.d', FALSE, TRUE], + ['feature14.e', TRUE, FALSE], + ['feature14.f', TRUE, FALSE], + ['feature14.g', TRUE, FALSE], + ['feature14.h', FALSE, TRUE], + ['feature14.i', FALSE, TRUE], + ['feature14.j', TRUE, FALSE], + ['feature14.k', FALSE, TRUE], + ['feature14.l', FALSE, TRUE], + ['feature14.m', TRUE, FALSE], + ['xptoFeat', TRUE, TRUE], + ['feature1.a', TRUE, TRUE], + ['xptoFeat', FALSE, FALSE], + ['feature1.a', FALSE, FALSE] ]; } } diff --git a/tests/Test Mass.csv b/tests/Test Mass.csv new file mode 100644 index 0000000..0d81f71 --- /dev/null +++ b/tests/Test Mass.csv @@ -0,0 +1,67 @@ +Tests Design,,,,,,, +Scenario ID,feature,value,default value,expected,comments,, +1,exists,boolean,empty,value,,, +2,exists,boolean,boolean,value,,, +4,non-exists,N/A,empty,FALSE,,, +5,non-exists,N/A,boolean,default value,,, +,,,,,,, +Tests Mass Implementation,,,,,,, +Scenario ID,feature,value,default,expected,comments,, +1,feature1,TRUE,empty,TRUE,,,"""feature1""TRUE," +1,feature2,FALSE,empty,FALSE,,,"""feature2"" => FALSE," +1,feature3,1,empty,TRUE,,,"""feature3"" => 1," +1,feature4,-1,empty,TRUE,,,"""feature4"" => -1," +1,feature5,0,empty,FALSE,,,"""feature5"" => 0," +1,feature6,"""""",empty,FALSE,,,"""feature6"" => """"," +1,feature7,NULL,empty,FALSE,,,"""feature7"" => NULL," +1,feature8,"""foo""",empty,TRUE,,,"""feature8"" => ""foo""," +1,feature9,"""0.00""",empty,TRUE,,,"""feature9"" => ""0.00""," +1,feature10,[],empty,FALSE,,,"""feature10"" => []," +1,feature11,[0],empty,TRUE,,,"""feature11"" => [0]," +1,feature12,"""false""",empty,TRUE,,,"""feature12"" => ""false""," +1,feature13,"(float) ""0.00""",empty,FALSE,,,"""feature13"" => (float) ""0.00""," +1,feature14.a,TRUE,empty,TRUE,,,"""feature14.a"" => TRUE," +1,feature14.b,FALSE,empty,FALSE,,,"""feature14.b"" => FALSE," +1,feature14.c,1,empty,TRUE,,,"""feature14.c"" => 1," +1,feature14.d,-1,empty,TRUE,,,"""feature14.d"" => -1," +1,feature14.e,0,empty,FALSE,,,"""feature14.e"" => 0," +1,feature14.f,"""""",empty,FALSE,,,"""feature14.f"" => """"," +1,feature14.g,NULL,empty,FALSE,,,"""feature14.g"" => NULL," +1,feature14.h,"""foo""",empty,TRUE,,,"""feature14.h"" => ""foo""," +1,feature14.i,"""0.00""",empty,TRUE,,,"""feature14.i"" => ""0.00""," +1,feature14.j,[],empty,FALSE,,,"""feature14.j"" => []," +1,feature14.k,[0],empty,TRUE,,,"""feature14.k"" => [0]," +1,feature14.l,"""false""",empty,TRUE,,,"""feature14.l"" => ""false""," +1,feature14.m,"(float) ""0.00""",empty,FALSE,,,"""feature14.m"" => (float) ""0.00""," +2,feature1,TRUE,FALSE,TRUE,,, +2,feature2,FALSE,TRUE,FALSE,,, +2,feature3,1,FALSE,TRUE,,, +2,feature4,-1,FALSE,TRUE,,, +2,feature5,0,TRUE,FALSE,,, +2,feature6,"""""",TRUE,FALSE,,, +2,feature7,NULL,TRUE,FALSE,,, +2,feature8,"""foo""",FALSE,TRUE,,, +2,feature9,"""0.00""",FALSE,TRUE,,, +2,feature10,[],TRUE,FALSE,,, +2,feature11,[0],FALSE,TRUE,,, +2,feature12,"""false""",FALSE,TRUE,,, +2,feature13,"(float) ""0.00""",TRUE,FALSE,,, +2,feature14.a,TRUE,FALSE,TRUE,,, +2,feature14.b,FALSE,TRUE,FALSE,,, +2,feature14.c,1,FALSE,TRUE,,, +2,feature14.d,-1,FALSE,TRUE,,, +2,feature14.e,0,TRUE,FALSE,,, +2,feature14.f,"""""",TRUE,FALSE,,, +2,feature14.g,NULL,TRUE,FALSE,,, +2,feature14.h,"""foo""",FALSE,TRUE,,, +2,feature14.i,"""0.00""",FALSE,TRUE,,, +2,feature14.j,[],TRUE,FALSE,,, +2,feature14.k,[0],FALSE,TRUE,,, +2,feature14.l,"""false""",FALSE,TRUE,,, +2,feature14.m,"(float) ""0.00""",TRUE,FALSE,,, +3,xptoFeat,,empty,FALSE,,, +3,feature1.a,,empty,FALSE,,, +3,xptoFeat,,TRUE,TRUE,,, +3,feature1.a,,TRUE,TRUE,,, +3,xptoFeat,,FALSE,FALSE,,, +3,feature1.a,,FALSE,FALSE,,, \ No newline at end of file diff --git a/tests/test_mass.php b/tests/test_mass.php new file mode 100644 index 0000000..fa506cb --- /dev/null +++ b/tests/test_mass.php @@ -0,0 +1,38 @@ + TRUE, + 'feature2' => FALSE, + 'feature3' => 1, + 'feature4' => -1, + 'feature5' => 0, + 'feature6' => '', + 'feature7' => NULL, + 'feature8' => 'foo', + 'feature9' => '0.00', + 'feature10' => [], + 'feature11' => [0], + 'feature12' => 'false', + 'feature13' => 0.00, + 'feature14' => [ + 'a' => TRUE, + 'b' => FALSE, + 'c' => 1, + 'd' => -1, + 'e' => 0, + 'f' => '', + 'g' => NULL, + 'h' => 'foo', + 'i' => '0.00', + 'j' => [], + 'k' => [0], + 'l' => 'false', + 'm' => 0.00 + ] +];