Skip to content

Commit

Permalink
Fixing an issue where using getters on a Entity field would keep modi…
Browse files Browse the repository at this point in the history
…fying the field

over and over. This comes at a cost of dropping the indirect modification feature
when using getters in entities.
  • Loading branch information
lorenzo committed Jul 27, 2014
1 parent 0d66b61 commit 456474c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/Datasource/EntityTrait.php
Expand Up @@ -257,7 +257,8 @@ public function &get($property) {
}

if ($this->_methodExists($method)) {
$value = $this->{$method}($value);
$result = $this->{$method}($value);
return $result;
}
return $value;
}
Expand Down
18 changes: 3 additions & 15 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -180,16 +180,18 @@ public function testGetNoGetters() {
*/
public function testGetCustomGetters() {
$entity = $this->getMock('\Cake\ORM\Entity', ['_getName']);
$entity->expects($this->once())->method('_getName')
$entity->expects($this->exactly(2))->method('_getName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertSame('Jones', $name);
return 'Dr. ' . $name;
}));
$entity->set('name', 'Jones');
$this->assertEquals('Dr. Jones', $entity->get('name'));
$this->assertEquals('Dr. Jones', $entity->get('name'));
}


/**
* Test magic property setting with no custom setter
*
Expand Down Expand Up @@ -249,20 +251,6 @@ public function testIndirectModification() {
$this->assertEquals(['a', 'b', 'c'], $entity->things);
}

/**
* Test indirectly modifying internal properties with a getter
*
* @return void
*/
public function testIndirectModificationWithGetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['_getThings']);
$entity->expects($this->atLeastOnce())->method('_getThings')
->will($this->returnArgument(0));
$entity->things = ['a', 'b'];
$entity->things[] = 'c';
$this->assertEquals(['a', 'b', 'c'], $entity->things);
}

/**
* Tests has() method
*
Expand Down

0 comments on commit 456474c

Please sign in to comment.