Skip to content

Commit

Permalink
Pushed item after a proxy loading would not be pushed into the Base c…
Browse files Browse the repository at this point in the history
…ollection, now fixed
  • Loading branch information
RemiCollin committed May 29, 2018
1 parent 41d7556 commit 9806b45
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/System/Proxies/CollectionProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,21 +840,27 @@ public function pop()
*/
public function prepend($value, $key = null)
{
$key
? $this->prependedItems[$key] = $value
: $this->prependedItems[] = $value;

return $this;
if(! $this->relationshipLoaded) {
$key
? $this->prependedItems[$key] = $value
: $this->prependedItems[] = $value;
return $this;
}

return parent::prepend($value, $key);
}

/**
* {@inheritdoc}
*/
public function push($value)
{
$this->pushedItems[] = $value;
if(! $this->relationshipLoaded) {
$this->pushedItems[] = $value;
return $this;
}

return $this;
return parent::push($value);
}

/**
Expand Down
61 changes: 59 additions & 2 deletions tests/cases/CollectionProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,32 @@ public function we_can_do_a_count_operation_on_proxy_without_loading_it()
/** @test */
public function we_can_push_to_a_collection_proxy_without_loading_it()
{
$id = $this->createRelatedSet(3);
$id = $this->createRelatedSet(0);
$user = mapper(User::class)->find($id);
$this->assertFalse($user->groups->isProxyInitialized());
$group = new Group();
$group->id = 666;
$group->name = 'added-test';
$user->groups->push($group);
$this->assertFalse($user->groups->isProxyInitialized());
$this->assertCount(1, $user->groups->getAddedItems());
mapper(User::class)->store($user);
$this->assertDatabaseHas('groups', [
'name' => 'added-test',
]);
$this->assertDatabaseHas('groups_users', [
'group_id' => 666,
'user_id' => $id,
]);
$this->clearCache();
$user = mapper(User::class)->find($user->id);
$this->assertEquals(666, $user->groups->first()->id);
}

/** @test */
public function pushed_items_are_stored_if_relationship_is_loaded_after_push()
{
$id = $this->createRelatedSet(0);
$user = mapper(User::class)->find($id);
$this->assertFalse($user->groups->isProxyInitialized());
$group = new Group();
Expand All @@ -83,6 +108,35 @@ public function we_can_push_to_a_collection_proxy_without_loading_it()
$user->groups->push($group);
$this->assertFalse($user->groups->isProxyInitialized());
$this->assertCount(1, $user->groups->getAddedItems());
$user->groups->initializeProxy();
$this->assertTrue($user->groups->isProxyInitialized());

mapper(User::class)->store($user);
$this->assertDatabaseHas('groups', [
'name' => 'added-test',
]);
$this->assertDatabaseHas('groups_users', [
'group_id' => 666,
'user_id' => $id,
]);
$this->clearCache();
$user = mapper(User::class)->find($user->id);
$this->assertEquals(666, $user->groups->first()->id);
}

/** @test */
public function pushed_items_are_stored_if_relationship_is_loaded_before_push()
{
$id = $this->createRelatedSet(0);
$user = mapper(User::class)->find($id);
$this->assertFalse($user->groups->isProxyInitialized());
$user->groups->initializeProxy();
$this->assertTrue($user->groups->isProxyInitialized());
$group = new Group();
$group->id = 666;
$group->name = 'added-test';
$user->groups->push($group);
$this->assertCount(0, $user->groups->getAddedItems());
mapper(User::class)->store($user);
$this->assertDatabaseHas('groups', [
'name' => 'added-test',
Expand All @@ -91,6 +145,9 @@ public function we_can_push_to_a_collection_proxy_without_loading_it()
'group_id' => 666,
'user_id' => $id,
]);
$this->clearCache();
$user = mapper(User::class)->find($user->id);
$this->assertEquals(666, $user->groups->first()->id);
}

/** @test */
Expand Down Expand Up @@ -128,7 +185,7 @@ public function we_can_remove_from_a_collection_proxy_without_loading_it()
protected function createRelatedSet($relatedCount = 1)
{
$userId = $this->insertUser();
for ($x = 1; $x <= $relatedCount; $x++) {
for ($x = 0; $x <= $relatedCount - 1 ; $x++) {
$groupId = $this->rawInsert('groups', [
'id' => $this->randId(),
'name' => $this->faker()->sentence,
Expand Down

0 comments on commit 9806b45

Please sign in to comment.