diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index 52e937944188b..133b085ad4297 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -13,8 +13,10 @@ use Symfony\Component\HttpFoundation\File\File as FileObject; use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Exception\UnexpectedValueException; @@ -170,12 +172,18 @@ public function validate($value, Constraint $constraint) } if ($constraint->mimeTypes) { - if (!$value instanceof FileObject) { + if (class_exists(MimeTypes::class)) { + $mime = MimeTypes::getDefault()->guessMimeType($path); + } else { + if (!class_exists(FileObject::class)) { + throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".'); + } + $value = new FileObject($value); + $mime = $value->getMimeType(); } $mimeTypes = (array) $constraint->mimeTypes; - $mime = $value->getMimeType(); foreach ($mimeTypes as $mimeType) { if ($mimeType === $mime) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 0bbac3c6b70a9..6e024431d4153 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\HttpFoundation\File\File as FileObject; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Constraints\File; use Symfony\Component\Validator\Constraints\FileValidator; @@ -283,21 +284,10 @@ public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsSt public function testValidMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('image/jpg'); + $file = new FileObject(__DIR__.'/Fixtures/blank.jpg'); $constraint = new File([ - 'mimeTypes' => ['image/png', 'image/jpg'], + 'mimeTypes' => ['image/png', 'image/jpeg'], ]); $this->validator->validate($file, $constraint); @@ -307,18 +297,7 @@ public function testValidMimeType() public function testValidWildcardMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('image/jpg'); + $file = new FileObject(__DIR__.'/Fixtures/test.gif'); $constraint = new File([ 'mimeTypes' => ['image/*'], @@ -331,21 +310,10 @@ public function testValidWildcardMimeType() public function testInvalidMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('application/pdf'); + $file = new FileObject(__DIR__.'/Fixtures/blank.pdf'); $constraint = new File([ - 'mimeTypes' => ['image/png', 'image/jpg'], + 'mimeTypes' => ['image/png', 'image/jpeg'], 'mimeTypesMessage' => 'myMessage', ]); @@ -353,30 +321,19 @@ public function testInvalidMimeType() $this->buildViolation('myMessage') ->setParameter('{{ type }}', '"application/pdf"') - ->setParameter('{{ types }}', '"image/png", "image/jpg"') - ->setParameter('{{ file }}', '"'.$this->path.'"') - ->setParameter('{{ name }}', '"'.basename($this->path).'"') + ->setParameter('{{ types }}', '"image/png", "image/jpeg"') + ->setParameter('{{ file }}', '"'.__DIR__.'/Fixtures/blank.pdf'.'"') + ->setParameter('{{ name }}', '"blank.pdf"') ->setCode(File::INVALID_MIME_TYPE_ERROR) ->assertRaised(); } public function testInvalidWildcardMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('application/pdf'); + $file = new FileObject(__DIR__.'/Fixtures/blank.pdf'); $constraint = new File([ - 'mimeTypes' => ['image/*', 'image/jpg'], + 'mimeTypes' => ['image/*', 'image/jpeg'], 'mimeTypesMessage' => 'myMessage', ]); @@ -384,9 +341,9 @@ public function testInvalidWildcardMimeType() $this->buildViolation('myMessage') ->setParameter('{{ type }}', '"application/pdf"') - ->setParameter('{{ types }}', '"image/*", "image/jpg"') - ->setParameter('{{ file }}', '"'.$this->path.'"') - ->setParameter('{{ name }}', '"'.basename($this->path).'"') + ->setParameter('{{ types }}', '"image/*", "image/jpeg"') + ->setParameter('{{ file }}', '"'.__DIR__.'/Fixtures/blank.pdf'.'"') + ->setParameter('{{ name }}', '"blank.pdf"') ->setCode(File::INVALID_MIME_TYPE_ERROR) ->assertRaised(); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.jpg b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.jpg new file mode 100644 index 0000000000000..51246b40a1265 Binary files /dev/null and b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.jpg differ diff --git a/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.pdf b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.pdf new file mode 100644 index 0000000000000..757bb1f36404f Binary files /dev/null and b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.pdf differ