Validators - the beginning
I'm implementing validation to the site right now, and writing some useful little codes.
I'll share them one by one with unittests.
[cc lines="-1" lang="php" escaped="true"]
[/cc]
Valid String
Let's say you want to validate a field containing only alphanumeric characters ( a-z, A-Z, 0-9, - and _) Then you'll have a validator like: [cc lines="-1" lang="php" escaped="true"] 0; } } ?> [/cc]unit test
[cc lines="-1" lang="php" escaped="true" theme="blackboard" width="510"] object = new StringValidator(); } public function stringProvider() { return array( array('a' , true), array('Z' , true), array('0' , true), array('9' , true), array('-' , true), array('_' , true), array('aZ09-_', true), array('á' , false), array('aZ09*_', false), array('userÉ' , false), ); } /** * The test needs to prove that the the validate method * gives the $expected value to the $string. * * @dataProvider stringProvider() */ public function testValidate($string, $expected) { $actual = $this->object->validate($string); $this->assertEquals($expected, $actual); } } ?> [/cc]
Unique username
If you'd like to check that the given username is unique, then you'll have: [cc lines="-1" lang="php" escaped="true"] _db=$db; } /** * Validates that the username is unique in the database * * @param string $value Username * * @return bool true if valid, false otherwise */ public function validate($value) { $sql = 'SELECT count(*) FROM users WHERE username = '.$this->_db->escape($value); $result = $this->_db->query($sql); //if the affected rows was 0, than return true. return ($result->affected_rows == 0) ? true : false; } } ?> [/cc]More are coming. If you have an idea of some commonly used validation process, please leave a comment and we'll do it :) hope this example helps out some struggling with massive, legacy validation logic. Later on the tutorial series will show you how to chain these validators. Until then, happy coding :)unit test
[cc lines="-1" lang="php" escaped="true" theme="blackboard" width="510"] dbMock = $this->getMock('DatabaseConnection',array( 'query', 'escape' ),array(),'',false); $this->object = new UniqueNameValidator($this->dbMock); } public function setUpDbExpection($userName, $returnValue=true) { $sql = 'SELECT count(*) FROM users WHERE username = '.$userName; $this->dbMock->expects($this->at(0)) ->method('escape') ->with($this->equalTo($userName)) ->will($this->returnValue($userName)); $this->dbMock->expects($this->at(1)) ->method('query') ->with($this->equalTo($sql)) ->will( $this->returnValue( (object) array('affected_rows'=>$returnValue) ) ); } public function provider() { return array( array('notexistintgUser', (int)0), array('existingUser' , (int)1) ); } /** * Wee need the test to prove that the validate method * gives false if the username is present in the db, and * true otherwise. The Database mock will return 1 if the user exists * and 0 if not. * * @dataProvider provider() */ public function testValidateForInvalid($username, $returnValue) { $this->setUpDbExpection($username, $returnValue); $actual = $this->object->validate($username); $expected = !(bool)$returnValue; $this->assertEquals($expected, $actual); } } ?> [/cc]