Skip to content

Commit

Permalink
[8.x] QueryBuilder add clone() (#34780)
Browse files Browse the repository at this point in the history
* cover existing clone methods with tests

* add clone() with test
  • Loading branch information
nuernbergerA committed Oct 12, 2020
1 parent 158c43c commit 4f59d32
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Expand Up @@ -3233,6 +3233,16 @@ protected function isQueryable($value)
$value instanceof Closure;
}

/**
* Clone the query.
*
* @return static
*/
public function clone()
{
return clone $this;
}

/**
* Clone the query without the given properties.
*
Expand All @@ -3241,7 +3251,7 @@ protected function isQueryable($value)
*/
public function cloneWithout(array $properties)
{
return tap(clone $this, function ($clone) use ($properties) {
return tap($this->clone(), function ($clone) use ($properties) {
foreach ($properties as $property) {
$clone->{$property} = null;
}
Expand All @@ -3256,7 +3266,7 @@ public function cloneWithout(array $properties)
*/
public function cloneWithoutBindings(array $except)
{
return tap(clone $this, function ($clone) use ($except) {
return tap($this->clone(), function ($clone) use ($except) {
foreach ($except as $type) {
$clone->bindings[$type] = [];
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -3749,6 +3749,40 @@ public function testFromQuestionMarkOperatorOnPostgres()
$this->assertSame('select * from "users" where "roles" ??& ?', $builder->toSql());
}

public function testClone()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users');
$clone = $builder->clone()->where('email', 'foo');

$this->assertNotSame($builder, $clone);
$this->assertSame('select * from "users"', $builder->toSql());
$this->assertSame('select * from "users" where "email" = ?', $clone->toSql());
}

public function testCloneWithout()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('email', 'foo')->orderBy('email');
$clone = $builder->cloneWithout(['orders']);

$this->assertSame('select * from "users" where "email" = ? order by "email" asc', $builder->toSql());
$this->assertSame('select * from "users" where "email" = ?', $clone->toSql());
}

public function testCloneWithoutBindings()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('email', 'foo')->orderBy('email');
$clone = $builder->cloneWithout(['wheres'])->cloneWithoutBindings(['where']);

$this->assertSame('select * from "users" where "email" = ? order by "email" asc', $builder->toSql());
$this->assertEquals([0 => 'foo'], $builder->getBindings());

$this->assertSame('select * from "users" order by "email" asc', $clone->toSql());
$this->assertEquals([], $clone->getBindings());
}

protected function getConnection()
{
$connection = m::mock(ConnectionInterface::class);
Expand Down

0 comments on commit 4f59d32

Please sign in to comment.