From c52612f86c0904ae5a46f8f69e31bf2e13202659 Mon Sep 17 00:00:00 2001 From: misantron Date: Sat, 22 Sep 2018 17:00:34 +0300 Subject: [PATCH] Tests code style fixes --- composer.json | 4 +-- src/Valitron/Validator.php | 36 +++++++++++++--------- tests/Valitron/BaseTestCase.php | 2 +- tests/Valitron/ErrorMessagesTest.php | 12 +++++--- tests/Valitron/LangTest.php | 4 +-- tests/Valitron/MapRulesTest.php | 12 ++------ tests/Valitron/StopOnFirstFailTest.php | 42 +++++++++++++------------- tests/Valitron/ValidateTest.php | 2 +- 8 files changed, 59 insertions(+), 55 deletions(-) diff --git a/composer.json b/composer.json index 30e5c96..43a760a 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 45c229d..8ab5f69 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -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 */ diff --git a/tests/Valitron/BaseTestCase.php b/tests/Valitron/BaseTestCase.php index ccfc736..40cf559 100644 --- a/tests/Valitron/BaseTestCase.php +++ b/tests/Valitron/BaseTestCase.php @@ -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); diff --git a/tests/Valitron/ErrorMessagesTest.php b/tests/Valitron/ErrorMessagesTest.php index 94f3333..583b1dd 100644 --- a/tests/Valitron/ErrorMessagesTest.php +++ b/tests/Valitron/ErrorMessagesTest.php @@ -1,4 +1,5 @@ ''), 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')); } } diff --git a/tests/Valitron/LangTest.php b/tests/Valitron/LangTest.php index 659d5f1..200519f 100644 --- a/tests/Valitron/LangTest.php +++ b/tests/Valitron/LangTest.php @@ -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() { diff --git a/tests/Valitron/MapRulesTest.php b/tests/Valitron/MapRulesTest.php index 46293e4..21e0c3a 100644 --- a/tests/Valitron/MapRulesTest.php +++ b/tests/Valitron/MapRulesTest.php @@ -1,12 +1,11 @@ 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]); } diff --git a/tests/Valitron/StopOnFirstFailTest.php b/tests/Valitron/StopOnFirstFailTest.php index 656427d..1e624f6 100644 --- a/tests/Valitron/StopOnFirstFailTest.php +++ b/tests/Valitron/StopOnFirstFailTest.php @@ -1,28 +1,28 @@ 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']); + } } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index c74697f..d33977d 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -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()); }