Skip to content

Commit

Permalink
Added test for model instance methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Sandelius committed Jan 6, 2012
1 parent 623b6e4 commit b3b5696
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 57 deletions.
58 changes: 29 additions & 29 deletions data/Entity.php
Expand Up @@ -177,35 +177,35 @@ public function __isset($name) {
return isset($this->_updated[$name]); return isset($this->_updated[$name]);
} }


/** /**
* Magic method that allows calling of model methods on this record instance, i.e.: * Magic method that allows calling of model methods on this record instance, i.e.:
* {{{ * {{{
* $record->validates(); * $record->validates();
* }}} * }}}
* *
* @see lithium\data\Model::instanceMethods * @see lithium\data\Model::instanceMethods
* @param string $method * @param string $method
* @param array $params * @param array $params
* @return mixed * @return mixed
*/ */
public function __call($method, $params) { public function __call($method, $params) {
if ($model = $this->_model) { if ($model = $this->_model) {
$callback = false; $callback = false;
$methods = $model::instanceMethods(); $methods = $model::instanceMethods();
if (method_exists($model, $method)) { if (method_exists($model, $method)) {
$class = $model::invokeMethod('_object'); $class = $model::invokeMethod('_object');
$callback = array(&$class, $method); $callback = array(&$class, $method);
} elseif (isset($methods[$method]) && is_callable($methods[$method])) { } elseif (isset($methods[$method]) && is_callable($methods[$method])) {
$callback = $methods[$method]; $callback = $methods[$method];
} }
if ($callback) { if ($callback) {
array_unshift($params, $this); array_unshift($params, $this);
return call_user_func_array($callback, $params); return call_user_func_array($callback, $params);
} }
} }
$message = "No model bound or unhandled method call `{$method}`."; $message = "No model bound or unhandled method call `{$method}`.";
throw new BadMethodCallException($message); throw new BadMethodCallException($message);
} }


/** /**
* Allows several properties to be assigned at once, i.e.: * Allows several properties to be assigned at once, i.e.:
Expand Down
54 changes: 27 additions & 27 deletions data/Model.php
Expand Up @@ -295,11 +295,11 @@ class Model extends \lithium\core\StaticObject {
protected static $_baseClasses = array(__CLASS__ => true); protected static $_baseClasses = array(__CLASS__ => true);


/** /**
* Stores all custom instance methods created by `Model::instanceMethods`. * Stores all custom instance methods created by `Model::instanceMethods`.
* *
* @var array * @var array
*/ */
protected static $_instanceMethods = array(); protected static $_instanceMethods = array();


/** /**
* Sets default connection options and connects default finders. * Sets default connection options and connects default finders.
Expand Down Expand Up @@ -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`. * Getter and setter for custom instance methods. This is used in `Entity::__call`.
* *
* {{{ * {{{
* Model::instanceMethods(array( * Model::instanceMethods(array(
* 'method_name' => array('Class', 'method'), * 'method_name' => array('Class', 'method'),
* 'another_method' => array($object, 'method') * 'another_method' => array($object, 'method')
* )); * ));
* }}} * }}}
* *
* @param array $methods * @param array $methods
* @return array * @return array
*/ */
public static function instanceMethods(array $methods = null) { public static function instanceMethods(array $methods = null) {
$class = get_called_class(); $class = get_called_class();
if (!isset(static::$_instanceMethods[$class])) { if (!isset(static::$_instanceMethods[$class])) {
static::$_instanceMethods[$class] = array(); static::$_instanceMethods[$class] = array();
} }
if (!is_null($methods)) { if (!is_null($methods)) {
static::$_instanceMethods[$class] = $methods + static::$_instanceMethods[$class]; static::$_instanceMethods[$class] = $methods + static::$_instanceMethods[$class];
} }
return static::$_instanceMethods[$class]; return static::$_instanceMethods[$class];
} }


/** /**
* An instance method (called on record and document objects) to create or update the record or * An instance method (called on record and document objects) to create or update the record or
Expand Down
9 changes: 8 additions & 1 deletion tests/cases/data/EntityTest.php
Expand Up @@ -57,8 +57,15 @@ public function testIncrement() {
} }


public function testMethodDispatch() { 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()); $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`.$/"); $this->expectException("/^No model bound or unhandled method call `foo`.$/");
$entity->foo(); $entity->foo();
} }
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/data/ModelTest.php
Expand Up @@ -88,6 +88,26 @@ public function testClassInitialization() {
$this->assertEqual('mock-source', MockPost::meta('connection')); $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() { public function testMetaInformation() {
$expected = array( $expected = array(
'class' => 'lithium\tests\mocks\data\MockPost', 'class' => 'lithium\tests\mocks\data\MockPost',
Expand Down

0 comments on commit b3b5696

Please sign in to comment.