Skip to content
Permalink
Browse files

Cleaning up and testing Table::validator()

  • Loading branch information...
lorenzo committed Oct 30, 2013
1 parent 6f9f5fa commit 13da71ecc79c6181fee0408115793b8c6fffafff
Showing with 45 additions and 5 deletions.
  1. +3 −2 Cake/ORM/Table.php
  2. +3 −3 Cake/ORM/Validator.php
  3. +39 −0 Cake/Test/TestCase/ORM/TableTest.php
@@ -27,6 +27,7 @@
use Cake\ORM\Association\HasOne;
use Cake\ORM\BehaviorRegistry;
use Cake\ORM\Entity;
use Cake\ORM\Validator;
use Cake\Utility\Inflector;
/**
@@ -788,8 +789,8 @@ public function validator($name = 'default', $instance = null) {
return $this->_validators[$name] = $instance;
}
$validator = new Validator;
$validator = $this->{'validation' . $name}($validator);
$validator = new Validator($this);
$validator = $this->{'validation' . ucfirst($name)}($validator);
return $this->_validators[$name] = $validator;
}
@@ -14,7 +14,7 @@
* @since CakePHP(tm) v 2.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model;
namespace Cake\ORM;
use Cake\ORM\Validation\ValidationSet;
use Cake\Utility\Hash;
@@ -27,7 +27,7 @@
*
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class ModelValidator implements \ArrayAccess, \IteratorAggregate, \Countable {
class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
/**
* Holds the ValidationSet objects array
@@ -79,7 +79,7 @@ class ModelValidator implements \ArrayAccess, \IteratorAggregate, \Countable {
* @param \Cake\ORM\Table $table A reference to the table object this is bound to
*/
public function __construct(Table $table) {
$this->_model = $model;
$this->_table = $table;
}
/**
@@ -1797,4 +1797,43 @@ public function testHasField() {
$this->assertTrue($table->hasField('body'), 'Should be there.');
}
/**
* Tests that there exists a default validator
*
* @return void
*/
public function testValidatorDefault() {
$table = new Table();
$validator = $table->validator();
$this->assertInstanceOf('\Cake\ORM\Validator', $validator);
$default = $table->validator('default');
$this->assertSame($validator, $default);
}
/**
* Tests that it is possible to define custom validator methods
*
* @return void
*/
public function functionTestValidationWithDefiner() {
$table = $this->getMock('\Cake\ORM\Table', ['validationForOtherStuff']);
$table->expects($this->once())->method('validationForOtherStuff')
->will($this->returnArgument(0));
$other = $table->validator('forOtherStuff');
$this->assertInstanceOf('\Cake\ORM\Validator', $other);
$this->assertNotSame($other, $table->validator());
}
/**
* Tests that it is possible to set a custom validator under a name
*
* @return void
*/
public function testValidatorSetter() {
$table = new Table;
$validator = new \Cake\ORM\Validator($table);
$table->validator('other', $validator);
$this->assertSame($validator, $table->validator('other'));
}
}

0 comments on commit 13da71e

Please sign in to comment.
You can’t perform that action at this time.