Permalink
Browse files

Refactoring `\data\Model::key()` to always return an associative arra…

…y in cases where a value is provided. Added test cases.
  • Loading branch information...
1 parent c73c25e commit 06326b3f80c7f7b5f82ec32cd39dcdac91e5f54a @nateabele nateabele committed Apr 27, 2010
@@ -260,7 +260,7 @@ class Model extends \lithium\core\StaticObject {
* themselves as base models using the following code:
* {{{
* public function __init() {
- * static::_isBase();
+ * static::_isBase(__CLASS__, true);
* parent::__init();
* }
* }}}
@@ -534,14 +534,12 @@ public static function key($values = array()) {
return $values->{$key};
}
- if (empty($values)) {
+ if (!$values) {
return $key;
}
- if (is_array($key)) {
- $scope = array_combine($key, array_fill(0, count($key), null));
- return array_intersect_key($values, $scope);
- }
- return isset($values[$key]) ? $values[$key] : null;
+ $key = (array) $key;
+ $scope = array_combine($key, array_fill(0, count($key), null));
+ return array_intersect_key($values, $scope);
}
/**
@@ -230,15 +230,18 @@ public function testCustomFinder() {
}
public function testCustomFindMethods() {
- // print_r(MockPost::findFirstById());
+ $result = MockPost::findFirstById(5);
+ $query = $result['query'];
+ $this->assertEqual(array('id' => 5), $query->conditions());
+ $this->assertEqual(1, $query->limit());
}
public function testKeyGeneration() {
$this->assertEqual('comment_id', MockComment::key());
$this->assertEqual(array('post_id', 'tag_id'), MockTagging::key());
$result = MockComment::key(array('comment_id' => 5, 'body' => 'This is a comment'));
- $this->assertEqual(5, $result);
+ $this->assertEqual(array('comment_id' => 5), $result);
$result = MockTagging::key(array(
'post_id' => 2,
@@ -14,6 +14,7 @@ class MockBase extends \lithium\data\Model {
public static function __init() {
static::_isBase(__CLASS__, true);
+ parent::__init();
}
}

0 comments on commit 06326b3

Please sign in to comment.