1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
<?php
namespace Illuminate\Tests\Integration\Database\Sqlite;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
class DatabaseSqliteConnectionTest extends DatabaseTestCase
{
protected function getEnvironmentSetUp($app)
{
if (getenv('DB_CONNECTION') !== 'testing') {
$this->markTestSkipped('Test requires a Sqlite connection.');
}
$app['config']->set('database.default', 'conn1');
$app['config']->set('database.connections.conn1', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
protected function afterRefreshingDatabase()
{
if (! Schema::hasTable('json_table')) {
Schema::create('json_table', function (Blueprint $table) {
$table->json('json_col')->nullable();
});
}
}
protected function destroyDatabaseMigrations()
{
Schema::drop('json_table');
}
/**
* @dataProvider jsonContainsKeyDataProvider
*/
public function testWhereJsonContainsKey($count, $column)
{
DB::table('json_table')->insert([
['json_col' => '{"foo":{"bar":["baz"]}}'],
['json_col' => '{"foo":{"bar":false}}'],
['json_col' => '{"foo":{}}'],
['json_col' => '{"foo":[{"bar":"bar"},{"baz":"baz"}]}'],
['json_col' => '{"bar":null}'],
]);
$this->assertSame($count, DB::table('json_table')->whereJsonContainsKey($column)->count());
}
public static function jsonContainsKeyDataProvider()
{
return [
'string key' => [4, 'json_col->foo'],
'nested key exists' => [2, 'json_col->foo->bar'],
'string key missing' => [0, 'json_col->none'],
'integer key with arrow ' => [0, 'json_col->foo->bar->0'],
'integer key with braces' => [1, 'json_col->foo->bar[0]'],
'integer key missing' => [0, 'json_col->foo->bar[1]'],
'mixed keys' => [1, 'json_col->foo[1]->baz'],
'null value' => [1, 'json_col->bar'],
];
}
}
|