|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Database\Postgres; |
| 4 | + |
| 5 | +use Illuminate\Database\Schema\Blueprint; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Support\Facades\Schema; |
| 8 | + |
| 9 | +/** |
| 10 | + * @requires extension pdo_pgsql |
| 11 | + * @requires OS Linux|Darwin |
| 12 | + */ |
| 13 | +class DatabasePostgresConnectionTest extends PostgresTestCase |
| 14 | +{ |
| 15 | + protected function defineDatabaseMigrationsAfterDatabaseRefreshed() |
| 16 | + { |
| 17 | + if (! Schema::hasTable('json_table')) { |
| 18 | + Schema::create('json_table', function (Blueprint $table) { |
| 19 | + $table->json('json_col')->nullable(); |
| 20 | + }); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + protected function destroyDatabaseMigrations() |
| 25 | + { |
| 26 | + Schema::drop('json_table'); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider jsonWhereNullDataProvider |
| 31 | + */ |
| 32 | + public function testJsonWhereNull($expected, $key, array $value = ['value' => 123]) |
| 33 | + { |
| 34 | + DB::table('json_table')->insert(['json_col' => json_encode($value)]); |
| 35 | + |
| 36 | + $this->assertSame($expected, DB::table('json_table')->whereNull("json_col->$key")->exists()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider jsonWhereNullDataProvider |
| 41 | + */ |
| 42 | + public function testJsonWhereNotNull($expected, $key, array $value = ['value' => 123]) |
| 43 | + { |
| 44 | + DB::table('json_table')->insert(['json_col' => json_encode($value)]); |
| 45 | + |
| 46 | + $this->assertSame(! $expected, DB::table('json_table')->whereNotNull("json_col->$key")->exists()); |
| 47 | + } |
| 48 | + |
| 49 | + public function jsonWhereNullDataProvider() |
| 50 | + { |
| 51 | + return [ |
| 52 | + 'key not exists' => [true, 'invalid'], |
| 53 | + 'key exists and null' => [true, 'value', ['value' => null]], |
| 54 | + 'key exists and "null"' => [false, 'value', ['value' => 'null']], |
| 55 | + 'key exists and not null' => [false, 'value', ['value' => false]], |
| 56 | + 'nested key not exists' => [true, 'nested->invalid'], |
| 57 | + 'nested key exists and null' => [true, 'nested->value', ['nested' => ['value' => null]]], |
| 58 | + 'nested key exists and "null"' => [false, 'nested->value', ['nested' => ['value' => 'null']]], |
| 59 | + 'nested key exists and not null' => [false, 'nested->value', ['nested' => ['value' => false]]], |
| 60 | + 'array index not exists' => [true, '[0]', [1 => 'invalid']], |
| 61 | + 'array index exists and null' => [true, '[0]', [null]], |
| 62 | + 'array index exists and "null"' => [false, '[0]', ['null']], |
| 63 | + 'array index exists and not null' => [false, '[0]', [false]], |
| 64 | + 'multiple array index not exists' => [true, '[0][0]', [1 => [1 => 'invalid']]], |
| 65 | + 'multiple array index exists and null' => [true, '[0][0]', [[null]]], |
| 66 | + 'multiple array index exists and "null"' => [false, '[0][0]', [['null']]], |
| 67 | + 'multiple array index exists and not null' => [false, '[0][0]', [[false]]], |
| 68 | + 'nested array index not exists' => [true, 'nested[0]', ['nested' => [1 => 'nested->invalid']]], |
| 69 | + 'nested array index exists and null' => [true, 'nested->value[1]', ['nested' => ['value' => [0, null]]]], |
| 70 | + 'nested array index exists and "null"' => [false, 'nested->value[1]', ['nested' => ['value' => [0, 'null']]]], |
| 71 | + 'nested array index exists and not null' => [false, 'nested->value[1]', ['nested' => ['value' => [0, false]]]], |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + public function testJsonPathUpdate() |
| 76 | + { |
| 77 | + DB::table('json_table')->insert([ |
| 78 | + ['json_col' => '{"foo":["bar"]}'], |
| 79 | + ['json_col' => '{"foo":["baz"]}'], |
| 80 | + ['json_col' => '{"foo":[["array"]]}'], |
| 81 | + ]); |
| 82 | + |
| 83 | + $updatedCount = DB::table('json_table')->where('json_col->foo[0]', 'baz')->update([ |
| 84 | + 'json_col->foo[0]' => 'updated', |
| 85 | + ]); |
| 86 | + $this->assertSame(1, $updatedCount); |
| 87 | + |
| 88 | + $updatedCount = DB::table('json_table')->where('json_col->foo[0][0]', 'array')->update([ |
| 89 | + 'json_col->foo[0][0]' => 'updated', |
| 90 | + ]); |
| 91 | + $this->assertSame(1, $updatedCount); |
| 92 | + } |
| 93 | +} |
0 commit comments