Tests code style fixes

This commit is contained in:
misantron 2018-09-22 17:00:34 +03:00
parent f70e27af56
commit c52612f86c
8 changed files with 59 additions and 55 deletions

View File

@ -4,8 +4,8 @@
"description": "Simple, elegant, stand-alone validation library with NO dependencies", "description": "Simple, elegant, stand-alone validation library with NO dependencies",
"keywords": ["validation", "validator", "valid"], "keywords": ["validation", "validator", "valid"],
"homepage": "http://github.com/vlucas/valitron", "homepage": "http://github.com/vlucas/valitron",
"license" : "BSD-3-Clause", "license": "BSD-3-Clause",
"authors" : [ "authors": [
{ {
"name": "Vance Lucas", "name": "Vance Lucas",
"email": "vance@vancelucas.com", "email": "vance@vancelucas.com",

View File

@ -897,7 +897,14 @@ class Validator
return $isInstanceOf; return $isInstanceOf;
} }
//Validate optional field /**
* Validate optional field
*
* @param $field
* @param $value
* @param $params
* @return bool
*/
protected function validateOptional($field, $value, $params) protected function validateOptional($field, $value, $params)
{ {
//Always return true //Always return true
@ -905,7 +912,7 @@ class Validator
} }
/** /**
* Get array of fields and data * Get array of fields and data
* *
* @return array * @return array
*/ */
@ -997,7 +1004,7 @@ class Validator
} }
// Catches the case where the data isn't an array or object // Catches the case where the data isn't an array or object
if (is_scalar($data)) { if (is_scalar($data)) {
return array(NULL, false); return array(null, false);
} }
$identifier = array_shift($identifiers); $identifier = array_shift($identifiers);
// Glob match // Glob match
@ -1012,8 +1019,7 @@ class Validator
} }
} }
return array($values, true); return array($values, true);
} } // Dead end, abort
// Dead end, abort
elseif ($identifier === null || ! isset($data[$identifier])) { elseif ($identifier === null || ! isset($data[$identifier])) {
if ($allow_empty){ if ($allow_empty){
//when empty values are allowed, we only care if the key exists //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; return count($this->errors()) === 0;
@ -1151,9 +1159,9 @@ class Validator
* Adds a new validation rule callback that is tied to the current * Adds a new validation rule callback that is tied to the current
* instance only. * instance only.
* *
* @param string $name * @param string $name
* @param callable $callback * @param callable $callback
* @param string $message * @param string $message
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function addInstanceRule($name, $callback, $message = null) public function addInstanceRule($name, $callback, $message = null)
@ -1167,9 +1175,9 @@ class Validator
/** /**
* Register new validation rule callback * Register new validation rule callback
* *
* @param string $name * @param string $name
* @param callable $callback * @param callable $callback
* @param string $message * @param string $message
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public static function addRule($name, $callback, $message = null) public static function addRule($name, $callback, $message = null)
@ -1221,8 +1229,8 @@ class Validator
/** /**
* Convenience method to add a single validation rule * Convenience method to add a single validation rule
* *
* @param string|callback $rule * @param string|callback $rule
* @param array|string $fields * @param array|string $fields
* @return Validator * @return Validator
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */

View File

@ -18,7 +18,7 @@ class BaseTestCase extends TestCase
protected function resetProperty($name, $value = null) protected function resetProperty($name, $value = null)
{ {
$prop = new ReflectionProperty('Valitron\Validator', $name); $prop = new \ReflectionProperty('Valitron\Validator', $name);
$prop->setAccessible(true); $prop->setAccessible(true);
$prop->setValue($value); $prop->setValue($value);
$prop->setAccessible(false); $prop->setAccessible(false);

View File

@ -1,4 +1,5 @@
<?php <?php
use Valitron\Validator; use Valitron\Validator;
class ErrorMessagesTest extends BaseTestCase class ErrorMessagesTest extends BaseTestCase
@ -60,17 +61,18 @@ class ErrorMessagesTest extends BaseTestCase
public function testMessageWithFieldSet() 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->rule('required', 'name');
$v->validate(); $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(){ public function testMessageWithFieldAndLabelSet()
$v = new Validator(array('name'=>''), array(), 'en', __DIR__ . '/../lang'); {
$v = new Validator(array('name' => ''), array(), 'en', __DIR__ . '/../lang');
$v->rule('required', 'name')->label('my name'); $v->rule('required', 'name')->label('my name');
$v->validate(); $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'));
} }
} }

View File

@ -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() 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() public function testLangDirDefinedStatically()
{ {

View File

@ -1,12 +1,11 @@
<?php <?php
use Valitron\Validator;
use Valitron\Validator;
class MapRulesTest extends BaseTestCase class MapRulesTest extends BaseTestCase
{ {
public function testMapSingleFieldRules() public function testMapSingleFieldRules()
{ {
$rules = array( $rules = array(
'required', 'required',
array('lengthMin', 4) array('lengthMin', 4)
@ -24,7 +23,6 @@ class MapRulesTest extends BaseTestCase
public function testSingleFieldDot() public function testSingleFieldDot()
{ {
$v = new Valitron\Validator(array( $v = new Valitron\Validator(array(
'settings' => array( 'settings' => array(
array('threshold' => 50), array('threshold' => 50),
@ -36,7 +34,6 @@ class MapRulesTest extends BaseTestCase
)); ));
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testMapMultipleFieldsRules() public function testMapMultipleFieldsRules()
@ -60,7 +57,6 @@ class MapRulesTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
$this->assertFalse($v->errors('myField1')); $this->assertFalse($v->errors('myField1'));
$this->assertEquals(2, sizeof($v->errors('myField2'))); $this->assertEquals(2, sizeof($v->errors('myField2')));
} }
public function testCustomMessageSingleField() public function testCustomMessageSingleField()
@ -82,10 +78,10 @@ class MapRulesTest extends BaseTestCase
{ {
$rules = array( $rules = array(
'myField1' => array( 'myField1' => array(
array('lengthMin', 14, 'message'=>'My Custom Error 1') array('lengthMin', 14, 'message' => 'My Custom Error 1')
), ),
'myField2' => array( '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'); $errors1 = $v->errors('myField1');
$this->assertEquals('My Custom Error 1', $errors1[0]); $this->assertEquals('My Custom Error 1', $errors1[0]);
$errors2 = $v->errors('myField2'); $errors2 = $v->errors('myField2');
$this->assertEquals('My Custom Error 2', $errors2[0]); $this->assertEquals('My Custom Error 2', $errors2[0]);
} }

View File

@ -1,28 +1,28 @@
<?php <?php
use Valitron\Validator; 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() { $v->mapFieldsRules($rules);
$rules = array( $v->stopOnFirstFail(true);
'myField1' => array( $this->assertFalse($v->validate());
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']);
}
$errors = $v->errors();
$this->assertCount(1, $errors['myField1']);
}
} }

View File

@ -1302,7 +1302,7 @@ class ValidateTest extends BaseTestCase
public function testFalseStillTriggersValidation() public function testFalseStillTriggersValidation()
{ {
$v = new Validator(array('test' => FALSE)); $v = new Validator(array('test' => false));
$v->rule('min', 'test', 5); $v->rule('min', 'test', 5);
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }