Apply contribution guidelines to "Extension" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Correa 2018-09-19 23:07:43 -03:00 committed by Henrique Moody
parent 1b4a904871
commit 4106589e7f
No known key found for this signature in database
GPG key ID: 221E9281655813A6
4 changed files with 82 additions and 60 deletions

View file

@ -16,12 +16,13 @@ namespace Respect\Validation\Exceptions;
/**
* Exception class for Extension rule.
*
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class ExtensionException extends ValidationException
final class ExtensionException extends ValidationException
{
/**
* @var array
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [

View file

@ -14,23 +14,29 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use SplFileInfo;
use const PATHINFO_EXTENSION;
use function is_string;
use function pathinfo;
/**
* Validate file extensions.
*
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Extension extends AbstractRule
final class Extension extends AbstractRule
{
/**
* @var string
*/
public $extension;
private $extension;
/**
* Initializes the rule.
*
* @param string $extension
*/
public function __construct($extension)
public function __construct(string $extension)
{
$this->extension = $extension;
}
@ -41,13 +47,13 @@ class Extension extends AbstractRule
public function validate($input): bool
{
if ($input instanceof SplFileInfo) {
return $input->getExtension() == $this->extension;
return $this->extension === $input->getExtension();
}
if (!is_string($input)) {
return false;
}
return pathinfo($input, PATHINFO_EXTENSION) == $this->extension;
return $this->extension === pathinfo($input, PATHINFO_EXTENSION);
}
}

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ExtensionException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::extension('png')->check('filename.txt');
} catch (ExtensionException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::extension('gif'))->check('filename.gif');
} catch (ExtensionException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::extension('mp3')->assert('filename.wav');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::extension('png'))->assert('tests/fixtures/invalid-image.png');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"filename.txt" must have "png" extension
"filename.gif" must not have "gif" extension
- "filename.wav" must have "mp3" extension
- "tests/fixtures/invalid-image.png" must not have "png" extension

View file

@ -13,72 +13,50 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Test\RuleTestCase;
use SplFileInfo;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @group rule
* @covers \Respect\Validation\Exceptions\ExtensionException
* @group rule
*
* @covers \Respect\Validation\Rules\Extension
*
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class ExtensionTest extends TestCase
final class ExtensionTest extends RuleTestCase
{
public function providerValidExtension()
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
return [
['filename.txt', 'txt'],
['filename.jpg', 'jpg'],
['filename.inc.php', 'php'],
['filename.foo.bar.bz2', 'bz2'],
'txt' => [new Extension('txt'), 'filename.txt'],
'jpg' => [new Extension('jpg'), 'filename.jpg'],
'inc' => [new Extension('inc'), 'filename.inc'],
'bz2' => [new Extension('bz2'), 'filename.foo.bar.bz2'],
'php' => [new Extension('php'), new SplFileInfo(__FILE__)],
'png' => [new Extension('png'), $this->getFixtureDirectory().'valid-image.png'],
'gif' => [new Extension('gif'), $this->getFixtureDirectory().'valid-image.gif'],
'file-invalid' => [new Extension('png'), $this->getFixtureDirectory().'invalid-image.png'],
];
}
/**
* @dataProvider providerValidExtension
*
* @test
* {@inheritdoc}
*/
public function shouldValidateExtension($filename, $extension): void
public function providerForInvalidInput(): array
{
$rule = new Extension($extension);
self::assertTrue($rule->validate($filename));
}
/**
* @test
*/
public function shouldAcceptSplFileInfo(): void
{
$fileInfo = new SplFileInfo(__FILE__);
$rule = new Extension('php');
self::assertTrue($rule->validate($fileInfo));
}
/**
* @test
*/
public function shouldInvalidWhenNotStringNorSplFileInfo(): void
{
$nonFile = [__FILE__];
$rule = new Extension('php');
self::assertFalse($rule->validate($nonFile));
}
/**
* @expectedException \Respect\Validation\Exceptions\ExtensionException
* @expectedExceptionMessage "filename.jpg" must have "png" extension
*
* @test
*/
public function shouldThrowExtensionExceptionWhenCheckingValue(): void
{
$rule = new Extension('png');
$rule->check('filename.jpg');
return [
'jpg' => [new Extension('jpg'), 'filename.txt'],
'txt' => [new Extension('txt'), 'filename.jpg'],
'bz2' => [new Extension('bz2'), 'filename.inc.php'],
'js' => [new Extension('js'), 'filename.foo.bar.bz2'],
'php' => [new Extension('php'), [__FILE__]],
'mp3' => [new Extension('mp3'), 999],
'gif' => [new Extension('gif'), ''],
'doc' => [new Extension('doc'), null],
];
}
}