Repository: symfony/symfony
File: src/Symfony/Component/Config/Tests/Definition/Builder/NodeDefinitionTest.php (PHP)

Code before:

    public function testDocUrl()
    {
        $node = new ArrayNodeDefinition('node');
        $node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/phpunit');

        $r = new \ReflectionObject($node);
        $p = $r->getProperty('attributes');

        $this->assertMatchesRegularExpression('~^https://example.com/doc/phpunit/phpunit/\d+\.\d+$~', $p->getValue($node)['docUrl']);
    }

    public function testDocUrlWithoutPackage()
    {
        $node = new ArrayNodeDefinition('node');
        $node->docUrl('https://example.com/doc/empty{version:major}.empty{version:minor}');

        $r = new \ReflectionObject($node);
        $p = $r->getProperty('attributes');

        $this->assertSame('https://example.com/doc/empty.empty', $p->getValue($node)['docUrl']);
    }

    public function testUnknownPackageThrowsException()
    {
        $node = new ArrayNodeDefinition('node');

        $this->expectException(\OutOfBoundsException::class);
        $this->expectExceptionMessage('Package "phpunit/invalid" is not installed');

        $node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/invalid');
    }

    #[IgnoreDeprecations]
    #[Group('legacy')]
    #[DataProvider('provideDefinitionClassesAndDefaultValues')]
    public function testIncoherentRequiredAndDefaultValue(string $class, mixed $defaultValue)
    {
        $node = new $class('foo');
        self::assertInstanceOf(NodeDefinition::class, $node);

        //$this->expectException(InvalidDefinitionException::class);
        //$this->expectExceptionMessage('You cannot set the node "foo" as required as it has a default value.');
        $this->expectUserDeprecationMessage('Since symfony/config 7.4: Setting the node "foo" as required is deprecated when it has a default value.');

        $node->defaultValue($defaultValue)->isRequired();
    }

    #[IgnoreDeprecations]
    #[Group('legacy')]
    #[DataProvider('provideDefinitionClassesAndDefaultValues')]

Review comment (suggestion):
Moving `expectException` just before the call that produces it to be more specific.

Code after:

    public function testDocUrl()
    {
        $node = new ArrayNodeDefinition('node');
        $node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/phpunit');

        $r = new \ReflectionObject($node);
        $p = $r->getProperty('attributes');

        $this->assertMatchesRegularExpression('~^https://example.com/doc/phpunit/phpunit/\d+\.\d+$~', $p->getValue($node)['docUrl']);
    }

    public function testDocUrlWithoutPackage()
    {
        $node = new ArrayNodeDefinition('node');
        $node->docUrl('https://example.com/doc/empty{version:major}.empty{version:minor}');

        $r = new \ReflectionObject($node);
        $p = $r->getProperty('attributes');

        $this->assertSame('https://example.com/doc/empty.empty', $p->getValue($node)['docUrl']);
    }

    public function testUnknownPackageThrowsException()
    {
        $node = new ArrayNodeDefinition('node');

        $this->expectException(\OutOfBoundsException::class);
        $this->expectExceptionMessage('Package "phpunit/invalid" is not installed');

        $node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/invalid');
    }

    #[IgnoreDeprecations]
    #[Group('legacy')]
    #[DataProvider('provideDefinitionClassesAndDefaultValues')]
    public function testIncoherentRequiredAndDefaultValue(string $class, mixed $defaultValue)
    {
        $node = new $class('foo');
        self::assertInstanceOf(NodeDefinition::class, $node);

        // $this->expectException(InvalidDefinitionException::class);
        // $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
        $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.');

        $node->defaultValue($defaultValue)->isRequired();
    }

    #[IgnoreDeprecations]
    #[Group('legacy')]
    #[DataProvider('provideDefinitionClassesAndDefaultValues')]