oop - Does this simple modeling example violate SOLID principles and how to unit test it -


i trying implement simple validator class system wich respect solid principles , unit testing purpose.

suppose have simple validators (mandatory, integer, greaterthan ...) , want implement more complex validator wich call several simple validators (example form validator wich use validators)

this inspired zend , other framework.

the question is, how solid principle applyed or violated here , how unit testing should done model?

i think unit test each simple validator not complex formvalidator

interface icheckable {     public function check($data); }  class mandatoryvalidator implements icheckable {     private $_property;      public function __construct($property)     {         $this->_property = $property;     }      public function check($data)     {         return isset($data[$property]);     } }  class integervalidator implements icheckable {     ... }   class formvalidator implements icheckable {     public function check($data)     {         $mandatoryvalidator = new mandatoryvalidator(array('login'));          if ($mandatoryvalidator->check($data) == false)         {                        return false;         }          $integervalidator = new integervalidator();          if ($integervalidator->check($data['amount']) == false)         {                        return false;         }          ...         return true;     } } 

first - job icheckable interface. that's start.

lets tackle s. single responsibility principle:

that's principle states, "a class should have 1 reason change" s. respected in classes? (simple responsibility.)

the 2 current validators respect this.

is formvalidator has 1 responsibility? can see 3 things:

  1. creates validators.
  2. calls 2 validators.
  3. checks validator resutls.

the problem design everytime have new validator, have create it, call it, , check return value. violates o in solid principles. (open / close)

the form validator should receive "custom" list of icheckable. "custom" list should impelment icheckable can call it. "custom" list iterate through list of icheckable. it's responsibility.

then, result has evaluated. when function returns value, have process it. in general means more code, if statement. 2 should give hint: responsibility.

so in order make solid, should pass validators callback interface serve process validator output. sample quite simple, validator returns true of false. can represented 2 "output" methods - validated() or validationfailed(). o_o, looks nice "output" interface validators , implemented formvalidator. design comply s. o. l. i. d. of principles.

remember, when first create formvalidator, have create 2 validators, custom list , connect together.

then able unit test simple classes, quickly. (try start writing test first)

note: in general if tackle s. properly, other principles easy implement.

hope helps. let me know if need more information.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -