Skip to content

Commit

Permalink
Fixes #8516
Browse files Browse the repository at this point in the history
  • Loading branch information
dakota committed Mar 23, 2016
1 parent 46c3ac9 commit f373785
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Datasource/EntityTrait.php
Expand Up @@ -526,6 +526,7 @@ public function offsetUnset($offset)
protected static function _accessor($property, $type)
{
$class = static::class;

if (isset(static::$_accessors[$class][$type][$property])) {
return static::$_accessors[$class][$type][$property];
}
Expand All @@ -545,8 +546,10 @@ protected static function _accessor($property, $type)
}
$field = lcfirst(substr($method, 4));
$snakeField = Inflector::underscore($field);
$titleField = ucfirst($field);
static::$_accessors[$class][$prefix][$snakeField] = $method;
static::$_accessors[$class][$prefix][$field] = $method;
static::$_accessors[$class][$prefix][$titleField] = $method;
}

if (!isset(static::$_accessors[$class][$type][$property])) {
Expand Down
40 changes: 40 additions & 0 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -360,6 +360,26 @@ public function testMagicSetWithSetter()
$this->assertEquals('Dr. Jones', $entity->name);
}

/**
* Tests magic set with custom setter function using a Title cased property
*
* @return void
*/
public function testMagicSetWithSetterTitleCase()
{
$entity = $this->getMock('\Cake\ORM\Entity', ['_setName']);
$entity->expects($this->once())
->method('_setName')
->with('Jones')
->will($this->returnCallback(function ($name) {
$this->assertEquals('Jones', $name);

return 'Dr. ' . $name;
}));
$entity->Name = 'Jones';
$this->assertEquals('Dr. Jones', $entity->Name);
}

/**
* Tests the magic getter with a custom getter function
*
Expand All @@ -378,6 +398,26 @@ public function testMagicGetWithGetter()
$this->assertEquals('Dr. Jones', $entity->name);
}

/**
* Tests magic get with custom getter function using a Title cased property
*
* @return void
*/
public function testMagicGetWithGetterTitleCase()
{
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->expects($this->once())
->method('_getName')
->with('Jones')
->will($this->returnCallback(function ($name) {
$this->assertEquals('Jones', $name);

return 'Dr. ' . $name;
}));
$entity->set('Name', 'Jones');
$this->assertEquals('Dr. Jones', $entity->Name);
}

/**
* Test indirectly modifying internal properties
*
Expand Down

0 comments on commit f373785

Please sign in to comment.