Permalink
Browse files

Added test for model instance methods.

  • Loading branch information...
1 parent 623b6e4 commit b3b5696f5dca11f798e201c98ff47fdc1bdc937e Tobias Sandelius committed Jan 6, 2012
Showing with 84 additions and 57 deletions.
  1. +29 −29 data/Entity.php
  2. +27 −27 data/Model.php
  3. +8 −1 tests/cases/data/EntityTest.php
  4. +20 −0 tests/cases/data/ModelTest.php
View
@@ -177,35 +177,35 @@ public function __isset($name) {
return isset($this->_updated[$name]);
}
- /**
- * Magic method that allows calling of model methods on this record instance, i.e.:
- * {{{
- * $record->validates();
- * }}}
- *
- * @see lithium\data\Model::instanceMethods
- * @param string $method
- * @param array $params
- * @return mixed
- */
- public function __call($method, $params) {
- if ($model = $this->_model) {
- $callback = false;
- $methods = $model::instanceMethods();
- if (method_exists($model, $method)) {
- $class = $model::invokeMethod('_object');
- $callback = array(&$class, $method);
- } elseif (isset($methods[$method]) && is_callable($methods[$method])) {
- $callback = $methods[$method];
- }
- if ($callback) {
- array_unshift($params, $this);
- return call_user_func_array($callback, $params);
- }
- }
- $message = "No model bound or unhandled method call `{$method}`.";
- throw new BadMethodCallException($message);
- }
+ /**
+ * Magic method that allows calling of model methods on this record instance, i.e.:
+ * {{{
+ * $record->validates();
+ * }}}
+ *
+ * @see lithium\data\Model::instanceMethods
+ * @param string $method
+ * @param array $params
+ * @return mixed
+ */
+ public function __call($method, $params) {
+ if ($model = $this->_model) {
+ $callback = false;
+ $methods = $model::instanceMethods();
+ if (method_exists($model, $method)) {
+ $class = $model::invokeMethod('_object');
+ $callback = array(&$class, $method);
+ } elseif (isset($methods[$method]) && is_callable($methods[$method])) {
+ $callback = $methods[$method];
+ }
+ if ($callback) {
+ array_unshift($params, $this);
+ return call_user_func_array($callback, $params);
+ }
+ }
+ $message = "No model bound or unhandled method call `{$method}`.";
+ throw new BadMethodCallException($message);
+ }
/**
* Allows several properties to be assigned at once, i.e.:
View
@@ -295,11 +295,11 @@ class Model extends \lithium\core\StaticObject {
protected static $_baseClasses = array(__CLASS__ => true);
/**
- * Stores all custom instance methods created by `Model::instanceMethods`.
- *
- * @var array
- */
- protected static $_instanceMethods = array();
+ * Stores all custom instance methods created by `Model::instanceMethods`.
+ *
+ * @var array
+ */
+ protected static $_instanceMethods = array();
/**
* Sets default connection options and connects default finders.
@@ -692,28 +692,28 @@ public static function create(array $data = array(), array $options = array()) {
}
/**
- * Getter and setter for custom instance methods. This is used in `Entity::__call`.
- *
- * {{{
- * Model::instanceMethods(array(
- * 'method_name' => array('Class', 'method'),
- * 'another_method' => array($object, 'method')
- * ));
- * }}}
- *
- * @param array $methods
- * @return array
- */
- public static function instanceMethods(array $methods = null) {
- $class = get_called_class();
- if (!isset(static::$_instanceMethods[$class])) {
- static::$_instanceMethods[$class] = array();
- }
- if (!is_null($methods)) {
- static::$_instanceMethods[$class] = $methods + static::$_instanceMethods[$class];
- }
- return static::$_instanceMethods[$class];
- }
+ * Getter and setter for custom instance methods. This is used in `Entity::__call`.
+ *
+ * {{{
+ * Model::instanceMethods(array(
+ * 'method_name' => array('Class', 'method'),
+ * 'another_method' => array($object, 'method')
+ * ));
+ * }}}
+ *
+ * @param array $methods
+ * @return array
+ */
+ public static function instanceMethods(array $methods = null) {
+ $class = get_called_class();
+ if (!isset(static::$_instanceMethods[$class])) {
+ static::$_instanceMethods[$class] = array();
+ }
+ if (!is_null($methods)) {
+ static::$_instanceMethods[$class] = $methods + static::$_instanceMethods[$class];
+ }
+ return static::$_instanceMethods[$class];
+ }
/**
* An instance method (called on record and document objects) to create or update the record or
@@ -57,8 +57,15 @@ public function testIncrement() {
}
public function testMethodDispatch() {
- $entity = new Entity(array('model' => $this->_model, 'data' => array('foo' => true)));
+ $model = $this->_model;
+ $entity = new Entity(array('model' => $model, 'data' => array('foo' => true)));
$this->assertTrue($entity->validates());
+
+ $model::instanceMethods(array(
+ 'testInstanceMethod' => function($entity) { return 'testInstanceMethod'; }
+ ));
+ $this->assertEqual('testInstanceMethod', $entity->testInstanceMethod($entity));
+
$this->expectException("/^No model bound or unhandled method call `foo`.$/");
$entity->foo();
}
@@ -88,6 +88,26 @@ public function testClassInitialization() {
$this->assertEqual('mock-source', MockPost::meta('connection'));
}
+ public function testInstanceMethods() {
+ $methods = MockPost::instanceMethods();
+ $this->assertTrue(empty($methods));
+
+ MockPost::instanceMethods(array(
+ 'first' => array('lithium\tests\mocks\data\source\MockMongoPost', 'testInstanceMethods'),
+ 'second' => function($entity) {}
+ ));
+
+ $methods = MockPost::instanceMethods();
+ $this->assertEqual(2, count($methods));
+
+ MockPost::instanceMethods(array(
+ 'third' => function($entity) {}
+ ));
+
+ $methods = MockPost::instanceMethods();
+ $this->assertEqual(3, count($methods));
+ }
+
public function testMetaInformation() {
$expected = array(
'class' => 'lithium\tests\mocks\data\MockPost',

0 comments on commit b3b5696

Please sign in to comment.