Skip to content

Commit

Permalink
test the Configuration (#63)
Browse files Browse the repository at this point in the history
* test the Configuration

* use PHPUnit 4.8.35 as minimum for forward compatibility with PHPUnit 6

* explicitly require matthiasnoback/symfony-config-test, use versions supporting Symfony 4

* require phpunit 5.2 for expectException()
  • Loading branch information
othillo authored Dec 20, 2017
1 parent e812c86 commit 38eee98
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
170 changes: 170 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <opensource@qandidate.com>
*
* 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'
);
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 38eee98

Please sign in to comment.