mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Create Executable rule
This commit is contained in:
parent
4f5bd8eaf6
commit
e2950e6a51
14
README.md
14
README.md
|
@ -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.
|
||||
|
|
15
library/Exceptions/ExecutableException.php
Normal file
15
library/Exceptions/ExecutableException.php
Normal 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',
|
||||
)
|
||||
);
|
||||
}
|
15
library/Rules/Executable.php
Normal file
15
library/Rules/Executable.php
Normal 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));
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
51
tests/library/Respect/Validation/Rules/ExecutableTest.php
Normal file
51
tests/library/Respect/Validation/Rules/ExecutableTest.php
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue