mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
Apply contribution guidelines to "Image" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com> Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
fd2bae7352
commit
861216c690
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ImageException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Guilherme Siani <guilherme@siani.com.br>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class ImageException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a valid image',
|
||||
|
|
|
@ -15,20 +15,41 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use finfo;
|
||||
use SplFileInfo;
|
||||
use function is_file;
|
||||
use function is_string;
|
||||
use function mb_strpos;
|
||||
|
||||
class Image extends AbstractRule
|
||||
/**
|
||||
* Validates if the file is a valid image by checking its MIME type.
|
||||
*
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Guilherme Siani <guilherme@siani.com.br>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class Image extends AbstractRule
|
||||
{
|
||||
public $fileInfo;
|
||||
/**
|
||||
* @var finfo
|
||||
*/
|
||||
private $fileInfo;
|
||||
|
||||
/**
|
||||
* Initializes the rule.
|
||||
*
|
||||
* @param finfo|null $fileInfo
|
||||
*/
|
||||
public function __construct(finfo $fileInfo = null)
|
||||
{
|
||||
$this->fileInfo = $fileInfo ?: new finfo(FILEINFO_MIME_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($input instanceof SplFileInfo) {
|
||||
$input = $input->getPathname();
|
||||
return $this->validate($input->getPathname());
|
||||
}
|
||||
|
||||
if (!is_string($input)) {
|
||||
|
|
38
tests/integration/rules/image.phpt
Normal file
38
tests/integration/rules/image.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ImageException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::image()->check('tests/fixtures/invalid-image.png');
|
||||
} catch (ImageException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::image())->check('tests/fixtures/valid-image.png');
|
||||
} catch (ImageException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::image()->assert(new stdClass());
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::image())->assert('tests/fixtures/valid-image.gif');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"tests/fixtures/invalid-image.png" must be a valid image
|
||||
"tests/fixtures/valid-image.png" must not be a valid image
|
||||
- `[object] (stdClass: { })` must be a valid image
|
||||
- "tests/fixtures/valid-image.gif" must not be a valid image
|
|
@ -1,10 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::image()->assert('tests/fixtures/valid-image.png');
|
||||
v::image()->check(new SplFileInfo('tests/fixtures/valid-image.gif'));
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ImageException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::image()->check('tests/fixtures/invalid-image.png');
|
||||
} catch (ImageException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"tests/fixtures/invalid-image.png" must be a valid image
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::image()->assert(new stdClass());
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- `[object] (stdClass: { })` must be a valid image
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ImageException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::image())->check('tests/fixtures/valid-image.png');
|
||||
} catch (ImageException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"tests/fixtures/valid-image.png" must not be a valid image
|
|
@ -14,62 +14,75 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use finfo;
|
||||
use realpath;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use SplFileInfo;
|
||||
use SplFileObject;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Image
|
||||
*
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Guilherme Siani <guilherme@siani.com.br>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class ImageTest extends RuleTestCase
|
||||
final class ImageTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldAcceptAnInstanceOfFinfoOnConstructor(): void
|
||||
{
|
||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||
$rule = new Image($finfo);
|
||||
|
||||
self::assertSame($rule->fileInfo, $finfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function shouldHaveAnInstanceOfFinfoByDefault(): void
|
||||
{
|
||||
$rule = new Image();
|
||||
|
||||
self::assertInstanceOf('finfo', $rule->fileInfo);
|
||||
}
|
||||
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new Image();
|
||||
$fixturesDirectory = realpath(__DIR__.'/../../fixtures/');
|
||||
|
||||
return [
|
||||
[$rule, $fixturesDirectory.'/valid-image.gif'],
|
||||
[$rule, $fixturesDirectory.'/valid-image.jpg'],
|
||||
[$rule, $fixturesDirectory.'/valid-image.png'],
|
||||
[$rule, new SplFileInfo($fixturesDirectory.'/valid-image.gif')],
|
||||
[$rule, new SplFileInfo($fixturesDirectory.'/valid-image.jpg')],
|
||||
[$rule, new SplFileObject($fixturesDirectory.'/valid-image.png')],
|
||||
[$rule, $this->getFixtureDirectory().'/valid-image.gif'],
|
||||
[$rule, $this->getFixtureDirectory().'/valid-image.jpg'],
|
||||
[$rule, $this->getFixtureDirectory().'/valid-image.png'],
|
||||
[$rule, new SplFileInfo($this->getFixtureDirectory().'/valid-image.gif')],
|
||||
[$rule, new SplFileInfo($this->getFixtureDirectory().'/valid-image.jpg')],
|
||||
[$rule, new SplFileObject($this->getFixtureDirectory().'/valid-image.png')],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new Image();
|
||||
$fixturesDirectory = realpath(__DIR__.'/../../fixtures/');
|
||||
|
||||
return [
|
||||
[$rule, $fixturesDirectory.'/invalid-image.png'],
|
||||
[$rule, $this->getFixtureDirectory().'/invalid-image.png'],
|
||||
[$rule, 'asdf'],
|
||||
[$rule, 1],
|
||||
[$rule, true],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function shouldValidateWithDefinedInstanceOfFileInfo(): void
|
||||
{
|
||||
$input = $this->getFixtureDirectory().'/valid-image.gif';
|
||||
|
||||
$finfo = $this->createMock(finfo::class);
|
||||
$finfo
|
||||
->expects(self::once())
|
||||
->method('file')
|
||||
->with($input)
|
||||
->will(self::returnValue('image/gif'));
|
||||
|
||||
$rule = new Image($finfo);
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
private function getFixtureDirectory(): string
|
||||
{
|
||||
return realpath(__DIR__.'/../../fixtures');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue