-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more tests for Validation utility
- Loading branch information
Showing
2 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,16 @@ class TestValidation extends TestCase{ | |
/** | ||
* @dataProvider dataProviderValidation | ||
*/ | ||
function testValidation($data,$validationSequence,$expectedErrors){ | ||
function testValidation($data,$validationSequence,$expectedErrors,$expectedData = null){ | ||
$originalData = json_decode(json_encode($data),true); | ||
$validation = new Validation('data', $data); | ||
call_user_func($validationSequence,$validation); | ||
$this->assertEquals($expectedErrors, $validation->getValidationErrors()); | ||
if($expectedData != null){ | ||
$this->assertEquals($expectedData, $data); | ||
}else{ | ||
$this->assertEquals($originalData, $data); | ||
} | ||
} | ||
|
||
function dataProviderValidation(){ | ||
|
@@ -94,7 +100,135 @@ function($validation){ | |
$validation->containsOnly(['a','b','c'])->key('b')->required()->key('A')->isNumeric()->in([10,20,30,40]); | ||
}, | ||
['b'=>['A'=>'invalid']] | ||
] | ||
], | ||
'require failure'=>[ | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z'], | ||
function($validation){ | ||
$validation->containsOnly(['a','b','c','d'])->key('d')->required(); | ||
}, | ||
['d'=>'missing'], | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z','d'=>null] | ||
], | ||
'default value'=>[ | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z'], | ||
function($validation){ | ||
$validation->containsOnly(['a','b','c','d'])->key('d')->defaultValue('P'); | ||
}, | ||
[], | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z','d'=>'P'] | ||
], | ||
'isNumeric Failure'=>[ | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z'], | ||
function($validation){ | ||
$validation->key('c')->isNumeric()->inRange(0); | ||
}, | ||
['c'=>'invalid'] | ||
], | ||
'isInt Success'=>[ | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->key('A')->isInt(); | ||
}, | ||
[] | ||
], | ||
'isInt Failure'=>[ | ||
['a'=>'x','b'=>['A'=>200.50],'c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->key('A')->isInt(); | ||
}, | ||
['b'=>['A'=>'invalid']] | ||
], | ||
'inRange Failure 1'=>[ | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->key('A')->isInt()->inRange(400); | ||
}, | ||
['b'=>['A'=>'invalid']] | ||
], | ||
'inRange Failure 2'=>[ | ||
['a'=>'x','b'=>['A'=>200],'c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->key('A')->isInt()->inRange(0,100); | ||
}, | ||
['b'=>['A'=>'invalid']] | ||
], | ||
'isTimestamp Success'=>[ | ||
['a'=>'x','b'=>'15th April 2018','c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->isTimestamp(); | ||
}, | ||
[] | ||
], | ||
'isTimestamp Failure'=>[ | ||
['a'=>'x','b'=>'2018-April-2802','c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->isTimestamp(); | ||
}, | ||
['b'=>'invalid'] | ||
], | ||
'isArray Success'=>[ | ||
['a'=>[['p'=>1],['q'=>2]],'b'=>'y','c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->isArray()->hasCount(1,3); | ||
}, | ||
[] | ||
], | ||
'isArray Failure'=>[ | ||
['a'=>'{"p":1,"q":2}','b'=>'y','c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->isArray()->hasCount(1,3); | ||
}, | ||
['a'=>'invalid'] | ||
], | ||
'hasCount Failure'=>[ | ||
['a'=>[['p'=>1],['q'=>2]],'b'=>'y','c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->isArray()->hasCount(3,10); | ||
}, | ||
['a'=>'invalid'] | ||
], | ||
'isBoolean Success'=>[ | ||
['a'=>'x','b'=>true,'c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->isBoolean(); | ||
}, | ||
[] | ||
], | ||
'isBoolean Failure'=>[ | ||
['a'=>'x','b'=>200,'c'=>'z'], | ||
function($validation){ | ||
$validation->key('b')->isBoolean(); | ||
}, | ||
['b'=>'invalid'] | ||
], | ||
'match Success'=>[ | ||
['a'=>'[email protected]','b'=>true,'c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->match('/([a-z]|[A-Z]|\.)*\@([a-z]|[A-Z]|\.)*/'); | ||
}, | ||
[] | ||
], | ||
'match Failure'=>[ | ||
['a'=>'Raghavendragmail.com','b'=>true,'c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->match('/([a-z]|[A-Z]|\.)*\@([a-z]|[A-Z]|\.)*/'); | ||
}, | ||
['a'=>'invalid'] | ||
], | ||
'hasLength Success'=>[ | ||
['a'=>'[email protected]','b'=>true,'c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->match('/([a-z]|[A-Z]|\.)*\@([a-z]|[A-Z]|\.)*/')->hasLength(1,255); | ||
}, | ||
[] | ||
], | ||
'hasLength Failure'=>[ | ||
['a'=>'Raghavendragmail.com','b'=>true,'c'=>'z'], | ||
function($validation){ | ||
$validation->key('a')->hasLength(1,10); | ||
}, | ||
['a'=>'invalid'] | ||
], | ||
|
||
]; | ||
} | ||
|