Implemented countable interface for ModelValidator

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-05 17:46:09 -04:30
parent 81c0c3d91d
commit 3199b9029b
2 changed files with 35 additions and 7 deletions

View file

@ -27,7 +27,7 @@ App::uses('CakeValidationSet', 'Model/Validator');
* @package Cake.Model
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class ModelValidator implements ArrayAccess, IteratorAggregate {
class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
/**
* Holds the CakeValidationSet objects array
@ -479,4 +479,15 @@ class ModelValidator implements ArrayAccess, IteratorAggregate {
$this->_parseRules();
return new ArrayIterator($this->_fields);
}
/**
* Returns the numbers of fields having validation rules
*
* @return int
**/
public function count() {
$this->_parseRules();
return count($this->_fields);
}
}

View file

@ -1688,7 +1688,6 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testGetModel() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$Validator = $TestModel->validator();
@ -1702,7 +1701,6 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testArrayAccessGet() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@ -1731,7 +1729,6 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testArrayAccessExists() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@ -1747,7 +1744,6 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testArrayAccessSet() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@ -1780,7 +1776,6 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testArrayAccessUset() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@ -1795,7 +1790,6 @@ class ModelValidationTest extends BaseModelTest {
* @return void
*/
public function testIterator() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@ -1816,4 +1810,27 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals(3, $i);
}
/**
* Tests countable interface in ModelValidator
*
* @return void
*/
public function testCount() {
$TestModel = new Article();
$Validator = $TestModel->validator();
$this->assertCount(3, $Validator);
$set = array(
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
);
$Validator['other'] = $set;
$this->assertCount(4, $Validator);
unset($Validator['title']);
$this->assertCount(3, $Validator);
unset($Validator['body']);
$this->assertCount(2, $Validator);
}
}