Sortie du snippet Symfony "Utiliser la validation Symfony en dehors du contexte des formulaires"

Environement :

  • Strangebuzz Snippet VM : 2.1.0
  • Symfony : 5.1.0

Sortie du code exécuté :

❌ The "toto/foo/notanURL" value is NOT valid: "Cette valeur n'est pas une URL valide."
❌ The "toto/foo/notanURL" value is NOT valid: "Cette chaîne est trop courte. Elle doit avoir au minimum 20 caractères."
✅ The "//example.com?foo=bar" value is valid.
✅ The "https://www.strangebuzz.com/en/snippets/using-the-symfony-validation-outside-of-the-form-context" value is valid.

 Retour au code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow  Snippet aléatoire

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
 * @covers Snippet14Trait
 */
final class Snippet14Test extends KernelTestCase
{
    private ValidatorInterface $validator;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->validator = self::$container->get('validator');
    }

    public function provide(): array
    {
        return [
            ['toto/foo/notanURL', false, 2],
            ['//example.com?foo=bar', true, 0],
            ['https://www.strangebuzz.com/en/snippets/using-the-symfony-validation-outside-of-the-form-context', true, 0],
        ];
    }

    /**
     * @covers Snippet14Trait::snippet14
     *
     * @dataProvider provide
     */
    public function testSnippet14(string $url, bool $isValid, int $count): void
    {
        $constraints = [
            new Url(['relativeProtocol' => true,]),
            new Length(['min' => 20, 'allowEmptyString' => false]),
        ];

        $violations = $this->validator->validate($url, $constraints);
        $this->assertSame($isValid, $violations->count() === 0);
        $this->assertCount($count, $violations);
    }
}