mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 02:30:06 +02:00
The finfo::file() accepts only valid files
This commit is contained in:
parent
c39e7fb08c
commit
8faed71a05
|
@ -54,6 +54,10 @@ class Mimetype extends AbstractRule
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!is_file($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ($this->fileInfo->file($input) == $this->mimetype);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,9 +22,23 @@ use SplFileInfo;
|
|||
*/
|
||||
class MimetypeTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
private $filename;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->filename = sprintf('%s/validation.txt', sys_get_temp_dir());
|
||||
|
||||
file_put_contents($this->filename, 'File content');
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unlink($this->filename);
|
||||
}
|
||||
|
||||
public function testShouldValidateMimetype()
|
||||
{
|
||||
$filename = 'filename.txt';
|
||||
$mimetype = 'plain/text';
|
||||
|
||||
$fileInfoMock = $this
|
||||
|
@ -36,18 +50,18 @@ class MimetypeTest extends PHPUnit_Framework_TestCase
|
|||
$fileInfoMock
|
||||
->expects($this->once())
|
||||
->method('file')
|
||||
->with($filename)
|
||||
->with($this->filename)
|
||||
->will($this->returnValue($mimetype));
|
||||
|
||||
$rule = new Mimetype($mimetype, $fileInfoMock);
|
||||
|
||||
$this->assertTrue($rule->validate($filename));
|
||||
$rule->validate($this->filename);
|
||||
}
|
||||
|
||||
public function testShouldValidateSplFileInfoMimetype()
|
||||
{
|
||||
$fileInfo = new SplFileInfo('filename.png');
|
||||
$mimetype = 'image/png';
|
||||
$fileInfo = new SplFileInfo($this->filename);
|
||||
$mimetype = 'plain/text';
|
||||
|
||||
$fileInfoMock = $this
|
||||
->getMockBuilder('finfo')
|
||||
|
@ -73,6 +87,13 @@ class MimetypeTest extends PHPUnit_Framework_TestCase
|
|||
$this->assertFalse($rule->validate(array(__FILE__)));
|
||||
}
|
||||
|
||||
public function testShouldInvalidateWhenItIsNotAValidFile()
|
||||
{
|
||||
$rule = new Mimetype('application/octet-stream');
|
||||
|
||||
$this->assertFalse($rule->validate(__DIR__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\MimetypeException
|
||||
* @expectedExceptionMessage MimetypeTest.php" must have "application/json" mimetype
|
||||
|
|
Loading…
Reference in a new issue