Bad use of exceptions and/or bad tests of the exceptions?

Hi,

I am not sure if this is a bad way to use exceptions and if so a bad way to test the exceptions? Currently all these SimpleTest tests pass:

function TestMinNumberOfChars(){
      //new GetClassMethods(new ValidationSelector("withinrange", new Errors()));
          $validateMinNumberOfChars  = new ValidationSelector("minnumchars", new Errors());
          $arr_min_number_of_chars = array("text"=>"password", 'minchars' => 8);
          $this->assertTrue($validateMinNumberOfChars->validateData($arr_min_number_of_chars));
          $arr_min_number_of_chars = array("text"=>"password", 'minchars' => 10);
          $this->assertFalse($validateMinNumberOfChars->validateData($arr_min_number_of_chars));
          
          try {
              $bad_data_as_string = 'bad_data_not_an_array';
              $validateMinNumberOfChars->validateData($bad_data_as_string);
          } catch (Exception $e){
              $this->assertPattern('/the past value is a string!/',$e->getMessage());
          }
          
        try {
              $bad_data_as_int = 8;
              $validateMinNumberOfChars->validateData($bad_data_as_int);
          } catch (Exception $e){
              $this->assertPattern('/the past value is a integer!/',$e->getMessage());
          }
          
        try {
              $bad_data_as_float = 0.8;
              $validateMinNumberOfChars->validateData($bad_data_as_float);
          } catch (Exception $e){
              $this->assertPattern('/the past value is a double!/',$e->getMessage());
          }
          
        try {
              $improper_array_indexs_set = array('input'=>'Array[0] index should be "text"', 'minchars'=> 8);
              $validateMinNumberOfChars->validateData($improper_array_indexs_set);
          } catch (Exception $e){
              $this->assertPattern('/In the MinUnumberOfChar object method validateData,
            Your first array index should be text with a string value/',$e->getMessage());
          }
          
        try {
              $improper_array_indexs_set = array('text'=>'this array index is correct', 'minvalue'=> 8);
              $validateMinNumberOfChars->validateData($improper_array_indexs_set);
          } catch (Exception $e){
              $this->assertPattern('/and your second index 
            should be minchars with an integer value/',$e->getMessage());
          }
    
    }

Here is the MinNumberOfChar class which is part of an overal strategy pattern:

class MinNumberOfChar implements iValidate {
    public function validateData($array){
        if(!is_array($array)){
            $type = gettype($array);
            throw new Exception("In validateArray($array) the past value is a $type!");
        }
        if (array_key_exists('text', $array) and array_key_exists('minchars', $array)){
            if ( is_string($array['text']) AND is_int($array['minchars'])){
                if(strlen($array['text']) >= $array['minchars']) {
                    return 1;
                } else {
                    return 0;
                }
            }
        } else {
            throw new Exception('In the MinUnumberOfChar object method validateData,
            Your first array index should be text with a string value and your second index 
            should be minchars with an integer value.example 
            $MinUnumberOfChar->validateData(array("text"=>"password", "minchars"=>8');
        }
        
    }

}

Do you think it would be more appropriate to trigger an error (send it to my error class) or, as the wrong type of data or keys being past are appropriate to throw an exception?

Your thoughts are appreciated.
Regards,
Steve

“past” is not “passed”.

Anyway, it looks like you’re using exceptions as a form of control structure. That is not their purpose - they should be used to handle abnormal program events, not as code branching.

Hi Michael,

past :blush:

Yes that is what was smelling bad to me. I will refactor using the error class instead.

Thanks
Steve