2015-06-16 06:16:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-16 06:16:29 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-06-16 06:16:29 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-09-16 09:47:16 +02:00
|
|
|
use finfo;
|
|
|
|
use Respect\Validation\Test\RuleTestCase;
|
2015-06-16 06:16:29 +02:00
|
|
|
use SplFileInfo;
|
2018-09-16 09:47:16 +02:00
|
|
|
use SplFileObject;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2018-09-16 09:47:16 +02:00
|
|
|
use function random_int;
|
|
|
|
use function tmpfile;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-02-09 13:31:32 +01:00
|
|
|
use const FILEINFO_MIME_TYPE;
|
|
|
|
use const PHP_INT_MAX;
|
2015-06-16 06:16:29 +02:00
|
|
|
|
|
|
|
/**
|
2018-09-16 09:47:16 +02:00
|
|
|
* @group rule
|
|
|
|
*
|
2018-07-23 20:35:14 +02:00
|
|
|
* @covers \Respect\Validation\Rules\Mimetype
|
2018-09-16 09:47:16 +02:00
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-09-16 09:47:16 +02:00
|
|
|
* @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
|
|
|
*/
|
2018-09-16 09:47:16 +02:00
|
|
|
final class MimetypeTest extends RuleTestCase
|
2015-06-16 06:16:29 +02:00
|
|
|
{
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2018-09-16 09:47:16 +02:00
|
|
|
public function itShouldValidateWithDefinedFinfoInstance(): void
|
2015-06-16 06:16:29 +02:00
|
|
|
{
|
2018-09-16 09:47:16 +02:00
|
|
|
$mimetype = 'application/octet-stream';
|
2019-02-03 18:58:30 +01:00
|
|
|
$filename = 'tests/fixtures/valid-image.png';
|
2015-06-16 06:16:29 +02:00
|
|
|
|
|
|
|
$fileInfoMock = $this
|
2018-09-16 09:47:16 +02:00
|
|
|
->getMockBuilder(finfo::class)
|
2015-06-16 06:16:29 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$fileInfoMock
|
2018-07-23 20:35:14 +02:00
|
|
|
->expects(self::once())
|
2015-06-16 06:16:29 +02:00
|
|
|
->method('file')
|
2018-09-16 09:47:16 +02:00
|
|
|
->with($filename, FILEINFO_MIME_TYPE)
|
2018-07-23 20:35:14 +02:00
|
|
|
->will(self::returnValue($mimetype));
|
2015-06-16 06:16:29 +02:00
|
|
|
|
|
|
|
$rule = new Mimetype($mimetype, $fileInfoMock);
|
|
|
|
|
2018-09-16 09:47:16 +02:00
|
|
|
self::assertTrue($rule->validate($filename));
|
2015-06-16 06:16:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-07-23 20:35:14 +02:00
|
|
|
*/
|
2023-04-02 16:00:37 +02:00
|
|
|
public static function providerForValidInput(): array
|
2015-06-16 06:16:29 +02:00
|
|
|
{
|
2018-09-16 09:47:16 +02:00
|
|
|
return [
|
2019-02-03 18:58:30 +01:00
|
|
|
'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')],
|
2018-09-16 09:47:16 +02:00
|
|
|
];
|
2015-08-27 00:46:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-16 06:16:29 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2015-06-16 06:16:29 +02:00
|
|
|
*/
|
2023-04-02 16:00:37 +02:00
|
|
|
public static function providerForInvalidInput(): array
|
2015-06-16 06:16:29 +02:00
|
|
|
{
|
2018-09-16 09:47:16 +02:00
|
|
|
return [
|
2019-02-03 18:58:30 +01:00
|
|
|
'invalid file' => [new Mimetype('image/png'), 'tests/fixtures/invalid-image.png'],
|
|
|
|
'mismatch' => [new Mimetype('image/gif'), 'tests/fixtures/valid-image.png'],
|
2018-09-16 09:47:16 +02:00
|
|
|
'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
|
|
|
}
|
|
|
|
}
|