-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Handle mixed-type values in compileInsert (#53948)
* Fix: Prevent incorrect handling of single-value inserts by checking if $values itself is a list using `array_is_list` instead of checking if the first element (`reset($values)`) is an array. * Add tests for `compileInsert` with different value structures * Refactor: Improve insert value handling for non-list arrays * formatting * formatting * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
502b489
commit 7d0a3d3
Showing
2 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ | |
|
||
namespace Illuminate\Tests\Database; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Database\Query\Expression; | ||
use Illuminate\Database\Query\Grammars\Grammar; | ||
use Illuminate\Database\Query\Processors\Processor; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
|
@@ -41,4 +43,79 @@ public function testWhereRawReturnsStringWhenStringPassed() | |
|
||
$this->assertSame('select * from "users"', $rawQuery); | ||
} | ||
|
||
public function testCompileInsertSingleValue() | ||
{ | ||
$builder = $this->getBuilder(); | ||
$grammar = $builder->getGrammar(); | ||
|
||
$sql = $grammar->compileInsert($builder, ['name' => 'John Doe', 'email' => '[email protected]']); | ||
$this->assertSame('insert into "users" ("name", "email") values (?, ?)', $sql); | ||
} | ||
|
||
public function testCompileInsertMultipleValues() | ||
{ | ||
$builder = $this->getBuilder(); | ||
$grammar = $builder->getGrammar(); | ||
$values = [ | ||
['name' => 'John Doe', 'email' => '[email protected]'], | ||
['name' => 'Alice Wong', 'email' => '[email protected]'], | ||
]; | ||
|
||
$sql = $grammar->compileInsert($builder, $values); | ||
$this->assertSame('insert into "users" ("name", "email") values (?, ?), (?, ?)', $sql); | ||
} | ||
|
||
public function testCompileInsertSingleValueWhereFirstKeyIsArray() | ||
{ | ||
$builder = $this->getBuilder(); | ||
$grammar = $builder->getGrammar(); | ||
$value = [ | ||
'configuration' => [ | ||
'dark_mode' => false, | ||
'language' => 'en', | ||
], | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
]; | ||
|
||
$sql = $grammar->compileInsert($builder, $value); | ||
|
||
$this->assertSame('insert into "users" ("configuration", "name", "email") values (?, ?, ?)', $sql); | ||
} | ||
|
||
public function testCompileInsertSingleValueWhereFirstKeyIsNotArray() | ||
{ | ||
$builder = $this->getBuilder(); | ||
$grammar = $builder->getGrammar(); | ||
|
||
$value = [ | ||
'name' => 'John Doe', | ||
'configuration' => [ | ||
'dark_mode' => false, | ||
'language' => 'en', | ||
], | ||
'email' => '[email protected]', | ||
]; | ||
|
||
$sql = $grammar->compileInsert($builder, $value); | ||
|
||
$this->assertSame('insert into "users" ("name", "configuration", "email") values (?, ?, ?)', $sql); | ||
} | ||
|
||
protected function getConnection() | ||
{ | ||
return m::mock(ConnectionInterface::class); | ||
} | ||
|
||
protected function getBuilder($tableName = 'users') | ||
{ | ||
$grammar = new Grammar; | ||
$processor = m::mock(Processor::class); | ||
|
||
$builder = new Builder($this->getConnection(), $grammar, $processor); | ||
$builder->from = $tableName; | ||
|
||
return $builder; | ||
} | ||
} |