mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Apply contribution guidelines to "File" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
23656c9030
commit
4200704bdc
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `File()`
|
||||
|
||||
Validates files.
|
||||
Validates whether file input is as a regular filename.
|
||||
|
||||
```php
|
||||
v::file()->validate(__FILE__); // true
|
||||
|
|
|
@ -13,8 +13,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class FileException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class FileException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a file',
|
||||
|
|
|
@ -13,11 +13,24 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class File extends AbstractRule
|
||||
use SplFileInfo;
|
||||
use function is_file;
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* Validates whether file input is as a regular filename.
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class File extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
if ($input instanceof SplFileInfo) {
|
||||
return $input->isFile();
|
||||
}
|
||||
|
||||
|
|
37
tests/integration/rules/file.phpt
Normal file
37
tests/integration/rules/file.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\FileException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::file()->check('tests/fixtures/non-existent.sh');
|
||||
} catch (FileException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::file())->check('tests/fixtures/valid-image.png');
|
||||
} catch (FileException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::file()->assert('tests/fixtures/non-existent.sh');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::file())->assert('tests/fixtures/valid-image.png');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"tests/fixtures/non-existent.sh" must be a file
|
||||
"tests/fixtures/valid-image.png" must not be a file
|
||||
- "tests/fixtures/non-existent.sh" must be a file
|
||||
- "tests/fixtures/valid-image.png" must not be a file
|
|
@ -13,69 +13,53 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
$GLOBALS['is_file'] = null;
|
||||
|
||||
function is_file($file)
|
||||
{
|
||||
$return = \is_file($file); // Running the real function
|
||||
if (null !== $GLOBALS['is_file']) {
|
||||
$return = $GLOBALS['is_file'];
|
||||
$GLOBALS['is_file'] = null;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use SplFileInfo;
|
||||
use SplFileObject;
|
||||
use stdClass;
|
||||
use const PHP_INT_MAX;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\FileException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\File
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class FileTest extends TestCase
|
||||
final class FileTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Respect\Validation\Rules\File::validate
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validFileShouldReturnTrue(): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$GLOBALS['is_file'] = true;
|
||||
$sut = new File();
|
||||
|
||||
$rule = new File();
|
||||
$input = '/path/of/a/valid/file.txt';
|
||||
self::assertTrue($rule->validate($input));
|
||||
return [
|
||||
'filename' => [$sut, __FILE__],
|
||||
'SplFileInfo' => [$sut, new SplFileInfo($this->getFixtureDirectory().'/valid-image.png')],
|
||||
'SplFileObject' => [$sut, new SplFileObject($this->getFixtureDirectory().'/invalid-image.png')],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Respect\Validation\Rules\File::validate
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function invalidFileShouldReturnFalse(): void
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$GLOBALS['is_file'] = false;
|
||||
$sut = new File();
|
||||
|
||||
$rule = new File();
|
||||
$input = '/path/of/an/invalid/file.txt';
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Respect\Validation\Rules\File::validate
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldValidateObjects(): void
|
||||
{
|
||||
$rule = new File();
|
||||
$object = $this->createMock('SplFileInfo');
|
||||
$object->expects(self::once())
|
||||
->method('isFile')
|
||||
->will(self::returnValue(true));
|
||||
|
||||
self::assertTrue($rule->validate($object));
|
||||
return [
|
||||
'directory' => [$sut, __DIR__],
|
||||
'object' => [$sut, new stdClass()],
|
||||
'array' => [$sut, []],
|
||||
'invalid filename' => [$sut, 'not-a-file-at-all'],
|
||||
'integer' => [$sut, PHP_INT_MAX],
|
||||
'float' => [$sut, 1.222],
|
||||
'boolean true' => [$sut, true],
|
||||
'boolean false' => [$sut, false],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue