2015-06-16 06:16:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use SplFileInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2015-08-16 20:00:05 +02:00
|
|
|
* @group rule
|
|
|
|
* @covers Respect\Validation\Rules\Mimetype
|
|
|
|
* @covers Respect\Validation\Exceptions\MimetypeException
|
2015-06-16 06:16:29 +02:00
|
|
|
*/
|
|
|
|
class MimetypeTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2015-08-27 00:46:51 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-16 06:16:29 +02:00
|
|
|
public function testShouldValidateMimetype()
|
|
|
|
{
|
|
|
|
$mimetype = 'plain/text';
|
|
|
|
|
|
|
|
$fileInfoMock = $this
|
|
|
|
->getMockBuilder('finfo')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(array('file'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$fileInfoMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('file')
|
2015-08-27 00:46:51 +02:00
|
|
|
->with($this->filename)
|
2015-06-16 06:16:29 +02:00
|
|
|
->will($this->returnValue($mimetype));
|
|
|
|
|
|
|
|
$rule = new Mimetype($mimetype, $fileInfoMock);
|
|
|
|
|
2015-08-27 00:46:51 +02:00
|
|
|
$rule->validate($this->filename);
|
2015-06-16 06:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldValidateSplFileInfoMimetype()
|
|
|
|
{
|
2015-08-27 00:46:51 +02:00
|
|
|
$fileInfo = new SplFileInfo($this->filename);
|
|
|
|
$mimetype = 'plain/text';
|
2015-06-16 06:16:29 +02:00
|
|
|
|
|
|
|
$fileInfoMock = $this
|
|
|
|
->getMockBuilder('finfo')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(array('file'))
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$fileInfoMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('file')
|
|
|
|
->with($fileInfo->getPathname())
|
|
|
|
->will($this->returnValue($mimetype));
|
|
|
|
|
|
|
|
$rule = new Mimetype($mimetype, $fileInfoMock);
|
|
|
|
|
|
|
|
$this->assertTrue($rule->validate($fileInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShouldInvalidateWhenNotStringNorSplFileInfo()
|
|
|
|
{
|
|
|
|
$rule = new Mimetype('application/octet-stream');
|
|
|
|
|
|
|
|
$this->assertFalse($rule->validate(array(__FILE__)));
|
|
|
|
}
|
|
|
|
|
2015-08-27 00:46:51 +02:00
|
|
|
public function testShouldInvalidateWhenItIsNotAValidFile()
|
|
|
|
{
|
|
|
|
$rule = new Mimetype('application/octet-stream');
|
|
|
|
|
|
|
|
$this->assertFalse($rule->validate(__DIR__));
|
|
|
|
}
|
|
|
|
|
2015-06-16 06:16:29 +02:00
|
|
|
/**
|
|
|
|
* @expectedException Respect\Validation\Exceptions\MimetypeException
|
2015-10-17 16:14:38 +02:00
|
|
|
* @expectedExceptionMessageRegExp #".+MimetypeTest.php" must have "application.?/json" mimetype#
|
2015-06-16 06:16:29 +02:00
|
|
|
*/
|
2015-10-17 16:14:38 +02:00
|
|
|
public function testShouldThrowMimetypeExceptionWhenCheckingValue()
|
2015-06-16 06:16:29 +02:00
|
|
|
{
|
|
|
|
$rule = new Mimetype('application/json');
|
|
|
|
$rule->check(__FILE__);
|
|
|
|
}
|
|
|
|
}
|