Create Executable rule

This commit is contained in:
Henrique Moody 2014-08-28 00:34:17 -03:00
parent 4f5bd8eaf6
commit e2950e6a51
5 changed files with 96 additions and 0 deletions

View file

@ -337,6 +337,7 @@ Reference
### Files
* [v::directory()](#vdirectory)
* [v::executable()](#vexecutable)
* [v::exists()](#vexists)
* [v::file()](#vfile)
* [v::readable()](#vreadable)
@ -924,6 +925,19 @@ Validates an email address.
v::email()->validate('alexandre@gaigalas.net'); //true
```
#### v::executable()
Validates if a file is an executable.
```php
v::email()->executable('script.sh'); //true
```
See also
* [v::readable()](#vreadable)
* [v::writable()](#vwritable)
#### v::exists()
Validates files or directories.

View file

@ -0,0 +1,15 @@
<?php
namespace Respect\Validation\Exceptions;
class ExecutableException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an executable file',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an executable file',
)
);
}

View file

@ -0,0 +1,15 @@
<?php
namespace Respect\Validation\Rules;
class Executable extends AbstractRule
{
public function validate($input)
{
if ($input instanceof \SplFileInfo) {
return $input->isExecutable();
}
return (is_string($input) && is_executable($input));
}
}

View file

@ -37,6 +37,7 @@ use Respect\Validation\Rules\AllOf;
* @method \Respect\Validation\Validator endsWith(mixed $endValue, bool $identical = false)
* @method \Respect\Validation\Validator equals(mixed $compareTo, bool $compareIdentical=false)
* @method \Respect\Validation\Validator even()
* @method \Respect\Validation\Validator executable()
* @method \Respect\Validation\Validator exists()
* @method \Respect\Validation\Validator file()
* @method \Respect\Validation\Validator float()

View file

@ -0,0 +1,51 @@
<?php
namespace Respect\Validation\Rules;
$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;
}
/**
* @covers Respect\Validation\Rules\Executable
*/
class ExecutableTest extends \PHPUnit_Framework_TestCase
{
public function testValidExecutableFileShouldReturnTrue()
{
$GLOBALS['is_executable'] = true;
$rule = new Executable();
$input = '/path/of/a/valid/executable/file.txt';
$this->assertTrue($rule->validate($input));
}
public function testInvalidExecutableFileShouldReturnFalse()
{
$GLOBALS['is_executable'] = false;
$rule = new Executable();
$input = '/path/of/an/invalid/executable/file.txt';
$this->assertFalse($rule->validate($input));
}
public function testShouldValidateObjects()
{
$rule = new Executable();
$object = $this->getMock('SplFileInfo', array('isExecutable'), array('somefile.txt'));
$object->expects($this->once())
->method('isExecutable')
->will($this->returnValue(true));
$this->assertTrue($rule->validate($object));
}
}