Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation for symfony/config 4.2+ #871

Merged
merged 1 commit into from Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -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');
greg0ire marked this conversation as resolved.
Show resolved Hide resolved

// 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()
Expand Down
99 changes: 99 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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);
}
}