respect-validation/tests/unit/Rules/MimetypeTest.php

122 lines
2.9 KiB
PHP
Raw Normal View History

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.
*/
declare(strict_types=1);
2015-06-16 06:16:29 +02:00
namespace Respect\Validation\Rules;
2017-11-04 11:21:40 +01:00
use PHPUnit\Framework\TestCase;
2015-06-16 06:16:29 +02:00
use SplFileInfo;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @group rule
2017-02-04 14:01:14 +01:00
* @covers \Respect\Validation\Exceptions\MimetypeException
* @covers \Respect\Validation\Rules\Mimetype
2015-06-16 06:16:29 +02:00
*/
2017-11-04 11:21:40 +01:00
class MimetypeTest extends TestCase
2015-06-16 06:16:29 +02:00
{
private $filename;
protected function setUp(): void
{
$this->filename = sprintf('%s/validation.txt', sys_get_temp_dir());
file_put_contents($this->filename, 'File content');
}
protected function tearDown(): void
{
unlink($this->filename);
}
/**
* @test
*/
public function shouldValidateMimetype(): void
2015-06-16 06:16:29 +02:00
{
$mimetype = 'plain/text';
$fileInfoMock = $this
->getMockBuilder('finfo')
->disableOriginalConstructor()
2015-10-18 03:44:47 +02:00
->setMethods(['file'])
2015-06-16 06:16:29 +02:00
->getMock();
$fileInfoMock
->expects(self::once())
2015-06-16 06:16:29 +02:00
->method('file')
->with($this->filename)
->will(self::returnValue($mimetype));
2015-06-16 06:16:29 +02:00
$rule = new Mimetype($mimetype, $fileInfoMock);
$rule->validate($this->filename);
2015-06-16 06:16:29 +02:00
}
/**
* @test
*/
public function shouldValidateSplFileInfoMimetype(): void
2015-06-16 06:16:29 +02:00
{
$fileInfo = new SplFileInfo($this->filename);
$mimetype = 'plain/text';
2015-06-16 06:16:29 +02:00
$fileInfoMock = $this
->getMockBuilder('finfo')
->disableOriginalConstructor()
2015-10-18 03:44:47 +02:00
->setMethods(['file'])
2015-06-16 06:16:29 +02:00
->getMock();
$fileInfoMock
->expects(self::once())
2015-06-16 06:16:29 +02:00
->method('file')
->with($fileInfo->getPathname())
->will(self::returnValue($mimetype));
2015-06-16 06:16:29 +02:00
$rule = new Mimetype($mimetype, $fileInfoMock);
self::assertTrue($rule->validate($fileInfo));
2015-06-16 06:16:29 +02:00
}
/**
* @test
*/
public function shouldInvalidateWhenNotStringNorSplFileInfo(): void
2015-06-16 06:16:29 +02:00
{
$rule = new Mimetype('application/octet-stream');
self::assertFalse($rule->validate([__FILE__]));
2015-06-16 06:16:29 +02:00
}
/**
* @test
*/
public function shouldInvalidateWhenItIsNotAValidFile(): void
{
$rule = new Mimetype('application/octet-stream');
self::assertFalse($rule->validate(__DIR__));
}
2015-06-16 06:16:29 +02:00
/**
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\MimetypeException
* @expectedExceptionMessageRegExp #".+MimetypeTest.php" must have "application.?/json" mimetype#
*
* @test
2015-06-16 06:16:29 +02:00
*/
public function shouldThrowMimetypeExceptionWhenCheckingValue(): void
2015-06-16 06:16:29 +02:00
{
$rule = new Mimetype('application/json');
$rule->check(__FILE__);
}
}