Repository: symfony/symfony
File: src/Symfony/Component/Validator/Tests/Constraints/XmlValidatorTest.php (PHP)

Code before:

use Symfony\Component\Validator\Constraints\Xml;
use Symfony\Component\Validator\Constraints\XmlValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;

final class XmlValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): XmlValidator
    {
        return new XmlValidator();
    }

    /**
     * @dataProvider getValidXmlValues
     */
    public function testValidXmlValue($value)
    {
        $this->validator->validate($value, new Xml());
        $this->assertNoViolation();
    }

    /**
     * @dataProvider getInvalidXmlValues
     */
    public function testInValidXmlValue($value)
    {
        $constraint = new Xml(message: 'myMessage');
        $this->validator->validate($value, $constraint);
        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"'.$value.'"')
            ->setCode(Xml::INVALID_XML_ERROR)
            ->assertRaised();
    }

    public static function getValidXmlValues(): array
    {
        $xml = <<<'XML'
        <?xml version="1.0" encoding="UTF-8"?>
        <srv:container xmlns="http://symfony.com/schema/dic/security"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:srv="http://symfony.com/schema/dic/services"
        >
            <config>
                <provider name="default">
                    <memory>
                        <user identifier="foo" password="foo" />
                    </memory>
                </provider>
                <firewall name="simple" pattern="/" security="false" />
            </config>

Review comment (suggestion):
```suggestion
    public function testInvalidXmlValue(string $value)
```

Code after:

use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Validator\Constraints\Xml;
use Symfony\Component\Validator\Constraints\XmlValidator;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;

class XmlValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): XmlValidator
    {
        return new XmlValidator();
    }

    #[DataProvider('getValidXmlFormatValues')]
    public function testValidXmlFormatValue($value)
    {
        $this->validator->validate($value, new Xml());
        $this->assertNoViolation();
    }

    public static function getValidXmlFormatValues(): array
    {
        return [
            ['<?xml version="1.0" encoding="utf-8" ?><code></code>'],
            ['<code></code>'],
            ['<test/>'],
            [file_get_contents(__DIR__.'/Fixtures/example.xml')],
            [new StringableValue('<test>test</test>')],
        ];
    }

    #[DataProvider('getInvalidXmlFormatValues')]
    public function testInvalidXmlFormatValue($value)
    {
        $constraint = new Xml(formatMessage: 'myMessage');
        $this->validator->validate($value, $constraint);
        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"'.$value.'"')
            ->setCode(Xml::INVALID_XML_ERROR)
            ->assertRaised();
    }

    public static function getInvalidXmlFormatValues(): array
    {
        return [
            ['test'],
            ['<?xml version="1" ?><code></code>'],
            ['<?xml version="1.0" encoding="" ?>'],
            ['<test><test>'],