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

92 lines
2.9 KiB
PHP
Raw Normal View History

2015-06-16 06:16:29 +02:00
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
2015-06-16 06:16:29 +02:00
*/
declare(strict_types=1);
2015-06-16 06:16:29 +02:00
namespace Respect\Validation\Rules;
use finfo;
use Respect\Validation\Test\RuleTestCase;
2015-06-16 06:16:29 +02:00
use SplFileInfo;
use SplFileObject;
use function random_int;
use function tmpfile;
use const FILEINFO_MIME_TYPE;
use const PHP_INT_MAX;
2015-06-16 06:16:29 +02:00
/**
* @group rule
*
* @covers \Respect\Validation\Rules\Mimetype
*
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
2015-06-16 06:16:29 +02:00
*/
final class MimetypeTest extends RuleTestCase
2015-06-16 06:16:29 +02:00
{
/**
* @test
*/
public function itShouldValidateWithDefinedFinfoInstance(): void
2015-06-16 06:16:29 +02:00
{
$mimetype = 'application/octet-stream';
$filename = 'tests/fixtures/valid-image.png';
2015-06-16 06:16:29 +02:00
$fileInfoMock = $this
->getMockBuilder(finfo::class)
2015-06-16 06:16:29 +02:00
->disableOriginalConstructor()
->getMock();
$fileInfoMock
->expects(self::once())
2015-06-16 06:16:29 +02:00
->method('file')
->with($filename, FILEINFO_MIME_TYPE)
->will(self::returnValue($mimetype));
2015-06-16 06:16:29 +02:00
$rule = new Mimetype($mimetype, $fileInfoMock);
self::assertTrue($rule->validate($filename));
2015-06-16 06:16:29 +02:00
}
/**
* {@inheritDoc}
*/
public static function providerForValidInput(): array
2015-06-16 06:16:29 +02:00
{
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')],
];
}
2015-06-16 06:16:29 +02:00
/**
* {@inheritDoc}
2015-06-16 06:16:29 +02:00
*/
public static function providerForInvalidInput(): array
2015-06-16 06:16:29 +02:00
{
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()],
];
2015-06-16 06:16:29 +02:00
}
}