Skip to content
Permalink
Browse files

Add beforeValidate, afterValidate callbacks for behaviors.

  • Loading branch information...
ADmad committed Sep 5, 2014
1 parent 23f3aec commit 5db7bb41e73647dcab118d3d750c05d80ffce558
Showing with 33 additions and 2 deletions.
  1. +9 −0 src/ORM/Behavior.php
  2. +24 −2 tests/TestCase/ORM/BehaviorTest.php
@@ -53,6 +53,13 @@
* $primary parameter indicates whether or not this is the root query,
* or an associated query.
*
* - `beforeValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
* Fired before an entity is validated. By stopping this event, you can abort
* the validate + save operations.
*
* - `afterValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
* Fired after an entity is validated.
*
* - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
* Fired before each entity is saved. Stopping this event will abort the save
* operation. When the event is stopped the result of the event will be returned.
@@ -211,6 +218,8 @@ public function implementedEvents() {
'Model.afterSave' => 'afterSave',
'Model.beforeDelete' => 'beforeDelete',
'Model.afterDelete' => 'afterDelete',
'Model.beforeValidate' => 'beforeValidate',
'Model.afterValidate' => 'afterValidate',
];
$config = $this->config();
$priority = isset($config['priority']) ? $config['priority'] : null;
@@ -28,6 +28,18 @@ class TestBehavior extends Behavior {
public function beforeFind() {
}
/**
* Test for event bindings.
*/
public function beforeValidate() {
}
/**
* Test for event bindings.
*/
public function afterValidate() {
}
}
/**
@@ -171,7 +183,9 @@ public function testImplementedEvents() {
$table = $this->getMock('Cake\ORM\Table');
$behavior = new TestBehavior($table);
$expected = [
'Model.beforeFind' => 'beforeFind'
'Model.beforeFind' => 'beforeFind',
'Model.beforeValidate' => 'beforeValidate',
'Model.afterValidate' => 'afterValidate',
];
$this->assertEquals($expected, $behavior->implementedEvents());
}
@@ -188,7 +202,15 @@ public function testImplementedEventsWithPriority() {
'Model.beforeFind' => [
'priority' => 10,
'callable' => 'beforeFind'
]
],
'Model.beforeValidate' => [
'priority' => 10,
'callable' => 'beforeValidate'
],
'Model.afterValidate' => [
'priority' => 10,
'callable' => 'afterValidate'
],
];
$this->assertEquals($expected, $behavior->implementedEvents());
}

0 comments on commit 5db7bb4

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