Apply contribution guidelines to "SymbolicLink" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Gus Antoniassi 2018-11-28 23:16:21 -02:00 committed by Henrique Moody
parent ab87cb083d
commit b392fbb92e
No known key found for this signature in database
GPG key ID: 221E9281655813A6
6 changed files with 88 additions and 54 deletions

View file

@ -2,10 +2,12 @@
- `SymbolicLink()`
Validates if the given data is a path of a valid symbolic link.
Validates if the given input is a symbolic link.
```php
v::symbolicLink()->validate('/path/of/valid/symbolic/link'); // true
v::symbolicLink()->validate(new SplFileInfo('/path/of/valid/symbolic/link)); // true
v::symbolicLink()->validate(new SplFileObject('/path/of/valid/symbolic/link')); // true
```
## Changelog

View file

@ -14,10 +14,14 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
/**
* @author Gus Antoniassi <gus.antoniassi@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class SymbolicLinkException extends ValidationException
final class SymbolicLinkException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a symbolic link',

View file

@ -13,14 +13,24 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use SplFileInfo;
use function is_link;
use function is_string;
/**
* Validates if the given input is a symbolic link.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Gus Antoniassi <gus.antoniassi@gmail.com>
*/
class SymbolicLink extends AbstractRule
final class SymbolicLink extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if ($input instanceof \SplFileInfo) {
if ($input instanceof SplFileInfo) {
return $input->isLink();
}

View file

@ -4,7 +4,6 @@ parameters:
- phpt
ignoreErrors:
- '/Call to an undefined static method Respect\\Validation\\Validator::iDoNotExistSoIShouldThrowException/'
- '/Function Respect\\Validation\\Rules\\is_(file|readable|link|writable) not found/'
level: 1
paths:
- library/

View file

@ -0,0 +1,40 @@
--CREDITS--
Gus Antoniassi <gus.antoniassi@gmail.com>
Henrique Moody <henriquemoody@gmail.com>
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\SymbolicLinkException;
use Respect\Validation\Validator as v;
try {
v::symbolicLink()->check('tests/fixtures/fake-filename');
} catch (SymbolicLinkException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::symbolicLink())->check('tests/fixtures/symbolic-link');
} catch (SymbolicLinkException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::symbolicLink()->assert('tests/fixtures/fake-filename');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::symbolicLink())->assert('tests/fixtures/symbolic-link');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECT--
"tests/fixtures/fake-filename" must be a symbolic link
"tests/fixtures/symbolic-link" must not be a symbolic link
- "tests/fixtures/fake-filename" must be a symbolic link
- "tests/fixtures/symbolic-link" must not be a symbolic link

View file

@ -13,72 +13,51 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
$GLOBALS['is_link'] = null;
function is_link($link)
{
$return = \is_link($link);
if (null !== $GLOBALS['is_link']) {
$return = $GLOBALS['is_link'];
$GLOBALS['is_link'] = null;
}
return $return;
}
use Respect\Validation\Test\RuleTestCase;
use SplFileInfo;
use SplFileObject;
use function tmpfile;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\SymbolicLinkException
* @group rule
*
* @covers \Respect\Validation\Rules\SymbolicLink
*
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Gus Antoniassi <gus.antoniassi@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class SymbolicLinkTest extends TestCase
final class SymbolicLinkTest extends RuleTestCase
{
/**
* @covers \Respect\Validation\Rules\SymbolicLink::validate
*
* @test
* {@inheritdoc}
*/
public function validSymbolicLinkShouldReturnTrue(): void
public function providerForValidInput(): array
{
$GLOBALS['is_link'] = true;
$sut = new SymbolicLink();
$rule = new SymbolicLink();
$input = '/path/of/a/valid/link.lnk';
self::assertTrue($rule->validate($input));
return [
'filename' => [$sut, $this->getFixtureDirectory().'/symbolic-link'],
'SplFileInfo' => [$sut, new SplFileInfo($this->getFixtureDirectory().'/symbolic-link')],
'SplFileObject' => [$sut, new SplFileObject($this->getFixtureDirectory().'/symbolic-link')],
];
}
/**
* @covers \Respect\Validation\Rules\SymbolicLink::validate
*
* @test
* {@inheritdoc}
*/
public function invalidSymbolicLinkShouldThrowException(): void
public function providerForInvalidInput(): array
{
$GLOBALS['is_link'] = false;
$sut = new SymbolicLink();
$rule = new SymbolicLink();
$input = '/path/of/an/invalid/link.lnk';
self::assertFalse($rule->validate($input));
}
/**
* @covers \Respect\Validation\Rules\SymbolicLink::validate
*
* @test
*/
public function shouldValidateObjects(): void
{
$rule = new SymbolicLink();
$object = $this->createMock('SplFileInfo');
$object->expects(self::once())
->method('isLink')
->will(self::returnValue(true));
self::assertTrue($rule->validate($object));
return [
'no existing filename' => [$sut, $this->getFixtureDirectory().'/non-existing-symbolic-link'],
'no existing SplFileInfo' => [$sut, new SplFileInfo($this->getFixtureDirectory().'/non-existing-symbolic-link')],
'bool true' => [$sut, true],
'bool false' => [$sut, false],
'empty string' => [$sut, ''],
'array' => [$sut, []],
'resource' => [$sut, tmpfile()],
];
}
}