mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
Apply contribution guidelines to "Directory" rule
Also consider PHP's "Directory" class as a valid directory. Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
97e94cafcc
commit
0413afc226
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `Directory()`
|
||||
|
||||
Validates directories.
|
||||
Validates if the given path is a directory.
|
||||
|
||||
```php
|
||||
v::directory()->validate(__DIR__); // true
|
||||
|
@ -13,12 +13,14 @@ This validator will consider SplFileInfo instances, so you can do something like
|
|||
|
||||
```php
|
||||
v::directory()->validate(new SplFileInfo('library/'));
|
||||
v::directory()->validate(dir('/'));
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Validates PHP's `Directory` class
|
||||
0.4.4 | Created
|
||||
|
||||
***
|
||||
|
|
|
@ -13,8 +13,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class DirectoryException extends ValidationException
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class DirectoryException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a directory',
|
||||
|
|
|
@ -13,14 +13,36 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Directory extends AbstractRule
|
||||
use Directory as NativeDirectory;
|
||||
use SplFileInfo;
|
||||
use function is_dir;
|
||||
use function is_scalar;
|
||||
|
||||
/**
|
||||
* Validates if the given path is a directory.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class Directory extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
if ($input instanceof SplFileInfo) {
|
||||
return $input->isDir();
|
||||
}
|
||||
|
||||
return is_string($input) && is_dir($input);
|
||||
if ($input instanceof NativeDirectory) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!is_scalar($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return is_dir((string) $input);
|
||||
}
|
||||
}
|
||||
|
|
37
tests/integration/rules/directory.phpt
Normal file
37
tests/integration/rules/directory.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\DirectoryException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::directory()->check('batman');
|
||||
} catch (DirectoryException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::directory())->check(dirname('/etc/'));
|
||||
} catch (DirectoryException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::directory()->assert('ppz');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::directory())->assert(dirname('/etc/'));
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"batman" must be a directory
|
||||
"/" must not be a directory
|
||||
- "ppz" must be a directory
|
||||
- "/" must not be a directory
|
|
@ -13,94 +13,49 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use SplFileInfo;
|
||||
use SplFileObject;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Directory
|
||||
* @covers \Respect\Validation\Exceptions\DirectoryException
|
||||
*
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespidolacom.br>
|
||||
*/
|
||||
class DirectoryTest extends TestCase
|
||||
final class DirectoryTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidDirectory
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testValidDirectoryShouldReturnTrue($input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new Directory();
|
||||
self::assertTrue($rule->__invoke($input));
|
||||
$rule->assert($input);
|
||||
$rule->check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidDirectory
|
||||
* @expectedException \Respect\Validation\Exceptions\DirectoryException
|
||||
*/
|
||||
public function testInvalidDirectoryShouldThrowException($input): void
|
||||
{
|
||||
$rule = new Directory();
|
||||
self::assertFalse($rule->__invoke($input));
|
||||
$rule->assert($input);
|
||||
$rule->check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForDirectoryObjects
|
||||
*/
|
||||
public function testDirectoryWithObjects($object, $valid): void
|
||||
{
|
||||
$rule = new Directory();
|
||||
self::assertEquals($valid, $rule->validate($object));
|
||||
}
|
||||
|
||||
public function providerForDirectoryObjects()
|
||||
{
|
||||
return [
|
||||
[new \SplFileInfo(__DIR__), true],
|
||||
[new \SplFileInfo(__FILE__), false],
|
||||
/*
|
||||
* PHP 5.4 does not allows to use SplFileObject with directories.
|
||||
* array(new \SplFileObject(__DIR__), true),
|
||||
*/
|
||||
[new \SplFileObject(__FILE__), false],
|
||||
[$rule, __DIR__],
|
||||
[$rule, new SplFileInfo(__DIR__)],
|
||||
[$rule, dir(__DIR__)],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForValidDirectory()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$directories = [
|
||||
sys_get_temp_dir().DIRECTORY_SEPARATOR.'dataprovider-1',
|
||||
sys_get_temp_dir().DIRECTORY_SEPARATOR.'dataprovider-2',
|
||||
sys_get_temp_dir().DIRECTORY_SEPARATOR.'dataprovider-3',
|
||||
sys_get_temp_dir().DIRECTORY_SEPARATOR.'dataprovider-4',
|
||||
sys_get_temp_dir().DIRECTORY_SEPARATOR.'dataprovider-5',
|
||||
$rule = new Directory();
|
||||
|
||||
return [
|
||||
[$rule, new SplFileInfo(__FILE__)],
|
||||
[$rule, new SplFileObject(__FILE__)],
|
||||
[$rule, ''],
|
||||
[$rule, __FILE__],
|
||||
[$rule, new \stdClass()],
|
||||
[$rule, [__DIR__]],
|
||||
];
|
||||
|
||||
return array_map(
|
||||
function ($directory) {
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0766, true);
|
||||
}
|
||||
|
||||
return [realpath($directory)];
|
||||
},
|
||||
$directories
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidDirectory()
|
||||
{
|
||||
return array_chunk(
|
||||
[
|
||||
'',
|
||||
__FILE__,
|
||||
__DIR__.'/../../../../../README.md',
|
||||
__DIR__.'/../../../../../composer.json',
|
||||
new \stdClass(),
|
||||
[__DIR__],
|
||||
],
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue