diff --git a/Cake/ORM/Behavior.php b/Cake/ORM/Behavior.php new file mode 100644 index 00000000000..a49afeb3a77 --- /dev/null +++ b/Cake/ORM/Behavior.php @@ -0,0 +1,82 @@ +Table->doSomething($arg1, $arg2);`. + * + * ## Callback methods + * + */ +class Behavior implements EventListener { + +/** + * Contains configuration settings. + * + * @var array + */ + public $settings = []; + +/** + * Get the Model callbacks this behavior is interested in. + * + * By defining one of the callback methods a behavior is assumed + * to be interested in the related event. + * + * Override this method if you need to add non-conventional event listeners. + * Or if you want you behavior to listen to non-standard events. + * + * @return array + */ + public function implementedEvents() { + $eventMap = [ + 'Model.beforeFind' => 'beforeFind', + 'Model.beforeSave' => 'beforeSave', + 'Model.afterSave' => 'afterSave', + 'Model.beforeDelete' => 'beforeDelete', + 'Model.afterDelete' => 'afterDelete', + ]; + $events = []; + foreach ($eventMap as $event => $method) { + if (method_exists($this, $method)) { + $events[$event] = $method; + } + } + return $events; + } + +} diff --git a/Cake/Test/TestApp/Model/Behavior/PersisterOneBehaviorBehavior.php b/Cake/Test/TestApp/Model/Behavior/PersisterOneBehaviorBehavior.php deleted file mode 100644 index 15eb25d3f16..00000000000 --- a/Cake/Test/TestApp/Model/Behavior/PersisterOneBehaviorBehavior.php +++ /dev/null @@ -1,28 +0,0 @@ -settings = $options; + } + + public function beforeFind(Query $query, $options = []) { + $query->where(['slug' => 'test']); + return $query; + } + + public function findNoSlug(Query $query, $options = []) { + $query->where(['slug' => null]); + return $query; + } + + public function slugify(Table $table, $value) { + return Inflector::slug($value); + } -/** - * Behavior to allow for dynamic and atomic manipulation of a Model's associations used for a find call. Most useful for limiting - * the amount of associations and data returned. - * - */ -class TestPluginPersisterTwoBehavior extends ModelBehavior { } diff --git a/Cake/Test/TestApp/Plugin/TestPlugin/Model/Behavior/TestPluginPersisterOneBehavior.php b/Cake/Test/TestApp/Plugin/TestPlugin/Model/Behavior/PersisterOneBehavior.php similarity index 59% rename from Cake/Test/TestApp/Plugin/TestPlugin/Model/Behavior/TestPluginPersisterOneBehavior.php rename to Cake/Test/TestApp/Plugin/TestPlugin/Model/Behavior/PersisterOneBehavior.php index 084a8c3b917..5e7b2218ce0 100644 --- a/Cake/Test/TestApp/Plugin/TestPlugin/Model/Behavior/TestPluginPersisterOneBehavior.php +++ b/Cake/Test/TestApp/Plugin/TestPlugin/Model/Behavior/PersisterOneBehavior.php @@ -1,11 +1,5 @@ Table = new Table(['table' => 'articles']); $this->EventManager = $this->Table->getEventManager(); $this->Behaviors = new BehaviorRegistry($this->Table); + Configure::write('App.namespace', 'TestApp'); } /** @@ -48,12 +50,15 @@ public function tearDown() { } /** - * Test loading app & core behaviors. + * Test loading behaviors. * * @return void */ public function testLoad() { - $this->markTestIncomplete('not done'); + $settings = ['replacement' => '-']; + $result = $this->Behaviors->load('Sluggable', $settings); + $this->assertInstanceOf('TestApp\Model\Behavior\SluggableBehavior', $result); + $this->assertEquals($settings, $result->settings); } /** @@ -62,7 +67,12 @@ public function testLoad() { * @return void */ public function testLoadBindEvents() { - $this->markTestIncomplete('not done'); + $result = $this->EventManager->listeners('Model.beforeFind'); + $this->assertCount(0, $result); + + $this->Behaviors->load('Sluggable'); + $result = $this->EventManager->listeners('Model.beforeFind'); + $this->assertCount(1, $result); } /** @@ -71,7 +81,12 @@ public function testLoadBindEvents() { * @return void */ public function testLoadEnabledFalse() { - $this->markTestIncomplete('not done'); + $result = $this->EventManager->listeners('Model.beforeFind'); + $this->assertCount(0, $result); + + $this->Behaviors->load('Sluggable', ['enabled' => false]); + $result = $this->EventManager->listeners('Model.beforeFind'); + $this->assertCount(0, $result); } /** @@ -80,7 +95,9 @@ public function testLoadEnabledFalse() { * @return void */ public function testLoadPlugin() { - $this->markTestIncomplete('not done'); + Plugin::load('TestPlugin'); + $result = $this->Behaviors->load('TestPlugin.PersisterOne'); + $this->assertInstanceOf('TestPlugin\Model\Behavior\PersisterOneBehavior', $result); } /** @@ -90,7 +107,7 @@ public function testLoadPlugin() { * @return void */ public function testLoadMissingClass() { - $this->markTestIncomplete('not done'); + $this->Behaviors->load('DoesNotExist'); } /**