Skip to content

Commit

Permalink
Start implementing Behavior & tests for BehaviorRegistry
Browse files Browse the repository at this point in the history
Add/remove/modify existing test stub classes, and make new base class
that does convention based event bindings. Add tests for various loading
situations.
  • Loading branch information
markstory committed Oct 22, 2013
1 parent 862a90c commit 2559b84
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 61 deletions.
82 changes: 82 additions & 0 deletions Cake/ORM/Behavior.php
@@ -0,0 +1,82 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM;

use Cake\Event\EventListener;

/**
* Base class for behaviors.
*
* Defines the Behavior interface, and contains common model interaction
* functionality. Behaviors allow you to simulate mixins, and create
* reusable blocks of application logic, that can be reused across
* several models. Behaviors also provide a way to hook into model
* callbacks and augment their behavior.
*
* ### Mixin methods
*
* Behaviors can provide mixin like features by declaring public
* methods. These methods should expect the Table instance to be
* shifted onto the parameter list.
*
* {{{
* function doSomething(Table $table, $arg1, $arg2) {
* //do something
* }
* }}}
*
* Would be called like `$this->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;
}

}
28 changes: 0 additions & 28 deletions Cake/Test/TestApp/Model/Behavior/PersisterOneBehaviorBehavior.php

This file was deleted.

@@ -1,11 +1,5 @@
<?php
/**
* Behavior for binding management.
*
* Behavior to simplify manipulating a model's bindings when doing a find operation
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -18,14 +12,31 @@
* @since CakePHP(tm) v 1.2.0.5669
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestPlugin\Model\Behavior;
namespace TestApp\Model\Behavior;

use Cake\Model\ModelBehavior;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Utility\Inflector;

class SluggableBehavior extends Behavior {

public function __construct(Table $table, $options = []) {
$this->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 {
}
@@ -1,11 +1,5 @@
<?php
/**
* Behavior for binding management.
*
* Behavior to simplify manipulating a model's bindings when doing a find operation
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -19,12 +13,7 @@
*/
namespace TestPlugin\Model\Behavior;

use Cake\Model\ModelBehavior;
use Cake\ORM\Behavior;

/**
* 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 TestPluginPersisterOneBehavior extends ModelBehavior {
class PersisterOneBehavior extends Behavior {
}
29 changes: 23 additions & 6 deletions Cake/Test/TestCase/ORM/BehaviorRegistryTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\ORM;

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\ORM\Table;
use Cake\ORM\BehaviorRegistry;
Expand All @@ -34,6 +35,7 @@ public function setUp() {
$this->Table = new Table(['table' => 'articles']);
$this->EventManager = $this->Table->getEventManager();
$this->Behaviors = new BehaviorRegistry($this->Table);
Configure::write('App.namespace', 'TestApp');
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -90,7 +107,7 @@ public function testLoadPlugin() {
* @return void
*/
public function testLoadMissingClass() {
$this->markTestIncomplete('not done');
$this->Behaviors->load('DoesNotExist');
}

/**
Expand Down

0 comments on commit 2559b84

Please sign in to comment.