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:
Danilo Benevides 2018-07-22 18:18:49 -03:00 committed by Henrique Moody
parent fd2bae7352
commit 861216c690
No known key found for this signature in database
GPG key ID: 221E9281655813A6
8 changed files with 113 additions and 91 deletions

View file

@ -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',

View file

@ -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)) {

View 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

View file

@ -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--

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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');
}
}