Walther Lalk 10 years ago
parent
commit
f373785a7c
2 changed files with 43 additions and 0 deletions
  1. 3 0
      src/Datasource/EntityTrait.php
  2. 40 0
      tests/TestCase/ORM/EntityTest.php

+ 3 - 0
src/Datasource/EntityTrait.php

@@ -526,6 +526,7 @@ trait EntityTrait
     protected static function _accessor($property, $type)
     {
         $class = static::class;
+
         if (isset(static::$_accessors[$class][$type][$property])) {
             return static::$_accessors[$class][$type][$property];
         }
@@ -545,8 +546,10 @@ trait EntityTrait
             }
             $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])) {

+ 40 - 0
tests/TestCase/ORM/EntityTest.php

@@ -361,6 +361,26 @@ class EntityTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void
@@ -379,6 +399,26 @@ class EntityTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void