mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Apply contribution guidelines to "Exists" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
01d6e40da4
commit
515b23e02f
|
@ -13,8 +13,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ExistsException extends ValidationException
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class ExistsException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must exists',
|
||||
|
|
|
@ -13,8 +13,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Exists extends AbstractRule
|
||||
use function file_exists;
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
final class Exists extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
|
|
37
tests/integration/rules/exists.phpt
Normal file
37
tests/integration/rules/exists.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ExistsException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::exists()->check('/path/of/a/non-existent/file');
|
||||
} catch (ExistsException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::exists())->check('tests/fixtures/valid-image.gif');
|
||||
} catch (ExistsException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::exists()->assert('/path/of/a/non-existent/file');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::exists())->assert('tests/fixtures/valid-image.png');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"/path/of/a/non-existent/file" must exists
|
||||
"tests/fixtures/valid-image.gif" must not exists
|
||||
- "/path/of/a/non-existent/file" must exists
|
||||
- "tests/fixtures/valid-image.png" must not exists
|
|
@ -1,17 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use org\bovigo\vfs\content\LargeFileContent;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
$root = vfsStream::setup();
|
||||
|
||||
$file = vfsStream::newFile('2kb.txt')->withContent(LargeFileContent::withKilobytes(2))->at($root);
|
||||
v::exists()->check($file->url());
|
||||
|
||||
$splFile = new SplFileInfo($file->url());
|
||||
v::exists()->assert($splFile);
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,16 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ExistsException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::exists()->check('/path/of/a/non-existent/file');
|
||||
} catch (ExistsException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"/path/of/a/non-existent/file" must exists
|
||||
|
|
@ -13,55 +13,46 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use org\bovigo\vfs\content\LargeFileContent;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use SplFileInfo;
|
||||
use SplFileObject;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Exists
|
||||
* @covers \Respect\Validation\Exceptions\ExistsException
|
||||
*
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Kennedy Tedesco <kennedyt.tw@gmail.com>
|
||||
* @author William Espindola <oi@williamespindola.com.br>
|
||||
*/
|
||||
class ExistsTest extends TestCase
|
||||
final class ExistsTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider fileProvider
|
||||
* @covers \Respect\Validation\Rules\Exists::validate
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testExistentFileShouldReturnTrue($file): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new Exists();
|
||||
self::assertTrue($rule->validate($file->url()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Respect\Validation\Rules\Exists::validate
|
||||
*/
|
||||
public function testNonExistentFileShouldReturnFalse(): void
|
||||
{
|
||||
$rule = new Exists();
|
||||
self::assertFalse($rule->validate('/path/of/a/non-existent/file'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider fileProvider
|
||||
* @covers \Respect\Validation\Rules\Exists::validate
|
||||
*/
|
||||
public function testShouldValidateObjects($file): void
|
||||
{
|
||||
$rule = new Exists();
|
||||
$object = new SplFileInfo($file->url());
|
||||
self::assertTrue($rule->validate($object));
|
||||
}
|
||||
|
||||
public function fileProvider()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
$file = vfsStream::newFile('2kb.txt')->withContent(LargeFileContent::withKilobytes(2))->at($root);
|
||||
|
||||
return [
|
||||
[$file],
|
||||
[$rule, __FILE__],
|
||||
[$rule, new SplFileInfo(__FILE__)],
|
||||
[$rule, new SplFileObject(__FILE__)],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new Exists();
|
||||
|
||||
return [
|
||||
[$rule, 'path/of/a/non-existent/file'],
|
||||
[$rule, new SplFileInfo('path/of/a/non-existent/file')],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue