From 67d895e3057671d43456950406da1057093c1bc5 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sun, 16 Dec 2018 15:20:18 +0100 Subject: [PATCH] Fix deprecation for symfony/config 3.2+ --- src/DependencyInjection/Configuration.php | 10 +- .../DependencyInjection/ConfigurationTest.php | 99 +++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 tests/DependencyInjection/ConfigurationTest.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3b4d191f8..c8baa7fec 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -31,8 +31,14 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('sonata_doctrine_orm_admin', 'array'); + $treeBuilder = new TreeBuilder('sonata_doctrine_orm_admin'); + + // Keep compatibility with symfony/config < 4.2 + if (!\method_exists($treeBuilder, 'getRootNode')) { + $rootNode = $treeBuilder->root('sonata_doctrine_orm_admin'); + } else { + $rootNode = $treeBuilder->getRootNode(); + } $rootNode ->children() diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 000000000..01c3f6fa0 --- /dev/null +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\DoctrineORMAdminBundle\Tests; + +use PHPUnit\Framework\TestCase; +use Sonata\DoctrineORMAdminBundle\DependencyInjection\Configuration; +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; +use Symfony\Component\Config\Definition\Exception\InvalidTypeException; +use Symfony\Component\Config\Definition\Processor; + +class ConfigurationTest extends TestCase +{ + public function testDefaultOptions() + { + $config = $this->process([]); + + $this->assertNull($config['entity_manager']); + $this->assertTrue($config['audit']['force']); + $this->assertArraySubset(['@SonataDoctrineORMAdmin/Form/form_admin_fields.html.twig'], $config['templates']['form']); + $this->assertArraySubset(['@SonataDoctrineORMAdmin/Form/filter_admin_fields.html.twig'], $config['templates']['filter']); + $this->assertArrayNotHasKey('types', $config['templates']); + } + + public function testAuditForceWithInvalidFormat() + { + $this->expectException(InvalidTypeException::class); + + $this->process([[ + 'audit' => [ + 'force' => '1', + ], + ]]); + } + + public function testCustomTemplates() + { + $config = $this->process([[ + 'templates' => [ + 'form' => ['form.twig.html', 'form_extra.twig.html'], + 'filter' => ['filter.twig.html'], + 'types' => [ + 'list' => [ + 'array' => 'list_array.twig.html', + ], + 'show' => [ + 'array' => 'show_array.twig.html', + ], + ], + ], + ]]); + + $this->assertSame(['form.twig.html', 'form_extra.twig.html'], $config['templates']['form']); + $this->assertSame(['filter.twig.html'], $config['templates']['filter']); + $this->assertSame([ + 'list' => [ + 'array' => 'list_array.twig.html', + ], + 'show' => [ + 'array' => 'show_array.twig.html', + ], + ], $config['templates']['types']); + } + + public function testTemplateTypesWithInvalidValues() + { + $this->expectException(InvalidConfigurationException::class); + + $this->process([[ + 'templates' => [ + 'types' => [ + 'edit' => [], + ], + ], + ]]); + } + + /** + * Processes an array of configurations and returns a compiled version. + * + * @param array $configs An array of raw configurations + * + * @return array A normalized array + */ + protected function process($configs) + { + $processor = new Processor(); + + return $processor->processConfiguration(new Configuration(), $configs); + } +}