Guest User

PHP Class which fails unit testing

a guest
Jun 26th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.99 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Model for a Client
  4.  *
  5.  * This class holds generic information about a client of a project, providing
  6.  * first steps for multi-client-support of the application.
  7.  *
  8.  * @author Manuel Stosic
  9.  */
  10. class Application_Model_Client
  11. {
  12. //region Properties
  13.  
  14.   /**
  15.    * Filters
  16.    *
  17.    * All filters for this model data
  18.    *
  19.    * @var     array
  20.    * @access  protected
  21.    */
  22.   protected $_filters;
  23.  
  24.   /**
  25.    * Validators
  26.    *
  27.    * All validators for this model data
  28.    *
  29.    * @var     array
  30.    * @access  protected
  31.    */
  32.   protected $_validators;
  33.  
  34.   /**
  35.    * Client ID
  36.    *
  37.    * @var int
  38.    * @access protected
  39.    */
  40.   protected $_id = 0;
  41.  
  42.   /**
  43.    * Client Name
  44.    *
  45.    * @var string
  46.    * @access protected
  47.    */
  48.   protected $_name;
  49.  
  50.   /**
  51.    * Client Description
  52.    *
  53.    * @var string
  54.    * @access protected
  55.    */
  56.   protected $_description;
  57.  
  58.   /**
  59.    * Client Flag Active
  60.    *
  61.    * @var bool
  62.    * @access protected
  63.    */
  64.   protected $_flag_active;
  65.  
  66.   /**
  67.    * Client Flag Deleted
  68.    *
  69.    * @var bool
  70.    * @access protected
  71.    */
  72.   protected $_flag_deleted;
  73.  
  74. //endregion
  75.  
  76. //region Magic functions
  77.  
  78.   /**
  79.    * __construct()
  80.    *
  81.    * Constructor which sets the filters and validators
  82.    *
  83.    * @access public
  84.    * @param   mixed[] $props Array || StdClass of Properties
  85.    */
  86.   public function __construct($props = null)
  87.   {
  88.     // Set Filters
  89.     $this->_filters = array(
  90.       'id'            => array(),
  91.       'name'          => array('StringTrim', 'StripTags'),
  92.       'description'   => array('StringTrim', 'StripTags'),
  93.       'flag_active'   => array(),
  94.       'flag_deleted'  => array()
  95.     );
  96.  
  97.     // Set Validators
  98.     $this->_validators = array(
  99.       'id'            => array('Int'),
  100.       'name'          => array(new Zend_Validate_StringLength(array('min'=>4, 'max'=>50)), new Zend_Validate_Alnum(true)),
  101.       'description'   => array(new Zend_Validate_StringLength(array('max'=>5000))),
  102.       'flag_active'   => array(),
  103.       'flag_deleted'  => array()
  104.     );
  105.  
  106.     // Set Properties
  107.     if (!is_null($props)) {
  108.       $this->populate($props);
  109.     }
  110.   }
  111.  
  112. //endregion
  113.  
  114. //region Setters and getters
  115.  
  116.   /**
  117.    * setId()
  118.    *
  119.    * Sets the ID of the Client Model
  120.    *
  121.    * @param int $id ID of the Client
  122.    * @access public
  123.    * @return Application_Model_Client
  124.    */
  125.   public function setId($arg)
  126.   {
  127.     $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  128.     $input->setData(array('id'=>$arg));
  129.     if (!$input->isValid('id') || $input->id < 0) {
  130.       throw new Zend_Exception('Invalid ID provided');
  131.     }
  132.  
  133.     $this->_id = (int) $input->id;
  134.     return $this;
  135.   }
  136.  
  137.   /**
  138.    * getId()
  139.    *
  140.    * Gets the ID of the Client Model
  141.    *
  142.    * @access public
  143.    * @return integer
  144.    */
  145.   public function getId()
  146.   {
  147.     return $this->_id;
  148.   }
  149.  
  150.   /**
  151.    * setName()
  152.    *
  153.    * Sets the name of the model
  154.    *
  155.    * @access public
  156.    * @return Application_Model_Client
  157.    */
  158.   public function setName($arg)
  159.   {
  160.     $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  161.     $input->setData(array('name'=>$arg));
  162.     if (!$input->isValid('name') || is_numeric($input->name)) {
  163.       throw new Zend_Exception('Invalid NAME provided');
  164.     }
  165.     $this->_name = (string) $input->getUnescaped('name');
  166.     return $this;
  167.   }
  168.  
  169.   /**
  170.    * getName()
  171.    *
  172.    * Returns the name of the client
  173.    *
  174.    * @access public
  175.    * @return string || null
  176.    */
  177.   public function getName()
  178.   {
  179.     return $this->_name;
  180.   }
  181.  
  182.   /**
  183.    * setDescription()
  184.    *
  185.    * Sets the description of the client
  186.    *
  187.    * @access public
  188.    * @return Application_Model_Client
  189.    */
  190.   public function setDescription($arg)
  191.   {
  192.     $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  193.     $input->setData(array('description'=>$arg));
  194.     if (!$input->isValid('description')) {
  195.       throw new Zend_Exception('Invalid DESCRIPTION provided');
  196.     }
  197.     $this->_description = (string) $input->getUnescaped('description');
  198.     return $this;
  199.   }
  200.  
  201.   /**
  202.    * getDescription()
  203.    *
  204.    * Returns the description of the client
  205.    *
  206.    * @access public
  207.    * @return string || null
  208.    */
  209.   public function getDescription()
  210.   {
  211.     return $this->_description;
  212.   }
  213.  
  214.   /**
  215.    * setFlagActive()
  216.    *
  217.    * Sets the active flag of the client
  218.    *
  219.    * @access public
  220.    * @return Application_Model_Client
  221.    */
  222.   public function setFlagActive($arg)
  223.   {
  224.     $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  225.     $input->setData(array('flag_active'=>$arg));
  226.     if (!$input->isValid('flag_active')) {
  227.       throw new Zend_Exception("Invalid FLAG_ACTIVE provided\n".Zend_Debug::dump($input->getMessages()));
  228.     }
  229.     $this->_description = (string) $input->getUnescaped('flag_active');
  230.     return $this;
  231.   }
  232.  
  233.   /**
  234.    * getFlagActive()
  235.    *
  236.    * Returns the active flag of the client
  237.    *
  238.    * @access public
  239.    * @return bool || null
  240.    */
  241.   public function getFlagActive()
  242.   {
  243.     return $this->_flag_active;
  244.   }
  245.  
  246.   /**
  247.    * setFlagDeleted()
  248.    *
  249.    * Sets the deleted flag of the client
  250.    *
  251.    * @access public
  252.    * @return Application_Model_Client
  253.    */
  254.   public function setFlagDeleted($arg)
  255.   {
  256.     $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  257.     $input->setData(array('flag_deleted'=>$arg));
  258.     if (!$input->isValid('flag_deleted')) {
  259.       throw new Zend_Exception("Invalid FLAG_DELETED provided\n".Zend_Debug::dump($input->getMessages()));
  260.     }
  261.     $this->_description = (string) $input->getUnescaped('flag_deleted');
  262.     return $this;
  263.   }
  264.  
  265.   /**
  266.    * getFlagDeleted()
  267.    *
  268.    * Returns the deleted flag of the client
  269.    *
  270.    * @access public
  271.    * @return bool || null
  272.    */
  273.   public function getFlagDeleted()
  274.   {
  275.     return $this->_flag_deleted;
  276.   }
  277.  
  278.   /**
  279.    * populate()
  280.    *
  281.    * Populates the model with specified attributes
  282.    *
  283.    * @access  public
  284.    * @param   ArrayObject || array $data Data to be populated
  285.    * @return  Application_Model_Client
  286.    */
  287.   public function populate($data)
  288.   {
  289.     if (is_array($data)) {
  290.       $data = new ArrayObject($data, ArrayObject::ARRAY_AS_PROPS);
  291.     }
  292.  
  293.     if (isset($data->id))           $this->setId($data->id);
  294.     if (isset($data->name))         $this->setName($data->name);
  295.     if (isset($data->description))  $this->setDescription($data->description);
  296.     if (isset($data->flag_active))  $this->setFlagActive($data->flag_active);
  297.     if (isset($data->flag_deleted)) $this->setFlagDeleted($data->flag_deleted);
  298.   }
  299.  
  300.   /**
  301.    * toArray()
  302.    *
  303.    * Returns the objects data as an array
  304.    *
  305.    * @access public
  306.    * @return array
  307.    */
  308.   public function toArray()
  309.   {
  310.     return array(
  311.       'id'            => $this->getId(),
  312.       'name'          => $this->getName(),
  313.       'description'   => $this->getDescription(),
  314.       'flag_active'   => $this->getFlagActive(),
  315.       'flag_deleted'  => $this->getFlagDeleted()
  316.     );
  317.   }
  318.  
  319. //endregion
  320. }
Advertisement
Add Comment
Please, Sign In to add comment