mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Created rule "Writable".
This rule checks if the given data is a file exists and is writable.
This commit is contained in:
parent
7a6fb85b46
commit
48a9fad2c2
|
@ -286,6 +286,7 @@ Reference
|
|||
* v::file()
|
||||
* v::readable()
|
||||
* v::symbolicLink()
|
||||
* v::writable()
|
||||
|
||||
### Other
|
||||
|
||||
|
@ -1512,6 +1513,12 @@ See also:
|
|||
* v::digit()
|
||||
* v::alnum()
|
||||
|
||||
#### v::writable()
|
||||
|
||||
Validates if the given data is a file exists and is writable.
|
||||
|
||||
v::writable()->validate('/path/of/a/writable/file'); //true
|
||||
|
||||
#### v::zend($zendValidator)
|
||||
|
||||
Use Zend validators inside Respect\Validation flow. Messages
|
||||
|
|
17
library/Respect/Validation/Exceptions/WritableException.php
Normal file
17
library/Respect/Validation/Exceptions/WritableException.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class WritableException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be writable',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be writable',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
18
library/Respect/Validation/Rules/Writable.php
Normal file
18
library/Respect/Validation/Rules/Writable.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Writable extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
return $input->isWritable();
|
||||
}
|
||||
|
||||
return (is_string($input) && is_writable($input));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -85,6 +85,7 @@ use Respect\Validation\Rules\AllOf;
|
|||
* @method \Respect\Validation\Validator version()
|
||||
* @method \Respect\Validation\Validator vowel()
|
||||
* @method \Respect\Validation\Validator when(Validatable $if, Validatable $then, Validatable $when)
|
||||
* @method \Respect\Validation\Validator writable()
|
||||
* @method \Respect\Validation\Validator xdigit(string $additionalChars = null)
|
||||
* @method \Respect\Validation\Validator zend(mixed $validator, array $params = null)
|
||||
*/
|
||||
|
|
59
tests/library/Respect/Validation/Rules/WritableTest.php
Normal file
59
tests/library/Respect/Validation/Rules/WritableTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
$GLOBALS['is_writable'] = null;
|
||||
|
||||
function is_writable($writable)
|
||||
{
|
||||
$return = \is_writable($writable); // Running the real function
|
||||
if (null !== $GLOBALS['is_writable']) {
|
||||
$return = $GLOBALS['is_writable'];
|
||||
$GLOBALS['is_writable'] = null;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
class WritableTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\Writable::validate
|
||||
*/
|
||||
public function testValidWritableFileShouldReturnTrue()
|
||||
{
|
||||
$GLOBALS['is_writable'] = true;
|
||||
|
||||
$rule = new Writable();
|
||||
$input = '/path/of/a/valid/writable/file.txt';
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\Writable::validate
|
||||
*/
|
||||
public function testInvalidWritableFileShouldReturnFalse()
|
||||
{
|
||||
$GLOBALS['is_writable'] = false;
|
||||
|
||||
$rule = new Writable();
|
||||
$input = '/path/of/an/invalid/writable/file.txt';
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\Writable::validate
|
||||
*/
|
||||
public function testShouldValidateObjects()
|
||||
{
|
||||
$rule = new Writable();
|
||||
$object = $this->getMock('SplFileInfo', array('isWritable'), array('somefile.txt'));
|
||||
$object->expects($this->once())
|
||||
->method('isWritable')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->assertTrue($rule->validate($object));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue