Skip to content
Permalink
Browse files

Unit testing and fixing Table::validate* functions

  • Loading branch information...
lorenzo committed Dec 23, 2013
1 parent edbf437 commit 13b25e15a356892d8ff8298e751ffc2ca65f8f67
Showing with 96 additions and 2 deletions.
  1. +2 −2 Cake/ORM/Table.php
  2. +94 −0 Cake/Test/TestCase/ORM/TableTest.php
@@ -1604,7 +1604,7 @@ public function newEntities(array $data, $associations = null) {
* - validate: The name of the validation set to use
* - associated: map of association names to validate as well
*/
public function validate($entity, $options) {
public function validate($entity, $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associated->keys();
}
@@ -1654,7 +1654,7 @@ public function validate($entity, $options) {
* - validate: The name of the validation set to use
* - associated: map of association names to validate as well
*/
public function validateMany($entities, $options) {
public function validateMany($entities, $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associated->keys();
}
@@ -3048,4 +3048,98 @@ public function testGetException() {
$result = $table->get(10, ['contain' => ['foo']]);
}
/**
* Tests entityValidator
*
* @return void
*/
public function testEntityValidator() {
$table = new Table;
$expected = new \Cake\ORM\EntityValidator($table);
$table->entityValidator();
$this->assertEquals($expected, $table->entityValidator());
}
/**
* Tests that validate will call the entity validator with the correct
* options
*
* @return void
*/
public function testValidateDefaultAssociations() {
$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
$table->belongsTo('users');
$table->hasMany('articles');
$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
$entity = $table->newEntity([]);
$table->expects($this->once())->method('entityValidator')
->will($this->returnValue($entityValidator));
$entityValidator->expects($this->once())->method('one')
->with($entity, ['associated' => ['users', 'articles']])
->will($this->returnValue(true));
$this->assertTrue($table->validate($entity));
}
/**
* Tests that validate will call the entity validator with the correct
* options
*
* @return void
*/
public function testValidateWithCustomOptions() {
$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
$entity = $table->newEntity([]);
$options = ['associated' => ['users'], 'validate' => 'foo'];
$table->expects($this->once())->method('entityValidator')
->will($this->returnValue($entityValidator));
$entityValidator->expects($this->once())->method('one')
->with($entity, $options)
->will($this->returnValue(false));
$this->assertFalse($table->validate($entity, $options));
}
/**
* Tests that validateMany will call the entity validator with the correct
* options
*
* @return void
*/
public function testValidateManyDefaultAssociaion() {
$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
$table->belongsTo('users');
$table->hasMany('articles');
$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
$entities = ['a', 'b'];
$table->expects($this->once())->method('entityValidator')
->will($this->returnValue($entityValidator));
$entityValidator->expects($this->once())->method('many')
->with($entities, ['associated' => ['users', 'articles']])
->will($this->returnValue(true));
$this->assertTrue($table->validateMany($entities));
}
/**
* Tests that validateMany will call the entity validator with the correct
* options
*
* @return void
*/
public function testValidateManyWithCustomOptions() {
$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
$entityValidator = $this->getMock('\Cake\ORM\EntityValidator', [], [$table]);
$entities = ['a', 'b', 'c'];
$options = ['associated' => ['users'], 'validate' => 'foo'];
$table->expects($this->once())->method('entityValidator')
->will($this->returnValue($entityValidator));
$entityValidator->expects($this->once())->method('many')
->with($entities, $options)
->will($this->returnValue(false));
$this->assertFalse($table->validateMany($entities, $options));
}
}

0 comments on commit 13b25e1

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