|
11 | 11 |
|
12 | 12 | namespace Symfony\Component
10000
span>\Validator\Tests\Constraints;
|
13 | 13 |
|
| 14 | +use Symfony\Component\HttpFoundation\File\File; |
| 15 | +use Symfony\Component\Mime\MimeTypes; |
14 | 16 | use Symfony\Component\Validator\Constraints\Image;
|
15 | 17 | use Symfony\Component\Validator\Constraints\ImageValidator;
|
16 | 18 | use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
@@ -579,4 +581,75 @@ public static function provideInvalidMimeTypeWithNarrowedSet()
|
579 | 581 | ]),
|
580 | 582 | ];
|
581 | 583 | }
|
| 584 | + |
| 585 | + /** |
| 586 | + * @dataProvider providerValidExtension |
| 587 | + */ |
| 588 | + public function testExtensionValid(string $name) |
| 589 | + { |
| 590 | + if (!class_exists(MimeTypes::class)) { |
| 591 | + $this->markTestSkipped('Guessing the mime type is not possible'); |
| 592 | + } |
| 593 | + |
| 594 | + $constraint = new Image(mimeTypes: [], extensions: ['gif'], extensionsMessage: 'myMessage'); |
| 595 | + |
| 596 | + $this->validator->validate(new File(__DIR__.'/Fixtures/'.$name), $constraint); |
| 597 | + |
| 598 | + $this->assertNoViolation(); |
| 599 | + } |
| 600 | + |
| 601 | + public static function providerValidExtension(): iterable |
| 602 | + { |
| 603 | + yield ['test.gif']; |
| 604 | + yield ['test.png.gif']; |
| 605 | + } |
| 606 | + |
| 607 | + /** |
| 608 | + * @dataProvider provideInvalidExtension |
| 609 | + */ |
| 61
6D4E
0 | + public function testExtensionInvalid(string $name, string $extension) |
| 611 | + { |
| 612 | + $path = __DIR__.'/Fixtures/'.$name; |
| 613 | + $constraint = new Image(extensions: ['png', 'svg'], extensionsMessage: 'myMessage'); |
| 614 | + |
| 615 | + $this->validator->validate(new File($path), $constraint); |
| 616 | + |
| 617 | + $this->buildViolation('myMessage') |
| 618 | + ->setParameters([ |
| 619 | + '{{ file }}' => '"'.$path.'"', |
| 620 | + '{{ extension }}' => '"'.$extension.'"', |
| 621 | + '{{ extensions }}' => '"png", "svg"', |
| 622 | + '{{ name }}' => '"'.$name.'"', |
| 623 | + ]) |
| 624 | + ->setCode(Image::INVALID_EXTENSION_ERROR) |
| 625 | + ->assertRaised(); |
| 626 | + } |
| 627 | + |
| 628 | + public static function provideInvalidExtension(): iterable |
| 629 | + { |
| 630 | + yield ['test.gif', 'gif']; |
| 631 | + yield ['test.png.gif', 'gif']; |
| 632 | + } |
| 633 | + |
| 634 | + public function testExtensionAutodetectMimeTypesInvalid() |
| 635 | + { |
| 636 | + if (!class_exists(MimeTypes::class)) { |
| 637 | + $this->markTestSkipped('Guessing the mime type is not possible'); |
| 638 | + } |
| 639 | + |
| 640 | + $path = __DIR__.'/Fixtures/invalid-content.gif'; |
| 641 | + $constraint = new Image(mimeTypesMessage: 'myMessage', extensions: ['gif']); |
| 642 | + |
| 643 | + $this->validator->validate(new File($path), $constraint); |
| 644 | + |
| 645 | + $this->buildViolation('myMessage') |
| 646 | + ->setParameters([ |
| 647 | + '{{ file }}' => '"'.$path.'"', |
| 648 | + '{{ name }}' => '"invalid-content.gif"', |
| 649 | + '{{ type }}' => '"text/plain"', |
| 650 | + '{{ types }}' => '"image/gif"', |
| 651 | + ]) |
| 652 | + ->setCode(Image::INVALID_MIME_TYPE_ERROR) |
| 653 | + ->assertRaised(); |
| 654 | + } |
582 | 655 | }
|
0 commit comments