|
| 1 | +# SymfonyConfigTest |
| 2 | + |
| 3 | +*By Matthias Noback* |
| 4 | + |
| 5 | +Writing configuration classes using the [Symfony Config |
| 6 | +Component](http://symfony.com/doc/current/components/config/definition.html) can be quite hard. To help you verify the |
| 7 | +validity of the resulting config node tree, this library provides a PHPUnit test case and some custom assertions. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +Using Composer: |
| 12 | + |
| 13 | + php composer.phar require matthiasnoback/symfony-config-test 0.* |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +Create a test case and extend from ``Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase``. Then implement |
| 18 | +``getConfiguration()``: |
| 19 | + |
| 20 | + <?php |
| 21 | + |
| 22 | + class ConfigurationTest extends AbstractConfigurationTestCase |
| 23 | + { |
| 24 | + protected function getConfiguration() |
| 25 | + { |
| 26 | + return new Configuration(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | +Let's assume the ``Configuration`` class you want to test looks like this: |
| 31 | + |
| 32 | + <?php |
| 33 | + |
| 34 | + use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
| 35 | + use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 36 | + |
| 37 | + class ConfigurationWithRequiredValue implements ConfigurationInterface |
| 38 | + { |
| 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 | + } |
| 54 | + } |
| 55 | + |
| 56 | +When you provide an empty array as the values for this configuration, you would expect an exception since the |
| 57 | +``required_value`` node is required. You can assert that a given set of configuration values is invalid using the |
| 58 | +``assertConfigurationIsInvalid()`` method: |
| 59 | + |
| 60 | + <?php |
| 61 | + |
| 62 | + class ConfigurationTest extends AbstractConfigurationTestCase |
| 63 | + { |
| 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 | + } |
| 74 | + |
| 75 | +You may also want to verify that after processing an array of configuration values the result will be as expected: |
| 76 | + |
| 77 | + <?php |
| 78 | + |
| 79 | + class ConfigurationTest extends AbstractConfigurationTestCase |
| 80 | + { |
| 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 | + } |
| 90 | + } |
| 91 | + |
| 92 | +Please note: the first argument of each of the ``assert*`` methods is an *array of arrays*. The extra nesting level |
| 93 | +allows you to test the merge process. See also the section [Merging |
| 94 | +options](http://symfony.com/doc/current/components/config/definition.html#merging-options) of the Config Component |
| 95 | +documentation. |
0 commit comments