Created rule "Writable".

This rule checks if the given data is a file exists and is writable.
This commit is contained in:
Henrique Moody 2013-02-04 22:26:38 -02:00
parent 7a6fb85b46
commit 48a9fad2c2
5 changed files with 102 additions and 0 deletions

View file

@ -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

View 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',
)
);
}

View 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));
}
}

View file

@ -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)
*/

View 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));
}
}