Skip to content

Commit

Permalink
Cleaning up and testing Table::validator()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent 6f9f5fa commit 13da71e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Cake/ORM/Table.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Cake\ORM\Association\HasOne; use Cake\ORM\Association\HasOne;
use Cake\ORM\BehaviorRegistry; use Cake\ORM\BehaviorRegistry;
use Cake\ORM\Entity; use Cake\ORM\Entity;
use Cake\ORM\Validator;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;


/** /**
Expand Down Expand Up @@ -788,8 +789,8 @@ public function validator($name = 'default', $instance = null) {
return $this->_validators[$name] = $instance; return $this->_validators[$name] = $instance;
} }


$validator = new Validator; $validator = new Validator($this);
$validator = $this->{'validation' . $name}($validator); $validator = $this->{'validation' . ucfirst($name)}($validator);
return $this->_validators[$name] = $validator; return $this->_validators[$name] = $validator;
} }


Expand Down
6 changes: 3 additions & 3 deletions Cake/ORM/Validator.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @since CakePHP(tm) v 2.2.0 * @since CakePHP(tm) v 2.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
namespace Cake\Model; namespace Cake\ORM;


use Cake\ORM\Validation\ValidationSet; use Cake\ORM\Validation\ValidationSet;
use Cake\Utility\Hash; use Cake\Utility\Hash;
Expand All @@ -27,7 +27,7 @@
* *
* @link http://book.cakephp.org/2.0/en/data-validation.html * @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 * Holds the ValidationSet objects array
Expand Down Expand Up @@ -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 * @param \Cake\ORM\Table $table A reference to the table object this is bound to
*/ */
public function __construct(Table $table) { public function __construct(Table $table) {
$this->_model = $model; $this->_table = $table;
} }


/** /**
Expand Down
39 changes: 39 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1797,4 +1797,43 @@ public function testHasField() {
$this->assertTrue($table->hasField('body'), 'Should be there.'); $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.