diff --git a/src/File/ExcludeMimeType.php b/src/File/ExcludeMimeType.php index f0bd4f8cc..ac8352fac 100644 --- a/src/File/ExcludeMimeType.php +++ b/src/File/ExcludeMimeType.php @@ -21,6 +21,15 @@ class ExcludeMimeType extends MimeType const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected'; const NOT_READABLE = 'fileExcludeMimeTypeNotReadable'; + /** + * @var array Error message templates + */ + protected $messageTemplates = [ + self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'", + self::NOT_DETECTED => "The mimetype could not be detected from the file", + self::NOT_READABLE => "File is not readable or does not exist", + ]; + /** * Returns true if the mimetype of the file does not matche the given ones. Also parts * of mimetypes can be checked. If you give for example "image" all image diff --git a/test/File/ExcludeMimeTypeTest.php b/test/File/ExcludeMimeTypeTest.php index 737f9e126..169f97832 100644 --- a/test/File/ExcludeMimeTypeTest.php +++ b/test/File/ExcludeMimeTypeTest.php @@ -25,18 +25,24 @@ public function basicBehaviorDataProvider() { $testFile = __DIR__ . '/_files/picture.jpg'; $fileUpload = [ - 'tmp_name' => $testFile, 'name' => basename($testFile), - 'size' => 200, 'error' => 0, 'type' => 'image/jpeg' + 'tmp_name' => $testFile, + 'name' => basename($testFile), + 'size' => 200, + 'error' => 0, + 'type' => 'image/jpeg', ]; + + $falseTypeMessage = [ExcludeMimeType::FALSE_TYPE => "File has an incorrect mimetype of 'image/jpeg'"]; + return [ - // Options, isValid Param, Expected value - ['image/gif', $fileUpload, true], - ['image', $fileUpload, false], - ['test/notype', $fileUpload, true], - ['image/gif, image/jpeg', $fileUpload, false], - [['image/vasa', 'image/gif'], $fileUpload, true], - [['image/gif', 'jpeg'], $fileUpload, false], - [['image/gif', 'gif'], $fileUpload, true], + // Options, isValid Param, Expected value, messages + ['image/gif', $fileUpload, true, []], + ['image', $fileUpload, false, $falseTypeMessage], + ['test/notype', $fileUpload, true, []], + ['image/gif, image/jpeg', $fileUpload, false, $falseTypeMessage], + [['image/vasa', 'image/gif'], $fileUpload, true, []], + [['image/gif', 'jpeg'], $fileUpload, false, $falseTypeMessage], + [['image/gif', 'gif'], $fileUpload, true, []], ]; } @@ -44,13 +50,18 @@ public function basicBehaviorDataProvider() * Ensures that the validator follows expected behavior * * @dataProvider basicBehaviorDataProvider - * @return void + * + * @param string|array $options + * @param array $isValidParam + * @param bool $expected + * @param array $messages */ - public function testBasic($options, $isValidParam, $expected) + public function testBasic($options, array $isValidParam, $expected, array $messages) { $validator = new ExcludeMimeType($options); $validator->enableHeaderCheck(); $this->assertEquals($expected, $validator->isValid($isValidParam)); + $this->assertEquals($messages, $validator->getMessages()); } /** @@ -137,6 +148,12 @@ public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage() $this->assertFalse($validator->isValid('')); $this->assertArrayHasKey(ExcludeMimeType::NOT_READABLE, $validator->getMessages()); + $this->assertNotEmpty($validator->getMessages()[ExcludeMimeType::NOT_READABLE]); + } + + public function testEmptyArrayFileShouldReturnFalseAdnDisplayNotFoundMessage() + { + $validator = new ExcludeMimeType(); $filesArray = [ 'name' => '', @@ -148,6 +165,7 @@ public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage() $this->assertFalse($validator->isValid($filesArray)); $this->assertArrayHasKey(ExcludeMimeType::NOT_READABLE, $validator->getMessages()); + $this->assertNotEmpty($validator->getMessages()[ExcludeMimeType::NOT_READABLE]); } public function testIsValidRaisesExceptionWithArrayNotInFilesFormat()