Skip to content

Commit

Permalink
Merge pull request #8247 from cakephp/issue-8246
Browse files Browse the repository at this point in the history
Allow camelBacked virtualProperties to be accessed.
  • Loading branch information
lorenzo committed Feb 15, 2016
2 parents 08e43f7 + 87d84f8 commit 04c5cbf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Datasource/EntityTrait.php
Expand Up @@ -535,7 +535,9 @@ protected static function _accessor($property, $type)
if ($method[0] !== '_' || ($prefix !== 'get' && $prefix !== 'set')) {
continue;
}
$field = Inflector::underscore(substr($method, 4));
$field = lcfirst(substr($method, 4));
$snakeField = Inflector::underscore($field);
static::$_accessors[$class][$prefix][$snakeField] = $method;
static::$_accessors[$class][$prefix][$field] = $method;
}

Expand Down
19 changes: 15 additions & 4 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -418,6 +418,7 @@ public function testSelectSimpleWhere()
->where(['id' => 1, 'title' => 'First Article'])
->execute();
$this->assertCount(1, $result);
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -426,6 +427,7 @@ public function testSelectSimpleWhere()
->where(['id' => 100], ['id' => 'integer'])
->execute();
$this->assertCount(0, $result);
$result->closeCursor();
}

/**
Expand All @@ -437,12 +439,13 @@ public function testSelectWhereOperators()
{
$query = new Query($this->connection);
$result = $query
->select(['title'])
->from('articles')
->where(['id >' => 1])
->select(['comment'])
->from('comments')
->where(['id >' => 4])
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['title' => 'Second Article'], $result->fetch('assoc'));
$this->assertEquals(['comment' => 'First Comment for Second Article'], $result->fetch('assoc'));
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -452,6 +455,7 @@ public function testSelectWhereOperators()
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['title' => 'First Article'], $result->fetch('assoc'));
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -460,6 +464,7 @@ public function testSelectWhereOperators()
->where(['id <=' => 2])
->execute();
$this->assertCount(2, $result);
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -468,6 +473,7 @@ public function testSelectWhereOperators()
->where(['id >=' => 1])
->execute();
$this->assertCount(3, $result);
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -476,6 +482,7 @@ public function testSelectWhereOperators()
->where(['id <=' => 1])
->execute();
$this->assertCount(1, $result);
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -485,6 +492,7 @@ public function testSelectWhereOperators()
->execute();
$this->assertCount(2, $result);
$this->assertEquals(['title' => 'First Article'], $result->fetch('assoc'));
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -494,6 +502,7 @@ public function testSelectWhereOperators()
->execute();
$this->assertCount(1, $result);
$this->assertEquals(['title' => 'First Article'], $result->fetch('assoc'));
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -502,6 +511,7 @@ public function testSelectWhereOperators()
->where(['title like' => '%Article%'])
->execute();
$this->assertCount(3, $result);
$result->closeCursor();

$query = new Query($this->connection);
$result = $query
Expand All @@ -510,6 +520,7 @@ public function testSelectWhereOperators()
->where(['title not like' => '%Article%'])
->execute();
$this->assertCount(0, $result);
$result->closeCursor();
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -312,6 +312,23 @@ public function testGetCacheClearedByUnset()
$this->assertEquals('Dr. ', $entity->get('name'));
}

/**
* Test getting camelcased virtual fields.
*
* @return void
*/
public function testGetCamelCasedProperties()
{
$entity = $this->getMock('\Cake\ORM\Entity', ['_getListIdName']);
$entity->expects($this->any())->method('_getListIdName')
->will($this->returnCallback(function ($name) {
return 'A name';
}));
$entity->virtualProperties(['ListIdName']);
$this->assertSame('A name', $entity->list_id_name, 'underscored virtual field should be accessible');
$this->assertSame('A name', $entity->listIdName, 'Camelbacked virtual field should be accessible');
}

/**
* Test magic property setting with no custom setter
*
Expand Down

0 comments on commit 04c5cbf

Please sign in to comment.