Login   Register  
PHP Classes
elePHPant
Icontem

File: unit-test/extended_class.test.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Can Gelis  >  Simple PHP Validation class  >  unit-test/extended_class.test.php  >  Download  
File: unit-test/extended_class.test.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: Simple PHP Validation class
Validate string value according to different rules
Author: By
Last change: Merge branch 'master' of github.com:cangelis/simple-validator.git
Date: 2013-05-06 07:01
Size: 3,919 bytes
 

Contents

Class file image Download
<?php

class CustomValidator extends SimpleValidator\Validator {

    public static function 
trueStaticRule($input) {
        return 
true;
    }

    public static function 
falseStaticRule($input) {
        return 
false;
    }

    public function 
trueRule($input) {
        return 
true;
    }

    public static function 
testParamStaticRule($input$param) {
        return 
true;
    }

    public function 
testParamRule($input$param) {
        return 
true;
    }

    public static function 
multipleParams($input$param1$param2$param3) {
        if ((
$param1 == 1) && ($param2 == 2) && ($param3 == 3))
            return 
true;
        return 
false;
    }

}

class 
ExtendedClassTest extends PHPUnit_Framework_TestCase {

    public function 
testValidButNonStaticMethodRule() {
        
$rules = array(
            
'name' => array(
                
'trueRule'
            
)
        );
        try {
            
CustomValidator::validate(null$rules);
        } catch (
SimpleValidator\SimpleValidatorException $e) {
            if (
$e->getCode() == SimpleValidator\SimpleValidatorException::STATIC_METHOD)
                return;
            else
                
$this->fail("Wrong Exception Code: " $e->getCode());
        }
        
$this->fail("Could not catched Exception");
    }

    public function 
testValidAndStaticMethodRule() {
        
$rules = array(
            
'name' => array(
                
'trueStaticRule'
            
)
        );
        try {
            
$validation_result CustomValidator::validate(null$rules);
            
$this->assertTrue($validation_result->isSuccess());
        } catch (
SimpleValidator\SimpleValidatorException $e) {
            
$this->fail("Exception occured: " $e->getMessage());
        }
        return;
    }

    public function 
testInValidAndStaticMethodRule() {
        
$rules = array(
            
'name' => array(
                
'falseStaticRule'
            
)
        );
        try {
            
$validation_result CustomValidator::validate(null$rules);
            
$this->assertFalse($validation_result->isSuccess());
        } catch (
SimpleValidator\SimpleValidatorException $e) {
            
$this->fail("Exception occured: " $e->getMessage());
        }
        return;
    }

    public function 
testValidAndParamStaticMethodRule() {
        
$rules = array(
            
'name' => array(
                
'testParamStaticRule(test)'
            
)
        );
        try {
            
$validation_result CustomValidator::validate(null$rules);
            
$this->assertTrue($validation_result->isSuccess());
        } catch (
SimpleValidator\SimpleValidatorException $e) {
            
$this->fail("Exception occured: " $e->getMessage());
        }
        return;
    }

    public function 
testValidParamButNonStaticMethodRule() {
        
$rules = array(
            
'name' => array(
                
'testParamRule(test)'
            
)
        );
        try {
            
CustomValidator::validate(null$rules);
        } catch (
SimpleValidator\SimpleValidatorException $e) {
            if (
$e->getCode() == SimpleValidator\SimpleValidatorException::STATIC_METHOD)
                return;
            else
                
$this->fail("Wrong Exception Code: " $e->getCode());
        }
        
$this->fail("Could not catched Exception");
    }

    public function 
testValidMultipleParams() {
        
$rules = array(
            
'name' => array(
                
'multipleParams(1,2,3)'
            
)
        );
        
$validation CustomValidator::validate(null$rules);
        
$this->assertTrue($validation->isSuccess());
    }

    public function 
testInvalidMultipleParams() {
        
$rules = array(
            
'name' => array(
                
'multipleParams(3,2,1)'
            
)
        );
        
$validation CustomValidator::validate(null$rules);
        
$this->assertFalse($validation->isSuccess());
    }

}

?>