Skip to content

Commit

Permalink
Merge pull request #19 from themrwilliams/byref-property-fix
Browse files Browse the repository at this point in the history
Fix issue where by-ref array functions didn't modify Entity properties.
  • Loading branch information
jeremyharris authored Mar 6, 2018
2 parents e3fdd5f + 7b213bb commit 7e1bcc1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/LazyLoadEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait LazyLoadEntityTrait
*/
public function &get($property)
{
$get = $this->_parentGet($property);
$get = &$this->_parentGet($property);

if ($get === null) {
$get = $this->_lazyLoad($property);
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/ORM/LazyLoadEntityTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
namespace JeremyHarris\LazyLoad\Test\TestCase\ORM;

use Cake\Datasource\EntityInterface;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use JeremyHarris\LazyLoad\TestApp\Model\Entity\Comment;
use JeremyHarris\LazyLoad\TestApp\Model\Entity\LazyLoadableEntity;
use JeremyHarris\LazyLoad\TestApp\Model\Entity\TablelessEntity;
use JeremyHarris\LazyLoad\TestApp\Model\Entity\User;
use JeremyHarris\LazyLoad\TestApp\Model\Table\ArticlesTable;

/**
* LazyLoadEntityTrait test
* @property ArticlesTable $Articles
*/
class LazyLoadEntityTraitTest extends TestCase
{
Expand Down Expand Up @@ -384,4 +387,32 @@ public function testDuplicateTrait()
$user = $this->Users->get(1);
$this->assertTrue($user->has('comments'));
}

/**
* @return void
*/
public function testByRefArrayFunctionsWorkOnNormalCakeEntities()
{
$this->Articles->setEntityClass(Entity::class);

$article = $this->Articles->get(1, ['contain' => ['Tags']]);
$newTag = new Entity();

$this->assertCount(2, $article->tags);
array_unshift($article->tags, $newTag);
$this->assertCount(3, $article->tags);
}

/**
* @return void
*/
public function testByRefArrayFunctionsWorkOnLazyLoadCakeEntities()
{
$article = $this->Articles->get(1, ['contain' => ['Tags']]);
$newTag = new Entity();

$this->assertCount(2, $article->tags);
array_unshift($article->tags, $newTag);
$this->assertCount(3, $article->tags);
}
}

0 comments on commit 7e1bcc1

Please sign in to comment.