/** * Validate if extension is allowed. * * @param FileInterface $file The file that should be validated * @param Constraint $constraint The constraint for the validation */ protected function validateFileExtension(FileInterface $file, Constraint $constraint) { if (!empty($constraint->allowedExtensions)) { $extension = null !== $file->getUploadedFile() ? $file->getUploadedFile()->getClientOriginalExtension() : $file->getExtension(); if (!in_array(strtolower($extension), $constraint->allowedExtensions)) { $this->context->buildViolation($constraint->extensionsMessage, ['%extensions%' => implode(', ', $constraint->allowedExtensions)])->addViolation(); } } }
function it_validates_empty_extension_and_size($context, File $constraint, FileInterface $file) { $constraint->allowedExtensions = []; $file->getId()->willReturn(12); $file->getUploadedFile()->willReturn(null); $file->getExtension()->willReturn('jpg'); $file->getSize()->willReturn(100); $context->buildViolation(Argument::any())->shouldNotBeCalled(); $this->validate($file, $constraint); }