Skip to content

Commit

Permalink
Making behaviors listeners of rules events
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 5, 2014
1 parent be30281 commit abfe59f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
19 changes: 12 additions & 7 deletions src/ORM/Behavior.php
Expand Up @@ -53,12 +53,16 @@
* $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.
* - `buildRules(Event $event, RulesChecker $rules)`
* Allows listeners to modify the rules checker by adding more rules.
*
* - `afterValidate(Event $event, Entity $entity, ArrayObject $options, Validator $validator)`
* Fired after an entity is validated.
* - `beforeRules(Event $event, Entity $entity, RulesChecker $rules)`
* Fired before an entity is validated using the rules checker. By stopping this event,
* you can return the final value of the rules checking operation.
*
* - `afterRules(Event $event, Entity $entity,RulesChecker $rules, bool $result)`
* Fired after the rules have been checked on the entity.By stopping this event,
* you can return the final value of the rules checking operation.
*
* - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
* Fired before each entity is saved. Stopping this event will abort the save
Expand Down Expand Up @@ -239,8 +243,9 @@ public function implementedEvents() {
'Model.afterSave' => 'afterSave',
'Model.beforeDelete' => 'beforeDelete',
'Model.afterDelete' => 'afterDelete',
'Model.beforeValidate' => 'beforeValidate',
'Model.afterValidate' => 'afterValidate',
'Model.buildRules' => 'buildRules',
'Model.beforeRules' => 'beforeRules',
'Model.afterRules' => 'afterRules',
];
$config = $this->config();
$priority = isset($config['priority']) ? $config['priority'] : null;
Expand Down
27 changes: 19 additions & 8 deletions tests/TestCase/ORM/BehaviorTest.php
Expand Up @@ -31,13 +31,19 @@ public function beforeFind() {
/**
* Test for event bindings.
*/
public function beforeValidate() {
public function beforeRules() {
}

/**
* Test for event bindings.
*/
public function afterValidate() {
public function afterRules() {
}

/**
* Test for event bindings.
*/
public function buildRules() {
}

}
Expand Down Expand Up @@ -184,8 +190,9 @@ public function testImplementedEvents() {
$behavior = new TestBehavior($table);
$expected = [
'Model.beforeFind' => 'beforeFind',
'Model.beforeValidate' => 'beforeValidate',
'Model.afterValidate' => 'afterValidate',
'Model.buildRules' => 'buildRules',
'Model.beforeRules' => 'beforeRules',
'Model.afterRules' => 'afterRules',
];
$this->assertEquals($expected, $behavior->implementedEvents());
}
Expand All @@ -203,13 +210,17 @@ public function testImplementedEventsWithPriority() {
'priority' => 10,
'callable' => 'beforeFind'
],
'Model.beforeValidate' => [
'Model.beforeRules' => [
'priority' => 10,
'callable' => 'beforeRules'
],
'Model.afterRules' => [
'priority' => 10,
'callable' => 'beforeValidate'
'callable' => 'afterRules'
],
'Model.afterValidate' => [
'Model.buildRules' => [
'priority' => 10,
'callable' => 'afterValidate'
'callable' => 'buildRules'
],
];
$this->assertEquals($expected, $behavior->implementedEvents());
Expand Down

0 comments on commit abfe59f

Please sign in to comment.