Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Prefixing with an underscore entity accessor methods to avoid confusi…
…on and discourange direct access
  • Loading branch information
lorenzo committed Jun 12, 2014
1 parent ec64e1d commit 4a453ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Datasource/EntityTrait.php
Expand Up @@ -226,7 +226,7 @@ public function set($property, $value = null, $options = []) {
continue;
}

$setter = 'set' . Inflector::camelize($p);
$setter = '_set' . Inflector::camelize($p);
if ($this->_methodExists($setter)) {
$value = $this->{$setter}($value);
}
Expand All @@ -248,7 +248,7 @@ public function &get($property) {
}

$value = null;
$method = 'get' . Inflector::camelize($property);
$method = '_get' . Inflector::camelize($property);

if (isset($this->_properties[$property])) {
$value =& $this->_properties[$property];
Expand Down
60 changes: 30 additions & 30 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -65,8 +65,8 @@ public function testSetMultiplePropertiesNoSetters() {
* @return void
*/
public function testSetOneParamWithSetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setName']);
$entity->expects($this->once())->method('setName')
$entity = $this->getMock('\Cake\ORM\Entity', ['_setName']);
$entity->expects($this->once())->method('_setName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertEquals('Jones', $name);
Expand All @@ -82,15 +82,15 @@ public function testSetOneParamWithSetter() {
* @return void
*/
public function testMultipleWithSetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setName', 'setStuff']);
$entity = $this->getMock('\Cake\ORM\Entity', ['_setName', '_setStuff']);
$entity->accessible('*', true);
$entity->expects($this->once())->method('setName')
$entity->expects($this->once())->method('_setName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertEquals('Jones', $name);
return 'Dr. ' . $name;
}));
$entity->expects($this->once())->method('setStuff')
$entity->expects($this->once())->method('_setStuff')
->with(['a', 'b'])
->will($this->returnCallback(function($stuff) {
$this->assertEquals(['a', 'b'], $stuff);
Expand All @@ -107,11 +107,11 @@ public function testMultipleWithSetter() {
* @return void
*/
public function testBypassSetters() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setName', 'setStuff']);
$entity = $this->getMock('\Cake\ORM\Entity', ['_setName', '_setStuff']);
$entity->accessible('*', true);

$entity->expects($this->never())->method('setName');
$entity->expects($this->never())->method('setStuff');
$entity->expects($this->never())->method('_setName');
$entity->expects($this->never())->method('_setStuff');

$entity->set('name', 'Jones', ['setter' => false]);
$this->assertEquals('Jones', $entity->name);
Expand Down Expand Up @@ -179,8 +179,8 @@ public function testGetNoGetters() {
* @return void
*/
public function testGetCustomGetters() {
$entity = $this->getMock('\Cake\ORM\Entity', ['getName']);
$entity->expects($this->once())->method('getName')
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->expects($this->once())->method('_getName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertSame('Jones', $name);
Expand Down Expand Up @@ -209,8 +209,8 @@ public function testMagicSet() {
* @return void
*/
public function testMagicSetWithSetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setName']);
$entity->expects($this->once())->method('setName')
$entity = $this->getMock('\Cake\ORM\Entity', ['_setName']);
$entity->expects($this->once())->method('_setName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertEquals('Jones', $name);
Expand All @@ -226,8 +226,8 @@ public function testMagicSetWithSetter() {
* @return void
*/
public function testMagicGetWithGetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['getName']);
$entity->expects($this->once())->method('getName')
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->expects($this->once())->method('_getName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertSame('Jones', $name);
Expand Down Expand Up @@ -255,8 +255,8 @@ public function testIndirectModification() {
* @return void
*/
public function testIndirectModificationWithGetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['getThings']);
$entity->expects($this->atLeastOnce())->method('getThings')
$entity = $this->getMock('\Cake\ORM\Entity', ['_getThings']);
$entity->expects($this->atLeastOnce())->method('_getThings')
->will($this->returnArgument(0));
$entity->things = ['a', 'b'];
$entity->things[] = 'c';
Expand All @@ -275,8 +275,8 @@ public function testHas() {
$this->assertFalse($entity->has('foo'));
$this->assertFalse($entity->has('last_name'));

$entity = $this->getMock('\Cake\ORM\Entity', ['getThings']);
$entity->expects($this->once())->method('getThings')
$entity = $this->getMock('\Cake\ORM\Entity', ['_getThings']);
$entity->expects($this->once())->method('_getThings')
->will($this->returnValue(0));
$this->assertTrue($entity->has('things'));
}
Expand Down Expand Up @@ -412,11 +412,11 @@ public function testUnsetArrayAccess() {
* @return void
*/
public function testMethodCache() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setFoo', 'getBar']);
$entity2 = $this->getMock('\Cake\ORM\Entity', ['setBar']);
$entity->expects($this->once())->method('setFoo');
$entity->expects($this->once())->method('getBar');
$entity2->expects($this->once())->method('setBar');
$entity = $this->getMock('\Cake\ORM\Entity', ['_setFoo', '_getBar']);
$entity2 = $this->getMock('\Cake\ORM\Entity', ['_setBar']);
$entity->expects($this->once())->method('_setFoo');
$entity->expects($this->once())->method('_getBar');
$entity2->expects($this->once())->method('_setBar');

$entity->set('foo', 1);
$entity->get('bar');
Expand All @@ -429,9 +429,9 @@ public function testMethodCache() {
* @return void
*/
public function testSetGetLongProperyNames() {
$entity = $this->getMock('\Cake\ORM\Entity', ['getVeryLongProperty', 'setVeryLongProperty']);
$entity->expects($this->once())->method('getVeryLongProperty');
$entity->expects($this->once())->method('setVeryLongProperty');
$entity = $this->getMock('\Cake\ORM\Entity', ['_getVeryLongProperty', '_setVeryLongProperty']);
$entity->expects($this->once())->method('_getVeryLongProperty');
$entity->expects($this->once())->method('_setVeryLongProperty');
$entity->get('very_long_property');
$entity->set('very_long_property', 1);
}
Expand Down Expand Up @@ -667,11 +667,11 @@ public function testToArrayRecursive() {
* @return void
*/
public function testToArrayWithAccessor() {
$entity = $this->getMock('\Cake\ORM\Entity', ['getName']);
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->accessible('*', true);
$entity->set(['name' => 'Mark', 'email' => 'mark@example.com']);
$entity->expects($this->any())
->method('getName')
->method('_getName')
->will($this->returnValue('Jose'));

$expected = ['name' => 'Jose', 'email' => 'mark@example.com'];
Expand All @@ -696,11 +696,11 @@ public function testToArrayHiddenProperties() {
* @return void
*/
public function testToArrayVirtualProperties() {
$entity = $this->getMock('\Cake\ORM\Entity', ['getName']);
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->accessible('*', true);

$entity->expects($this->any())
->method('getName')
->method('_getName')
->will($this->returnValue('Jose'));
$entity->set(['email' => 'mark@example.com']);

Expand Down

0 comments on commit 4a453ae

Please sign in to comment.