Navigation Menu

Skip to content

Commit

Permalink
Renaming more methods and implemeting events for the rules checking
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 2, 2014
1 parent 56a6e4f commit 1f3c372
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/ORM/Rule/ExistsIn.php
Expand Up @@ -37,7 +37,7 @@ class ExistsIn {
* or the association name for the repository.
*/
public function __construct($fields, $repository) {
$this->_field = (array)$fields;
$this->_fields = (array)$fields;
$this->_repository = $repository;
}

Expand Down
29 changes: 21 additions & 8 deletions src/ORM/Table.php
Expand Up @@ -1231,7 +1231,7 @@ protected function _processSave($entity, $options) {
$entity->isNew(!$this->exists($conditions));
}

if ($options['checkRules'] && !$this->checkDomainRules($entity)) {
if ($options['checkRules'] && !$this->checkRules($entity)) {
return false;
}

Expand Down Expand Up @@ -1842,17 +1842,30 @@ public function validateUnique($value, array $options, array $context = []) {
return !$this->exists($conditions);
}

public function checkDomainRules($entity) {
$rules = $this->domainRules();
public function checkRules($entity) {
$rules = $this->rulesChecker();
$event = $this->dispatchEvent('Model.beforeRules', compact('entity', 'rules'));

if ($event->isStopped()) {
return $event->result;
}

if ($entity->isNew()) {
return $rules->checkCreate($entity);
$result = $rules->checkCreate($entity);
} else {
$result = $rules->checkUpdate($entity);
}

$event = $this->dispatchEvent('Model.afterRules', compact('entity', 'rules', 'result'));

if ($event->isStopped()) {
return $event->result;
}

return $rules->checkUpdate($entity);
return $result;
}

public function domainRules() {
public function rulesChecker() {
if ($this->_rulesChecker !== null) {
return $this->_rulesChecker;
}
Expand Down Expand Up @@ -1881,8 +1894,8 @@ public function implementedEvents() {
'Model.afterSave' => 'afterSave',
'Model.beforeDelete' => 'beforeDelete',
'Model.afterDelete' => 'afterDelete',
'Model.beforeValidate' => 'beforeValidate',
'Model.afterValidate' => 'afterValidate',
'Model.beforeRules' => 'beforeRules',
'Model.afterRules' => 'afterRules',
];
$events = [];

Expand Down
73 changes: 62 additions & 11 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\ORM;

use Cake\ORM\Entity;
use Cake\ORM\RulesChecker;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -59,7 +60,7 @@ public function testsSaveBelongsToWithValidationError() {
$table->belongsTo('authors');
$table->association('authors')
->target()
->domainRules()
->rulesChecker()
->add(function (Entity $author, array $options) use ($table) {
$this->assertSame($options['repository'], $table->association('authors')->target());
return false;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function testSaveHasOneWithValidationError() {
$table->hasOne('articles');
$table->association('articles')
->target()
->domainRules()
->rulesChecker()
->add(function (Entity $entity) {
return false;
}, ['errorField' => 'title', 'message' => 'This is an error']);
Expand Down Expand Up @@ -132,7 +133,7 @@ public function testSaveHasManyWithErrorsAtomic() {
$table->hasMany('articles');
$table->association('articles')
->target()
->domainRules()
->rulesChecker()
->add(function (Entity $entity) {
return $entity->title === '1';
}, ['errorField' => 'title', 'message' => 'This is an error']);
Expand Down Expand Up @@ -175,7 +176,7 @@ public function testSaveHasManyWithErrorsNonAtomic() {
$table->hasMany('articles');
$table->association('articles')
->target()
->domainRules()
->rulesChecker()
->add(function (Entity $article) {
return is_numeric($article->title);
}, ['errorField' => 'title', 'message' => 'This is an error']);
Expand Down Expand Up @@ -213,7 +214,7 @@ public function testSaveBelongsToManyWithValidationErrorInJointEntity() {
$table->belongsToMany('tags');
$table->association('tags')
->junction()
->domainRules()
->rulesChecker()
->add(function (Entity $entity) {
return $entity->article_id > 4;
});
Expand Down Expand Up @@ -252,7 +253,7 @@ public function testSaveBelongsToManyWithValidationErrorInJointEntityNonAtomic()
$table->belongsToMany('tags');
$table->association('tags')
->junction()
->domainRules()
->rulesChecker()
->add(function (Entity $entity) {
return $entity->tag_id > 4;
});
Expand Down Expand Up @@ -280,7 +281,7 @@ public function testIsUniqueDomainRule() {
]);

$table = TableRegistry::get('Authors');
$rules = $table->domainRules();
$rules = $table->rulesChecker();
$rules->add($rules->isUnique(['name']));

$this->assertFalse($table->save($entity));
Expand All @@ -307,7 +308,7 @@ public function testIsUniqueMultipleFields() {
]);

$table = TableRegistry::get('Articles');
$rules = $table->domainRules();
$rules = $table->rulesChecker();
$rules->add($rules->isUnique(['title', 'author_id'], 'Nope'));

$this->assertFalse($table->save($entity));
Expand All @@ -332,7 +333,7 @@ public function testExistsInDomainRule() {

$table = TableRegistry::get('Articles');
$table->belongsTo('Authors');
$rules = $table->domainRules();
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('author_id', 'Authors'));

$this->assertFalse($table->save($entity));
Expand All @@ -352,7 +353,7 @@ public function testExistsInDomainRuleWithObject() {
]);

$table = TableRegistry::get('Articles');
$rules = $table->domainRules();
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));

$this->assertFalse($table->save($entity));
Expand All @@ -372,10 +373,60 @@ public function testSkipRulesChecking() {
]);

$table = TableRegistry::get('Articles');
$rules = $table->domainRules();
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));

$this->assertSame($entity, $table->save($entity, ['checkRules' => false]));
}

/**
* Tests the beforeRules event
*
* @group save
* @return void
*/
public function testUseBeforeRules() {
$entity = new Entity([
'title' => 'An Article',
'author_id' => 500
]);

$table = TableRegistry::get('Articles');
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));

$table->eventManager()->attach(function ($event, Entity $entity, RulesChecker $check) {
$this->assertSame($event->subject()->rulesChecker(), $check);
$event->stopPropagation();
return true;
}, 'Model.beforeRules');

$this->assertSame($entity, $table->save($entity));
}

/**
* Tests the afterRules event
*
* @group save
* @return void
*/
public function testUseAfterRules() {
$entity = new Entity([
'title' => 'An Article',
'author_id' => 500
]);

$table = TableRegistry::get('Articles');
$rules = $table->rulesChecker();
$rules->add($rules->existsIn('author_id', TableRegistry::get('Authors'), 'Nope'));

$table->eventManager()->attach(function ($event, Entity $entity, RulesChecker $check, $result) {
$this->assertSame($event->subject()->rulesChecker(), $check);
$this->assertFalse($result);
$event->stopPropagation();
return true;
}, 'Model.afterRules');

$this->assertSame($entity, $table->save($entity));
}
}

0 comments on commit 1f3c372

Please sign in to comment.