22
33* By Matthias Noback*
44
5+ [ ![ Build Status] ( https://secure.travis-ci.org/matthiasnoback/SymfonyConfigTest.png )] ( http://travis-ci.org/matthiasnoback/SymfonyConfigTest )
6+
57Writing configuration classes using the [ Symfony Config
68Component] ( http://symfony.com/doc/current/components/config/definition.html ) can be quite hard. To help you verify the
79validity of the resulting config node tree, this library provides a PHPUnit test case and some custom assertions.
@@ -17,6 +19,7 @@ Using Composer:
1719Create a test case and extend from `` Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase `` . Then implement
1820`` getConfiguration() `` :
1921
22+ ``` php
2023 <?php
2124
2225 class ConfigurationTest extends AbstractConfigurationTestCase
@@ -26,68 +29,75 @@ Create a test case and extend from ``Matthias\SymfonyConfigTest\PhpUnit\Abstract
2629 return new Configuration();
2730 }
2831 }
32+ ```
2933
3034Let's assume the `` Configuration `` class you want to test looks like this:
3135
32- <?php
36+ ``` php
37+ <?php
3338
34- use Symfony\Component\Config\Definition\Builder\TreeBuilder;
35- use Symfony\Component\Config\Definition\ConfigurationInterface;
39+ use Symfony\Component\Config\Definition\Builder\TreeBuilder;
40+ use Symfony\Component\Config\Definition\ConfigurationInterface;
3641
37- class ConfigurationWithRequiredValue implements ConfigurationInterface
42+ class ConfigurationWithRequiredValue implements ConfigurationInterface
43+ {
44+ public function getConfigTreeBuilder()
3845 {
39- public function getConfigTreeBuilder()
40- {
41- $treeBuilder = new TreeBuilder();
42-
43- $rootNode = $treeBuilder->root('root');
44- $rootNode
45- ->isRequired()
46- ->children()
47- ->scalarNode('required_value')
48- ->isRequired()
49- ->end()
50- ->end();
51-
52- return $treeBuilder;
53- }
46+ $treeBuilder = new TreeBuilder();
47+
48+ $rootNode = $treeBuilder->root('root');
49+ $rootNode
50+ ->isRequired()
51+ ->children()
52+ ->scalarNode('required_value')
53+ ->isRequired()
54+ ->end()
55+ ->end();
56+
57+ return $treeBuilder;
5458 }
59+ }
60+ ```
5561
5662When you provide an empty array as the values for this configuration, you would expect an exception since the
5763`` required_value `` node is required. You can assert that a given set of configuration values is invalid using the
5864`` assertConfigurationIsInvalid() `` method:
5965
60- <?php
66+ ``` php
67+ <?php
6168
62- class ConfigurationTest extends AbstractConfigurationTestCase
69+ class ConfigurationTest extends AbstractConfigurationTestCase
70+ {
71+ public function testValuesAreInvalidIfRequiredValueIsNotProvided()
6372 {
64- public function testValuesAreInvalidIfRequiredValueIsNotProvided()
65- {
66- $this->assertConfigurationIsInvalid(
67- array(
68- array() // no values at all
69- ),
70- 'required_value' // (part of) the expected exception message - optional
71- );
72- }
73+ $this->assertConfigurationIsInvalid(
74+ array(
75+ array() // no values at all
76+ ),
77+ 'required_value' // (part of) the expected exception message - optional
78+ );
7379 }
80+ }
81+ ```
7482
7583You may also want to verify that after processing an array of configuration values the result will be as expected:
7684
77- <?php
85+ ``` php
86+ <?php
7887
79- class ConfigurationTest extends AbstractConfigurationTestCase
88+ class ConfigurationTest extends AbstractConfigurationTestCase
89+ {
90+ public function testProcessedValueContainsRequiredValue()
8091 {
81- public function testProcessedValueContainsRequiredValue()
82- {
83- $this->assertProcessedConfigurationEquals(array(
84- array('required_value' => 'first value'),
85- array('required_value' => 'last value')
86- ), array(
87- 'required_value'=> 'last value'
88- ));
89- }
92+ $this->assertProcessedConfigurationEquals(array(
93+ array('required_value' => 'first value'),
94+ array('required_value' => 'last value')
95+ ), array(
96+ 'required_value'=> 'last value'
97+ ));
9098 }
99+ }
100+ ```
91101
92102Please note: the first argument of each of the `` assert* `` methods is an * array of arrays* . The extra nesting level
93103allows you to test the merge process. See also the section [ Merging
0 commit comments