Apply contribution guidelines to "Executable" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
William Espindola 2018-06-16 22:53:49 -03:00 committed by Henrique Moody
parent c44faf0d3f
commit 01d6e40da4
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 99 additions and 41 deletions

View file

@ -13,8 +13,15 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class ExecutableException extends ValidationException
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class ExecutableException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be an executable file',

View file

@ -13,14 +13,31 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class Executable extends AbstractRule
use SplFileInfo;
use function is_executable;
use function is_scalar;
/**
* Validates if a file is an executable.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
final class Executable extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if ($input instanceof \SplFileInfo) {
if ($input instanceof SplFileInfo) {
return $input->isExecutable();
}
return is_string($input) && is_executable($input);
if (!is_scalar($input)) {
return false;
}
return is_executable((string) $input);
}
}

3
tests/fixtures/executable vendored Executable file
View file

@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
echo 'Hello world!', PHP_EOL;

View file

@ -0,0 +1,38 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\ExecutableException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::executable()->check('bar');
} catch (ExecutableException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::executable())->check('tests/fixtures/executable');
} catch (ExecutableException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::executable()->assert('bar');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::executable())->assert('tests/fixtures/executable');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"bar" must be an executable file
"tests/fixtures/executable" must not be an executable file
- "bar" must be an executable file
- "tests/fixtures/executable" must not be an executable file

View file

@ -13,54 +13,47 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
$GLOBALS['is_executable'] = null;
function is_executable($executable)
{
$return = \is_executable($executable); // Running the real function
if (null !== $GLOBALS['is_executable']) {
$return = $GLOBALS['is_executable'];
$GLOBALS['is_executable'] = null;
}
return $return;
}
use Respect\Validation\Test\RuleTestCase;
use SplFileInfo;
use SplFileObject;
/**
* @group rule
* @group rule
*
* @covers \Respect\Validation\Rules\Executable
* @covers \Respect\Validation\Exceptions\ExecutableException
*
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Royall Spence <royall@royall.us>
* @author William Espindola <oi@williamespindola.com.br>
*/
class ExecutableTest extends TestCase
final class ExecutableTest extends RuleTestCase
{
public function testValidExecutableFileShouldReturnTrue(): void
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$GLOBALS['is_executable'] = true;
$rule = new Executable();
$input = '/path/of/a/valid/executable/file.txt';
self::assertTrue($rule->validate($input));
return [
[$rule, 'tests/fixtures/executable'],
[$rule, new SplFileInfo('tests/fixtures/executable')],
[$rule, new SplFileObject('tests/fixtures/executable')],
];
}
public function testInvalidExecutableFileShouldReturnFalse(): void
{
$GLOBALS['is_executable'] = false;
$rule = new Executable();
$input = '/path/of/an/invalid/executable/file.txt';
self::assertFalse($rule->validate($input));
}
public function testShouldValidateObjects(): void
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Executable();
$object = $this->createMock('SplFileInfo', ['isExecutable'], ['somefile.txt']);
$object->expects($this->once())
->method('isExecutable')
->will($this->returnValue(true));
self::assertTrue($rule->validate($object));
return [
[$rule, 'tests/fixtures/valid-image.gif'],
[$rule, new SplFileInfo('tests/fixtures/valid-image.jpg')],
[$rule, new SplFileObject('tests/fixtures/valid-image.png')],
];
}
}