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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!is_file($input)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return ($this->fileInfo->file($input) == $this->mimetype);
|
return ($this->fileInfo->file($input) == $this->mimetype);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,9 +22,23 @@ use SplFileInfo;
|
||||||
*/
|
*/
|
||||||
class MimetypeTest extends PHPUnit_Framework_TestCase
|
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()
|
public function testShouldValidateMimetype()
|
||||||
{
|
{
|
||||||
$filename = 'filename.txt';
|
|
||||||
$mimetype = 'plain/text';
|
$mimetype = 'plain/text';
|
||||||
|
|
||||||
$fileInfoMock = $this
|
$fileInfoMock = $this
|
||||||
|
@ -36,18 +50,18 @@ class MimetypeTest extends PHPUnit_Framework_TestCase
|
||||||
$fileInfoMock
|
$fileInfoMock
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('file')
|
->method('file')
|
||||||
->with($filename)
|
->with($this->filename)
|
||||||
->will($this->returnValue($mimetype));
|
->will($this->returnValue($mimetype));
|
||||||
|
|
||||||
$rule = new Mimetype($mimetype, $fileInfoMock);
|
$rule = new Mimetype($mimetype, $fileInfoMock);
|
||||||
|
|
||||||
$this->assertTrue($rule->validate($filename));
|
$rule->validate($this->filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testShouldValidateSplFileInfoMimetype()
|
public function testShouldValidateSplFileInfoMimetype()
|
||||||
{
|
{
|
||||||
$fileInfo = new SplFileInfo('filename.png');
|
$fileInfo = new SplFileInfo($this->filename);
|
||||||
$mimetype = 'image/png';
|
$mimetype = 'plain/text';
|
||||||
|
|
||||||
$fileInfoMock = $this
|
$fileInfoMock = $this
|
||||||
->getMockBuilder('finfo')
|
->getMockBuilder('finfo')
|
||||||
|
@ -73,6 +87,13 @@ class MimetypeTest extends PHPUnit_Framework_TestCase
|
||||||
$this->assertFalse($rule->validate(array(__FILE__)));
|
$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
|
* @expectedException Respect\Validation\Exceptions\MimetypeException
|
||||||
* @expectedExceptionMessage MimetypeTest.php" must have "application/json" mimetype
|
* @expectedExceptionMessage MimetypeTest.php" must have "application/json" mimetype
|
||||||
|
|
Loading…
Reference in a new issue