Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Fix - missing messageTemplates in ExcludeMimeType validator #92

Merged
merged 3 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/File/ExcludeMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think INCORRECT_TYPE it's a better name. Why FALSE?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant was defined, the same constants name are in parent class MimeType, I've added only messages.
We can change it, maybe it is even better, but for consistency I'd prefer to keep FALSE_TYPE ...
And it's not in the scope of the issue ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. You're right.

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
Expand Down
42 changes: 30 additions & 12 deletions test/File/ExcludeMimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,43 @@ 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, []],
];
}

/**
* 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());
}

/**
Expand Down Expand Up @@ -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' => '',
Expand All @@ -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()
Expand Down