|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
| 16 | +use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition; |
| 17 | +use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
16 | 18 | use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
|
17 | 19 | use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
|
18 | 20 | use Symfony\Component\Config\Definition\Processor;
|
@@ -357,6 +359,85 @@ public function testCannotBeEmptyOnConcreteNode()
|
357 | 359 | $node->getNode()->finalize([]);
|
358 | 360 | }
|
359 | 361 |
|
| 362 | + public function testFindShouldThrowExceptionIfNodeDoesNotExistInRootNode() |
| 363 | + { |
| 364 | + $this->expectException(\RuntimeException::class); |
| 365 | + $this->expectExceptionMessage('Node with name "child" does not exist in the current node "root".'); |
| 366 | + |
| 367 | + $rootNode = new ArrayNodeDefinition('root'); |
| 368 | + $rootNode |
| 369 | + ->children() |
| 370 | + ->arrayNode('social_media_channels')->end() |
| 371 | + ->end() |
| 372 | + ; |
| 373 | + |
| 374 | + $rootNode->find('child'); |
| 375 | + } |
| 376 | + |
| 377 | + public function testFindShouldHandleComplexConfigurationProperly() |
| 378 | + { |
| 379 | + $rootNode = new ArrayNodeDefinition('root'); |
| 380 | + $rootNode |
| 381 | + ->children() |
| 382 | + ->arrayNode('social_media_channels') |
| 383 | + ->children() |
| 384 | + ->booleanNode('enable')->end() |
| 385 | + ->arrayNode('twitter')->end() |
| 386 | + ->arrayNode('facebook')->end() |
| 387 | + ->arrayNode('instagram') |
| 388 | + ->children() |
| 389 | + ->booleanNode('enable')->end() |
| 390 | + ->arrayNode('accounts')->end() |
| 391 | + ->end() |
| 392 | + ->end() |
| 393 | + ->end() |
| 394 | + ->append( |
| 395 | + $mailerNode = (new ArrayNodeDefinition('mailer')) |
| 396 | + ->children() |
| 397 | + ->booleanNode('enable')->end() |
| 398 | + ->arrayNode('transports')->end() |
| 399 | + ->end() |
| 400 | + ) |
| 401 | + ->end() |
| 402 | + ; |
| 403 | + |
| 404 | + $this->assertNode('social_media_channels', ArrayNodeDefinition::class, $rootNode->find('social_media_channels')); |
| 405 | + $this->assertNode('enable', BooleanNodeDefinition::class, $rootNode->find('social_media_channels.enable')); |
| 406 | + $this->assertNode('twitter', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.twitter')); |
| 407 | + $this->assertNode('facebook', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.facebook')); |
| 408 | + $this->assertNode('instagram', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.instagram')); |
| 409 | + $this->assertNode('enable', BooleanNodeDefinition::class, $rootNode->find('social_media_channels.instagram.enable')); |
| 410 | + $this->assertNode('accounts', ArrayNodeDefinition::class, $rootNode->find('social_media_channels.instagram.accounts')); |
| 411 | + |
| 412 | + $this->assertNode('enable', BooleanNodeDefinition::class, $mailerNode->find('enable')); |
| 413 | + $this->assertNode('transports', ArrayNodeDefinition::class, $mailerNode->find('transports')); |
| 414 | + } |
| 415 | + |
| 416 | + public function testFindShouldWorkProperlyForNonDefaultPathSeparator() |
| 417 | + { |
| 418 | + $rootNode = new ArrayNodeDefinition('root'); |
| 419 | + $rootNode |
| 420 | + ->setPathSeparator('.|') |
| 421 | + ->children() |
| 422 | + ->arrayNode('mailer.configuration') |
| 423 | + ->children() |
| 424 | + ->booleanNode('enable')->end() |
| 425 | + ->arrayNode('transports')->end() |
| 426 | + ->end() |
| 427 | + ->end() |
| 428 | + ; |
| 429 | + |
| 430 | + $this->assertNode('mailer.configuration', ArrayNodeDefinition::class, $rootNode->find('mailer.configuration')); |
| 431 | + $this->assertNode('enable', BooleanNodeDefinition::class, $rootNode->find('mailer.configuration.|enable')); |
| 432 | + $this->assertNode('transports', ArrayNodeDefinition::class, $rootNode->find('mailer.configuration.|transports')); |
| 433 | + } |
| 434 | + |
| 435 | + protected function assertNode(string $expectedName, string $expectedType, NodeDefinition $actualNode): void |
| 436 | + { |
| 437 | + $this->assertInstanceOf($expectedType, $actualNode); |
| 438 | + $this->assertSame($expectedName, $this->getField($actualNode, 'name')); |
| 439 | + } |
| 440 | + |
360 | 441 | protected function getField($object, $field)
|
361 | 442 | {
|
362 | 443 | $reflection = new \ReflectionProperty($object, $field);
|
|
0 commit comments