diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2adb98d..44f0378 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -75,7 +75,7 @@ public function getConfigTreeBuilder() ->end() ->validate() ->ifTrue(function ($v) { - if ($v['persistence'] === 'factory') { + if (isset($v['persistence']) && $v['persistence'] === 'factory') { return ! isset($v['collection_factory']['service_id'], $v['collection_factory']['method']); } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 0000000..68c7f85 --- /dev/null +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,170 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Qandidate\Bundle\ToggleBundle\DependencyInjection; + +use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; + +class ConfigurationTest extends \PHPUnit_Framework_TestCase +{ + use ConfigurationTestCaseTrait; + + /** + * {@inheritdoc} + */ + protected function getConfiguration() + { + return new Configuration(); + } + + /** + * @test + */ + public function it_accepts_empty_configuration_and_configures_defaults() + { + $this->assertProcessedConfigurationEquals( + [ + [], + ], + [ + 'persistence' => 'in_memory', + 'context_factory' => null, + 'redis_namespace' => 'toggle_%kernel.environment%', + 'redis_client' => null, + 'toggles' => [], + ] + ); + } + + /** + * @test + */ + public function it_defaults_to_in_memory_persistence() + { + $this->assertProcessedConfigurationEquals( + [ + [], + ], + [ + 'persistence' => 'in_memory', + ], + 'persistence' + ); + } + + /** + * @test + */ + public function it_configures_toggles_without_conditions() + { + $this->assertProcessedConfigurationEquals( + [ + [ + 'toggles' => [ + 'always-active-feature' => [ + 'name' => 'always-active-feature', + 'status' => 'always-active', + ], + 'inactive-feature' => [ + 'name' => 'inactive-feature', + 'status' => 'inactive', + ], + ] + ], + ], + [ + 'toggles' => [ + 'always_active_feature' => [ + 'name' => 'always-active-feature', + 'status' => 'always-active', + 'conditions' => [], + ], + 'inactive_feature' => [ + 'name' => 'inactive-feature', + 'status' => 'inactive', + 'conditions' => [], + ], + ], + ], + 'toggles' + ); + } + + /** + * @test + */ + public function it_configures_toggles_with_conditions() + { + $this->assertProcessedConfigurationEquals( + [ + [ + 'toggles' => [ + 'conditionally-active' => [ + 'name' => 'conditionally-active', + 'status' => 'conditionally-active', + 'conditions' => [ + [ + 'name' => 'operator-condition', + 'key' => 'user_id', + 'operator' => [ + 'name' => 'greater-than', + 'value' => 42, + ], + ], + ], + ], + ], + ], + ], + [ + 'toggles' => [ + 'conditionally_active' => [ + 'name' => 'conditionally-active', + 'status' => 'conditionally-active', + 'conditions' => [ + [ + 'name' => 'operator-condition', + 'key' => 'user_id', + 'operator' => [ + 'name' => 'greater-than', + 'value' => 42, + ], + ], + ], + ], + ], + ], + 'toggles' + ); + } + + /** + * @test + */ + public function it_requires_collection_factory_to_be_set_when_persistence_is_factory() + { + $this->expectException(InvalidConfigurationException::class); + $this->expectExceptionMessage('Invalid configuration for path "qandidate_toggle": When choosing "factory" persistence make sure you set "collection_factory.service_id" and "collection_factory.method"'); + + $this->assertProcessedConfigurationEquals( + [ + [ + 'persistence' => 'factory', + ], + ], + [ + 'persistence' => 'factory', + ], + 'persistence' + ); + } +} diff --git a/composer.json b/composer.json index 57e79f3..d8c9b89 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,9 @@ "symfony/browser-kit": "^2.7||^3.0||^4.0", "twig/twig": "~1.16,>=1.16.2", "symfony/twig-bundle": "^2.7||^3.0||^4.0", - "phpunit/phpunit": "^5.0 || ^4.8.10", - "matthiasnoback/symfony-dependency-injection-test": "^1.1" + "phpunit/phpunit": "^5.2", + "matthiasnoback/symfony-dependency-injection-test": "^1.2", + "matthiasnoback/symfony-config-test": "^2.2" }, "suggest": { "twig/twig": "For using the twig helper"