Skip to content

Fix TreeBuilder in Symfony 4.2 #692

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('enqueue_async_event_dispatcher');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$tb = new TreeBuilder('enqueue_async_event_dispatcher');
$rootNode = $tb->getRootNode();
} else {
$tb = new TreeBuilder();
$rootNode = $tb->root('enqueue_async_event_dispatcher');
}

$rootNode->children()
->scalarNode('context_service')->isRequired()->cannotBeEmpty()->end()
Expand Down
9 changes: 7 additions & 2 deletions pkg/enqueue-bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ public function __construct(bool $debug)

public function getConfigTreeBuilder(): TreeBuilder
{
$tb = new TreeBuilder();
$rootNode = $tb->root('enqueue');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$tb = new TreeBuilder('enqueue');
$rootNode = $tb->getRootNode();
} else {
$tb = new TreeBuilder();
$rootNode = $tb->root('enqueue');
}

$rootNode
->requiresAtLeastOneElement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Interop\Queue\ConnectionFactory;
use Interop\Queue\Context;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
Expand All @@ -37,8 +38,7 @@ public function testThrowIfEmptyNameGivenOnConstruction()

public function testShouldAllowAddConfigurationAsStringDsn()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -59,8 +59,7 @@ public function testShouldAllowAddConfigurationAsStringDsn()
*/
public function testShouldAllowAddConfigurationAsDsnWithoutSlashes()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -76,8 +75,7 @@ public function testShouldAllowAddConfigurationAsDsnWithoutSlashes()

public function testShouldSetNullTransportIfNullGiven()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -93,8 +91,7 @@ public function testShouldSetNullTransportIfNullGiven()

public function testShouldSetNullTransportIfEmptyStringGiven()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -110,8 +107,7 @@ public function testShouldSetNullTransportIfEmptyStringGiven()

public function testShouldSetNullTransportIfEmptyArrayGiven()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -127,8 +123,7 @@ public function testShouldSetNullTransportIfEmptyArrayGiven()

public function testThrowIfEmptyDsnGiven()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -140,8 +135,7 @@ public function testThrowIfEmptyDsnGiven()

public function testThrowIfFactoryClassAndFactoryServiceSetAtTheSameTime()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -159,8 +153,7 @@ public function testThrowIfFactoryClassAndFactoryServiceSetAtTheSameTime()

public function testThrowIfConnectionFactoryClassUsedWithFactoryClassAtTheSameTime()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());

Expand All @@ -178,8 +171,7 @@ public function testThrowIfConnectionFactoryClassUsedWithFactoryClassAtTheSameTi

public function testThrowIfConnectionFactoryClassUsedWithFactoryServiceAtTheSameTime()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());
$processor = new Processor();
Expand All @@ -196,8 +188,7 @@ public function testThrowIfConnectionFactoryClassUsedWithFactoryServiceAtTheSame

public function testShouldAllowSetFactoryClass()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());
$processor = new Processor();
Expand All @@ -214,8 +205,7 @@ public function testShouldAllowSetFactoryClass()

public function testShouldAllowSetFactoryService()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());
$processor = new Processor();
Expand All @@ -232,8 +222,7 @@ public function testShouldAllowSetFactoryService()

public function testShouldAllowSetConnectionFactoryClass()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());
$processor = new Processor();
Expand All @@ -250,8 +239,7 @@ public function testShouldAllowSetConnectionFactoryClass()

public function testThrowIfExtraOptionGiven()
{
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');
list($tb, $rootNode) = $this->getRootNode();

$rootNode->append(TransportFactory::getConfiguration());
$processor = new Processor();
Expand Down Expand Up @@ -464,4 +452,20 @@ public function testThrowIfBuildRpcClientCalledButContextServiceDoesNotExist()
$this->expectExceptionMessage('The service "enqueue.transport.default.context" does not exist.');
$transport->buildRpcClient($container, []);
}

/**
* @return [TreeBuilder, NodeDefinition]
*/
private function getRootNode(): array
{
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$tb = new TreeBuilder('foo');

return [$tb, $tb->getRootNode()];
}

$tb = new TreeBuilder();

return [$tb, $tb->root('foo')];
}
}
9 changes: 7 additions & 2 deletions pkg/simple-client/SimpleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,13 @@ public function build(array $configs): void

private function createConfiguration(): NodeInterface
{
$tb = new TreeBuilder();
$rootNode = $tb->root('enqueue');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$tb = new TreeBuilder('enqueue');
$rootNode = $tb->getRootNode();
} else {
$tb = new TreeBuilder();
$rootNode = $tb->root('enqueue');
}

$rootNode
->beforeNormalization()
Expand Down