2017 © Pedro Peláez
 

library symfony-config-test

Library for testing user classes related to the Symfony Config Component

image

hostnet/symfony-config-test

Library for testing user classes related to the Symfony Config Component

  • Thursday, February 8, 2018
  • by hboomsma
  • Repository
  • 4 Watchers
  • 0 Stars
  • 1,658 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 25 Forks
  • 0 Open issues
  • 30 Versions
  • 54 % Grown

The README.md

IMPORTANT: This package is no longer maintained, please consider using the original source: https://github.com/SymfonyTest/SymfonyConfigTest., (*1)


SymfonyConfigTest

By Matthias Noback, (*2)

Build Status, (*3)

Writing configuration classes using the Symfony Config Component can be quite hard. To help you verify the validity of the resulting config node tree, this library provides a PHPUnit test case and some custom assertions., (*4)

Installation

Using Composer:, (*5)

php composer.phar require --dev matthiasnoback/symfony-config-test

Usage

Create a test case and use the trait from Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait. Then implement getConfiguration():, (*6)

<?php

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
    use ConfigurationTestCaseTrait;

    protected function getConfiguration()
    {
        return new Configuration();
    }
}

Test invalid configuration values

Let's assume the Configuration class you want to test looks like this:, (*7)

<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class ConfigurationWithRequiredValue implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();

        $rootNode = $treeBuilder->root('root');
        $rootNode
            ->isRequired()
            ->children()
                ->scalarNode('required_value')
                    ->isRequired()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

When you provide an empty array as the value for this configuration, you would expect an exception since the required_value node is required. You can assert that a given set of configuration values is invalid using the assertConfigurationIsInvalid() method:, (*8)

<?php

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
    use ConfigurationTestCaseTrait;

    public function testValuesAreInvalidIfRequiredValueIsNotProvided()
    {
        $this->assertConfigurationIsInvalid(
            array(
                array() // no values at all
            ),
            'required_value' // (part of) the expected exception message - optional
        );
    }
}

Test processed configuration values

You may also want to verify that after processing an array of configuration values the result will be as expected:, (*9)

<?php

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
    use ConfigurationTestCaseTrait;

    public function testProcessedValueContainsRequiredValue()
    {
        $this->assertProcessedConfigurationEquals(array(
            array('required_value' => 'first value'),
            array('required_value' => 'last value')
        ), array(
            'required_value'=> 'last value'
        ));
    }
}

Please note: the first argument of each of the assert* methods is an array of arrays. The extra nesting level allows you to test the merge process. See also the section Merging options of the Config Component documentation., (*10)

Test a subset of the configuration tree

Using this library it's possible to test just one branch of your configuration tree. Consider the following node tree definition, which contains the branches array_node_1 and array_node_2:, (*11)

<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class ConfigurationWithTwoBranches implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();

        $rootNode = $treeBuilder->root('root');
        $rootNode
            ->children()
                ->arrayNode('array_node_1')
                    ->isRequired()
                    ->children()
                        ->scalarNode('required_value_1')
                            ->isRequired()
                        ->end()
                    ->end()
                ->end()
                ->arrayNode('array_node_2')
                    ->isRequired()
                    ->children()
                        ->scalarNode('required_value_2')
                            ->isRequired()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

If you want to test, for instance, only the array_node_1 branch from the example below, and ignore the array_node_2, provide array_node_1 as the argument for the $breadcrumbPath parameter of the test helper functions, for example:, (*12)

/**
 * @test
 */
public function processed_configuration_for_array_node_1()
{
    $this->assertProcessedConfigurationEquals(
        array(
            array('array_node_1' => array('required_value_1' => 'original value')),
            array('array_node_1' => array('required_value_1' => 'final value'))
        ),
        array(
            'array_node_1' => array(
                'required_value_1' => 'final value'
            )
        ),
        // the path of the nodes you want to focus on in this test:
        'array_node_1'
    );
}

This would trigger no validation errors for any value in the array_node_2 branch., (*13)

Note that the $breadcrumbPath can be even more specific, e.g. "doctrine.orm" (which would skip configuration processing for branch "doctrine.dbal", etc.)., (*14)

Also note that you can only traverse over array nodes using the . in the breadcrumb path. The last part of the breadcrumb path can be any other type of node., (*15)

Test a subset of the prototyped configuration tree

You can traverse through prototype array nodes using * as its name in the breadcrumb path., (*16)

<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class PrototypedConfiguration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();

        $rootNode = $treeBuilder->root('root');
        $rootNode
            ->children()
                ->arrayNode('array_node')
                    ->useAttributeAsKey('name')
                    ->prototype('array')
                        ->children()
                            ->scalarNode('default_value')->cannotBeEmpty()->defaultValue('foobar')->end()
                            ->scalarNode('required_value')->isRequired()->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

If you want to test whether default_value is set to foobar by default, but don't want the test to be affected by requirements on required_value node, you can define its path as array_node.*.default_value, for example:, (*17)

/**
 * @test
 */
public function processed_configuration_for_array_node_1()
{
    $this->assertProcessedConfigurationEquals(
        array(
            array('array_node' => array('prototype_name' => null)),
        ),
        array(
            'array_node' => array(
                'prototype_name' => array(
                    'default_value' => 'foobar'
                )
            )
        ),
        // the path of the nodes you want to focus on in this test:
        'array_node.*.default_value'
    );
}

Version Guidance

Version Released Status
3.x Nov 30, 2017 Latest
2.x Jun 18, 2016 EOL
1.x Oct 12, 2014 EOL

The Versions

08/02 2018

dev-master

9999999-dev

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

08/02 2018

v3.2.0

3.2.0.0

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

04/02 2018

dev-analysis-86oEyn

dev-analysis-86oEyn https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

30/11 2017

v3.1.0

3.1.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

21/11 2017

v2.x-dev

2.9999999.9999999.9999999-dev https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

21/11 2017

v2.2.0

2.2.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

08/02 2017

v3.0.1

3.0.1.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

08/02 2017

v3.0.0

3.0.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

08/02 2017

dev-ng

dev-ng https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

30/11 2016

v2.1.0

2.1.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

19/10 2016

v2.0.1

2.0.1.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

18/06 2016

v2.0.0

2.0.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

phpunit config symfony

25/11 2015

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

25/11 2015

v1.4.0

1.4.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

12/11 2015

v1.3.1

1.3.1.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

29/09 2015

v1.3.0

1.3.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

phpunit config symfony

24/09 2015

v1.2.3

1.2.3.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

phpunit config symfony

23/09 2015

v1.2.2

1.2.2.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

09/09 2015

1.2.1

1.2.1.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

20/04 2015

1.2.0

1.2.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

15/04 2015

dev-partial_node_testing

dev-partial_node_testing https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

10/04 2015

1.1.0

1.1.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

12/10 2014

v0.4.0

0.4.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

12/10 2014

1.0.0

1.0.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

10/09 2014

v0.3.0

0.3.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

26/07 2014

v0.2.1

0.2.1.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

26/07 2014

v0.2.0

0.2.0.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

26/07 2014

v0.1.2

0.1.2.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony

20/10 2013

v0.1.1

0.1.1.0 https://github.com/matthiasnoback/SymfonyConfigTest

Library for testing user classes related to the Symfony Config Component

  Sources   Download

MIT

The Requires

 

The Development Requires

phpunit config symfony