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

Commit

Permalink
Merge pull request #92 in develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w authored Jun 23, 2016
2 parents 59270e9 + 66b579b commit d2ceefd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
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%'",
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

0 comments on commit d2ceefd

Please sign in to comment.