Skip to content

Commit

Permalink
Removing all validation methods from Table
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 23, 2014
1 parent bd185ac commit 12fdc5c
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 321 deletions.
2 changes: 1 addition & 1 deletion src/ORM/EntityValidator.php
Expand Up @@ -99,7 +99,7 @@ public function one(Entity $entity, $options = []) {
continue;
}

$validator = $association->target()->entityValidator();
$validator = new self($association->target());
if ($isOne) {
$valid = $validator->one($value, $assoc['options']) && $valid;
} else {
Expand Down
114 changes: 0 additions & 114 deletions src/ORM/Table.php
Expand Up @@ -1635,18 +1635,6 @@ public function marshaller() {
return new Marshaller($this);
}

/**
* Returns a new instance of an EntityValidator that is configured to be used
* for entities generated by this table. An EntityValidator can be used to
* process validation rules on a single or multiple entities and any of its
* associated values.
*
* @return EntityValidator
*/
public function entityValidator() {
return new EntityValidator($this);
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -1781,108 +1769,6 @@ public function patchEntities($entities, array $data, array $options = []) {
return $marshaller->mergeMany($entities, $data, $options);
}

/**
* Validates a single entity based on the passed options and validates
* any nested entity for this table associations as requested in the
* options array.
*
* Calling this function directly is mostly useful when you need to get
* validation errors for an entity and associated nested entities before
* they are saved.
*
* {{{
* $articles->validate($article);
* }}}
*
* You can specify which validation set to use using the options array:
*
* {{{
* $users->validate($user, ['validate' => 'forSignup']);
* }}}
*
* By default all the associations on this table will be validated if they can
* be found in the passed entity. You can limit which associations are built,
* or include deeper associations using the options parameter
*
* {{{
* $articles->validate($article, [
* 'associated' => [
* 'Tags',
* 'Comments' => [
* 'validate' => 'myCustomSet',
* 'associated' => ['Users']
* ]
* ]
* ]);
* }}}
*
* @param \Cake\Datasource\EntityInterface $entity The entity to be validated
* @param array|\ArrayObject $options A list of options to use while validating, the following
* keys are accepted:
* - validate: The name of the validation set to use
* - associated: map of association names to validate as well
* @return bool true if the passed entity and its associations are valid
*/
public function validate($entity, $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}

$entityValidator = $this->entityValidator();
return $entityValidator->one($entity, $options);
}

/**
* Validates a list of entities based on the passed options and validates
* any nested entity for this table associations as requested in the
* options array.
*
* Calling this function directly is mostly useful when you need to get
* validation errors for a list of entities and associations before they are
* saved.
*
* {{{
* $articles->validateMany([$article1, $article2]);
* }}}
*
* You can specify which validation set to use using the options array:
*
* {{{
* $users->validateMany([$user1, $user2], ['validate' => 'forSignup']);
* }}}
*
* By default all the associations on this table will be validated if they can
* be found in the passed entities. You can limit which associations are built,
* or include deeper associations using the options parameter
*
* {{{
* $articles->validateMany([$article1, $article2], [
* 'associated' => [
* 'Tags',
* 'Comments' => [
* 'validate' => 'myCustomSet',
* 'associated' => ['Users']
* ]
* ]
* ]);
* }}}
*
* @param array|\ArrayAccess $entities The entities to be validated
* @param array $options A list of options to use while validating, the following
* keys are accepted:
* - validate: The name of the validation set to use
* - associated: map of association names to validate as well
* @return bool true if the passed entities and their associations are valid
*/
public function validateMany($entities, array $options = []) {
if (!isset($options['associated'])) {
$options['associated'] = $this->_associations->keys();
}

$entityValidator = $this->entityValidator();
return $entityValidator->many($entities, $options);
}

/**
* Validator method used to check the uniqueness of a value for a column.
* This is meant to be used with the validation API and not to be called
Expand Down
102 changes: 0 additions & 102 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -2933,108 +2933,6 @@ public function testGetExceptionOnIncorrectData() {
$table->get(null);
}

/**
* 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');
$table->schema([]);

$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']);
$table->schema([]);

$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 testValidateManyDefaultAssociation() {
$table = $this->getMock('\Cake\ORM\Table', ['entityValidator']);
$table->belongsTo('users');
$table->hasMany('articles');
$table->schema([]);

$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']);
$table->schema([]);

$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));
}

/**
* Tests that patchEntity delegates the task to the marshaller and passed
* all associations
Expand Down
104 changes: 0 additions & 104 deletions tests/TestCase/ORM/ValidationIntegrationTest.php

This file was deleted.

0 comments on commit 12fdc5c

Please sign in to comment.