|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Config\Tests\Definition\Builder; |
13 | 13 |
|
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\Attributes\Group; |
| 16 | +use PHPUnit\Framework\Attributes\IgnoreDeprecations; |
14 | 17 | use PHPUnit\Framework\TestCase; |
15 | 18 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
| 19 | +use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition; |
16 | 20 | use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
| 21 | +use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition; |
| 22 | +use Symfony\Component\Config\Definition\Builder\StringNodeDefinition; |
| 23 | +use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition; |
| 24 | +use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException; |
17 | 25 |
|
18 | 26 | class NodeDefinitionTest extends TestCase |
19 | 27 | { |
@@ -60,10 +68,50 @@ public function testDocUrlWithoutPackage() |
60 | 68 |
|
61 | 69 | public function testUnknownPackageThrowsException() |
62 | 70 | { |
| 71 | + $node = new ArrayNodeDefinition('node'); |
| 72 | + |
63 | 73 | $this->expectException(\OutOfBoundsException::class); |
64 | 74 | $this->expectExceptionMessage('Package "phpunit/invalid" is not installed'); |
65 | 75 |
|
66 | | - $node = new ArrayNodeDefinition('node'); |
67 | 76 | $node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/invalid'); |
68 | 77 | } |
| 78 | + |
| 79 | + #[IgnoreDeprecations] |
| 80 | + #[Group('legacy')] |
| 81 | + #[DataProvider('provideDefinitionClassesAndDefaultValues')] |
| 82 | + public function testIncoherentRequiredAndDefaultValue(string $class, mixed $defaultValue) |
| 83 | + { |
| 84 | + $node = new $class('foo'); |
| 85 | + self::assertInstanceOf(NodeDefinition::class, $node); |
| 86 | + |
| 87 | + // $this->expectException(InvalidDefinitionException::class); |
| 88 | + // $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.'); |
| 89 | + $this->expectUserDeprecationMessage('Since symfony/config 7.4: Flagging a node with a default value as required is deprecated. Remove the default from node "foo" or make it optional.'); |
| 90 | + |
| 91 | + $node->defaultValue($defaultValue)->isRequired(); |
| 92 | + } |
| 93 | + |
| 94 | + #[IgnoreDeprecations] |
| 95 | + #[Group('legacy')] |
| 96 | + #[DataProvider('provideDefinitionClassesAndDefaultValues')] |
| 97 | + public function testIncoherentDefaultValueAndRequired(string $class, mixed $defaultValue) |
| 98 | + { |
| 99 | + $node = new $class('foo'); |
| 100 | + self::assertInstanceOf(NodeDefinition::class, $node); |
| 101 | + |
| 102 | + // $this->expectException(InvalidDefinitionException::class); |
| 103 | + // $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.'); |
| 104 | + $this->expectUserDeprecationMessage('Since symfony/config 7.4: Setting a default value to a required node is deprecated. Remove the default value from the node "foo" or make it optional.'); |
| 105 | + |
| 106 | + $node->isRequired()->defaultValue($defaultValue); |
| 107 | + } |
| 108 | + |
| 109 | + public static function provideDefinitionClassesAndDefaultValues() |
| 110 | + { |
| 111 | + yield [ArrayNodeDefinition::class, []]; |
| 112 | + yield [ScalarNodeDefinition::class, null]; |
| 113 | + yield [BooleanNodeDefinition::class, false]; |
| 114 | + yield [StringNodeDefinition::class, 'default']; |
| 115 | + yield [VariableNodeDefinition::class, 'default']; |
| 116 | + } |
69 | 117 | } |
0 commit comments