-
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.
[9.x]
InteractsWithDatabase::castAsJson($value)
incorrectly handles…
… SQLite Database (#44196) * Implement compileJsonCast method in database query grammar * Use compileJsonCast to retrieve queryable json value * Add castAsJson tests * CS fixes * Fix tests * Use single quotes with concatenation for consistency * formatting Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
f40fe7d
commit 98a9954
Showing
5 changed files
with
181 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Testing\Concerns; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Illuminate\Database\Query\Expression; | ||
use Illuminate\Database\Query\Grammars\MySqlGrammar; | ||
use Illuminate\Database\Query\Grammars\PostgresGrammar; | ||
use Illuminate\Database\Query\Grammars\SQLiteGrammar; | ||
use Illuminate\Database\Query\Grammars\SqlServerGrammar; | ||
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Facade; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InteractsWithDatabaseTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
Facade::clearResolvedInstances(); | ||
Facade::setFacadeApplication(null); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testCastToJsonSqlite() | ||
{ | ||
$grammar = new SQLiteGrammar(); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
'["foo","bar"]' | ||
TEXT, | ||
$this->castAsJson(['foo', 'bar'], $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
'["foo","bar"]' | ||
TEXT, | ||
$this->castAsJson(collect(['foo', 'bar']), $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
'{"foo":"bar"}' | ||
TEXT, | ||
$this->castAsJson((object) ['foo' => 'bar'], $grammar) | ||
); | ||
} | ||
|
||
public function testCastToJsonPostgres() | ||
{ | ||
$grammar = new PostgresGrammar(); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
'["foo","bar"]' | ||
TEXT, | ||
$this->castAsJson(['foo', 'bar'], $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
'["foo","bar"]' | ||
TEXT, | ||
$this->castAsJson(collect(['foo', 'bar']), $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
'{"foo":"bar"}' | ||
TEXT, | ||
$this->castAsJson((object) ['foo' => 'bar'], $grammar) | ||
); | ||
} | ||
|
||
public function testCastToJsonSqlServer() | ||
{ | ||
$grammar = new SqlServerGrammar(); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
json_query('["foo","bar"]') | ||
TEXT, | ||
$this->castAsJson(['foo', 'bar'], $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
json_query('["foo","bar"]') | ||
TEXT, | ||
$this->castAsJson(collect(['foo', 'bar']), $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
json_query('{"foo":"bar"}') | ||
TEXT, | ||
$this->castAsJson((object) ['foo' => 'bar'], $grammar) | ||
); | ||
} | ||
|
||
public function testCastToJsonMySql() | ||
{ | ||
$grammar = new MySqlGrammar(); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
cast('["foo","bar"]' as json) | ||
TEXT, | ||
$this->castAsJson(['foo', 'bar'], $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
cast('["foo","bar"]' as json) | ||
TEXT, | ||
$this->castAsJson(collect(['foo', 'bar']), $grammar) | ||
); | ||
|
||
$this->assertEquals(<<<'TEXT' | ||
cast('{"foo":"bar"}' as json) | ||
TEXT, | ||
$this->castAsJson((object) ['foo' => 'bar'], $grammar) | ||
); | ||
} | ||
|
||
protected function castAsJson($value, $grammar) | ||
{ | ||
$connection = m::mock(ConnectionInterface::class); | ||
|
||
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar); | ||
|
||
$connection->shouldReceive('getPdo->quote')->andReturnUsing(function ($value) { | ||
return "'".$value."'"; | ||
}); | ||
|
||
DB::shouldReceive('connection')->andReturn($connection); | ||
|
||
DB::shouldReceive('raw')->andReturnUsing(function ($value) { | ||
return new Expression($value); | ||
}); | ||
|
||
$instance = new class | ||
{ | ||
use InteractsWithDatabase; | ||
}; | ||
|
||
return $instance->castAsJson($value)->getValue(); | ||
} | ||
} |