Skip to content

Commit

Permalink
Fix clone behavior of QueryBuilder (#621)
Browse files Browse the repository at this point in the history
* Update QueryBuilder.php

* Update QueryBuilderTest.php

* Update QueryBuilder.php
  • Loading branch information
Propaganistas committed May 20, 2021
1 parent c8b0590 commit 02b78e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/QueryBuilder.php
Expand Up @@ -130,6 +130,16 @@ public function __call($name, $arguments)

return $result;
}

public function clone()
{
return clone $this;
}

public function __clone()
{
$this->subject = clone $this->subject;
}

public function __get($name)
{
Expand Down
29 changes: 29 additions & 0 deletions tests/QueryBuilderTest.php
Expand Up @@ -297,4 +297,33 @@ public function it_does_not_lose_pivot_values_with_belongs_to_many_relation()
$foundTestModel->pivot->location
);
}


/** @test */
public function it_clones_the_subject_upon_cloning()
{
$queryBuilder = QueryBuilder::for(TestModel::class);

$queryBuilder1 = (clone $queryBuilder)->where('id', 1);
$queryBuilder2 = (clone $queryBuilder)->where('name', 'John Doe');

$this->assertNotSame(
$queryBuilder1->toSql(),
$queryBuilder2->toSql()
);
}

/** @test */
public function it_supports_clone_as_method()
{
$queryBuilder = QueryBuilder::for(TestModel::class);

$queryBuilder1 = $queryBuilder->clone()->where('id', 1);
$queryBuilder2 = $queryBuilder->clone()->where('name', 'John Doe');

$this->assertNotSame(
$queryBuilder1->toSql(),
$queryBuilder2->toSql()
);
}
}

0 comments on commit 02b78e2

Please sign in to comment.