respect-validation/tests/unit/Rules/MimetypeTest.php
Henrique Moody b7043b2652
Set up "squizlabs/php_codesniffer"
The tool we used to verify whether the code base has the correct coding
standard was removed [1].

This commit will set up one that works best for us and will also make
sure we have fully compliant to PS1 and PSR2.

[1]: ffec95acda

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-03 19:39:14 +01:00

95 lines
3.1 KiB
PHP

<?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);
namespace Respect\Validation\Rules;
use finfo;
use Respect\Validation\Test\RuleTestCase;
use SplFileInfo;
use SplFileObject;
use const FILEINFO_MIME_TYPE;
use const PHP_INT_MAX;
use function random_int;
use function tmpfile;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\Mimetype
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class MimetypeTest extends RuleTestCase
{
/**
* @test
*/
public function itShouldValidateWithDefinedFinfoInstance(): void
{
$mimetype = 'application/octet-stream';
$filename = 'tests/fixtures/valid-image.png';
$fileInfoMock = $this
->getMockBuilder(finfo::class)
->disableOriginalConstructor()
->setMethods(['file'])
->getMock();
$fileInfoMock
->expects(self::once())
->method('file')
->with($filename, FILEINFO_MIME_TYPE)
->will(self::returnValue($mimetype));
$rule = new Mimetype($mimetype, $fileInfoMock);
self::assertTrue($rule->validate($filename));
}
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
return [
'image/png' => [new Mimetype('image/png'), 'tests/fixtures/valid-image.png'],
'image/gif' => [new Mimetype('image/gif'), 'tests/fixtures/valid-image.gif'],
'image/jpeg' => [new Mimetype('image/jpeg'), 'tests/fixtures/valid-image.jpg'],
'text/plain' => [new Mimetype('text/plain'), 'tests/fixtures/executable'],
'SplFileInfo' => [new Mimetype('image/png'), new SplFileInfo('tests/fixtures/valid-image.png')],
'SplFileObject' => [new Mimetype('image/png'), new SplFileObject('tests/fixtures/valid-image.png')],
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
return [
'invalid file' => [new Mimetype('image/png'), 'tests/fixtures/invalid-image.png'],
'mismatch' => [new Mimetype('image/gif'), 'tests/fixtures/valid-image.png'],
'directory' => [new Mimetype('application/octet-stream'), __DIR__],
'boolean' => [new Mimetype('application/octet-stream'), true],
'array' => [new Mimetype('application/octet-stream'), [__FILE__]],
'integer' => [new Mimetype('application/octet-stream'), random_int(1, PHP_INT_MAX)],
'float' => [new Mimetype('application/octet-stream'), random_int(1, 9) / 10],
'null' => [new Mimetype('application/octet-stream'), null],
'resource' => [new Mimetype('application/octet-stream'), tmpfile()],
];
}
}