mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Tests code style fixes
This commit is contained in:
parent
f70e27af56
commit
c52612f86c
@ -4,8 +4,8 @@
|
||||
"description": "Simple, elegant, stand-alone validation library with NO dependencies",
|
||||
"keywords": ["validation", "validator", "valid"],
|
||||
"homepage": "http://github.com/vlucas/valitron",
|
||||
"license" : "BSD-3-Clause",
|
||||
"authors" : [
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Vance Lucas",
|
||||
"email": "vance@vancelucas.com",
|
||||
|
||||
@ -897,7 +897,14 @@ class Validator
|
||||
return $isInstanceOf;
|
||||
}
|
||||
|
||||
//Validate optional field
|
||||
/**
|
||||
* Validate optional field
|
||||
*
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @param $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateOptional($field, $value, $params)
|
||||
{
|
||||
//Always return true
|
||||
@ -905,7 +912,7 @@ class Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* Get array of fields and data
|
||||
* Get array of fields and data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@ -997,7 +1004,7 @@ class Validator
|
||||
}
|
||||
// Catches the case where the data isn't an array or object
|
||||
if (is_scalar($data)) {
|
||||
return array(NULL, false);
|
||||
return array(null, false);
|
||||
}
|
||||
$identifier = array_shift($identifiers);
|
||||
// Glob match
|
||||
@ -1012,8 +1019,7 @@ class Validator
|
||||
}
|
||||
}
|
||||
return array($values, true);
|
||||
}
|
||||
// Dead end, abort
|
||||
} // Dead end, abort
|
||||
elseif ($identifier === null || ! isset($data[$identifier])) {
|
||||
if ($allow_empty){
|
||||
//when empty values are allowed, we only care if the key exists
|
||||
@ -1083,7 +1089,9 @@ class Validator
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($set_to_break) break;
|
||||
if ($set_to_break) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return count($this->errors()) === 0;
|
||||
@ -1151,9 +1159,9 @@ class Validator
|
||||
* Adds a new validation rule callback that is tied to the current
|
||||
* instance only.
|
||||
*
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function addInstanceRule($name, $callback, $message = null)
|
||||
@ -1167,9 +1175,9 @@ class Validator
|
||||
/**
|
||||
* Register new validation rule callback
|
||||
*
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function addRule($name, $callback, $message = null)
|
||||
@ -1221,8 +1229,8 @@ class Validator
|
||||
/**
|
||||
* Convenience method to add a single validation rule
|
||||
*
|
||||
* @param string|callback $rule
|
||||
* @param array|string $fields
|
||||
* @param string|callback $rule
|
||||
* @param array|string $fields
|
||||
* @return Validator
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
|
||||
@ -18,7 +18,7 @@ class BaseTestCase extends TestCase
|
||||
|
||||
protected function resetProperty($name, $value = null)
|
||||
{
|
||||
$prop = new ReflectionProperty('Valitron\Validator', $name);
|
||||
$prop = new \ReflectionProperty('Valitron\Validator', $name);
|
||||
$prop->setAccessible(true);
|
||||
$prop->setValue($value);
|
||||
$prop->setAccessible(false);
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
use Valitron\Validator;
|
||||
|
||||
class ErrorMessagesTest extends BaseTestCase
|
||||
@ -60,17 +61,18 @@ class ErrorMessagesTest extends BaseTestCase
|
||||
|
||||
public function testMessageWithFieldSet()
|
||||
{
|
||||
$v = new Validator(array('name'=>''), array(), 'en', __DIR__ . '/../lang');
|
||||
$v = new Validator(array('name' => ''), array(), 'en', __DIR__ . '/../lang');
|
||||
$v->rule('required', 'name');
|
||||
$v->validate();
|
||||
$this->assertEquals( $v->errors('name'), array('A value is required for Name'));
|
||||
$this->assertEquals($v->errors('name'), array('A value is required for Name'));
|
||||
}
|
||||
|
||||
public function testMessageWithFieldAndLabelSet(){
|
||||
$v = new Validator(array('name'=>''), array(), 'en', __DIR__ . '/../lang');
|
||||
public function testMessageWithFieldAndLabelSet()
|
||||
{
|
||||
$v = new Validator(array('name' => ''), array(), 'en', __DIR__ . '/../lang');
|
||||
$v->rule('required', 'name')->label('my name');
|
||||
$v->validate();
|
||||
$this->assertEquals( $v->errors('name'), array('A value is required for my name'));
|
||||
$this->assertEquals($v->errors('name'), array('A value is required for my name'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ class LangTest extends BaseTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* Lang defined statically should not be overrided by constructor default
|
||||
* Lang defined statically should not be override by constructor default
|
||||
*/
|
||||
public function testLangDefinedStatically()
|
||||
{
|
||||
@ -20,7 +20,7 @@ class LangTest extends BaseTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* LangDir defined statically should not be overrided by constructor default
|
||||
* LangDir defined statically should not be override by constructor default
|
||||
*/
|
||||
public function testLangDirDefinedStatically()
|
||||
{
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
<?php
|
||||
use Valitron\Validator;
|
||||
|
||||
use Valitron\Validator;
|
||||
|
||||
class MapRulesTest extends BaseTestCase
|
||||
{
|
||||
public function testMapSingleFieldRules()
|
||||
{
|
||||
|
||||
$rules = array(
|
||||
'required',
|
||||
array('lengthMin', 4)
|
||||
@ -24,7 +23,6 @@ class MapRulesTest extends BaseTestCase
|
||||
|
||||
public function testSingleFieldDot()
|
||||
{
|
||||
|
||||
$v = new Valitron\Validator(array(
|
||||
'settings' => array(
|
||||
array('threshold' => 50),
|
||||
@ -36,7 +34,6 @@ class MapRulesTest extends BaseTestCase
|
||||
));
|
||||
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
}
|
||||
|
||||
public function testMapMultipleFieldsRules()
|
||||
@ -60,7 +57,6 @@ class MapRulesTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
$this->assertFalse($v->errors('myField1'));
|
||||
$this->assertEquals(2, sizeof($v->errors('myField2')));
|
||||
|
||||
}
|
||||
|
||||
public function testCustomMessageSingleField()
|
||||
@ -82,10 +78,10 @@ class MapRulesTest extends BaseTestCase
|
||||
{
|
||||
$rules = array(
|
||||
'myField1' => array(
|
||||
array('lengthMin', 14, 'message'=>'My Custom Error 1')
|
||||
array('lengthMin', 14, 'message' => 'My Custom Error 1')
|
||||
),
|
||||
'myField2' => array(
|
||||
array('lengthMin', 14, 'message'=>'My Custom Error 2')
|
||||
array('lengthMin', 14, 'message' => 'My Custom Error 2')
|
||||
)
|
||||
);
|
||||
|
||||
@ -100,8 +96,6 @@ class MapRulesTest extends BaseTestCase
|
||||
$errors1 = $v->errors('myField1');
|
||||
$this->assertEquals('My Custom Error 1', $errors1[0]);
|
||||
|
||||
|
||||
|
||||
$errors2 = $v->errors('myField2');
|
||||
$this->assertEquals('My Custom Error 2', $errors2[0]);
|
||||
}
|
||||
|
||||
@ -1,28 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Valitron\Validator;
|
||||
|
||||
class StopOnFirstFail extends BaseTestCase
|
||||
{
|
||||
public function testStopOnFirstFail()
|
||||
{
|
||||
$rules = array(
|
||||
'myField1' => array(
|
||||
array('lengthMin', 5, 'message' => 'myField1 must be 5 characters minimum'),
|
||||
array('url', 'message' => 'myField1 is not a valid url'),
|
||||
array('urlActive', 'message' => 'myField1 is not an active url')
|
||||
)
|
||||
);
|
||||
|
||||
class StopOnFirstFail extends BaseTestCase {
|
||||
$v = new Validator(array(
|
||||
'myField1' => 'myVal'
|
||||
));
|
||||
|
||||
public function testStopOnFirstFail() {
|
||||
$rules = array(
|
||||
'myField1' => array(
|
||||
array('lengthMin', 5, 'message'=>'myField1 must be 5 characters minimum'),
|
||||
array('url', 'message' => 'myField1 is not a valid url'),
|
||||
array('urlActive', 'message' => 'myField1 is not an active url')
|
||||
)
|
||||
);
|
||||
|
||||
$v = new Validator(array(
|
||||
'myField1' => 'myVal'
|
||||
));
|
||||
|
||||
$v->mapFieldsRules($rules);
|
||||
$v->stopOnFirstFail(true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$errors = $v->errors();
|
||||
$this->assertCount(1, $errors['myField1']);
|
||||
}
|
||||
$v->mapFieldsRules($rules);
|
||||
$v->stopOnFirstFail(true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$errors = $v->errors();
|
||||
$this->assertCount(1, $errors['myField1']);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1302,7 +1302,7 @@ class ValidateTest extends BaseTestCase
|
||||
|
||||
public function testFalseStillTriggersValidation()
|
||||
{
|
||||
$v = new Validator(array('test' => FALSE));
|
||||
$v = new Validator(array('test' => false));
|
||||
$v->rule('min', 'test', 5);
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user