diff --git a/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php b/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php index d802fa096f..c37d7a5502 100644 --- a/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php +++ b/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php @@ -259,54 +259,41 @@ public function testJsonSchema(): void { $this->connection->insert('test_json') ->fields([ - 'test_field' => '{"key": "value", "number": 0, "bool": true, "list": ["a","b","c"], "nested": {"key": "value"}}', + 'test_field' => '{"key": "value1", "number": 0, "bool": true, "list": ["a","b","c"], "nested": {"key": "value2"}}', ]) ->execute(); - // The '->' operator returns strings wrapped into quotes. - $query = $this->connection->select('test_json'); - $query->addExpression('test_field -> :key', NULL, [ - ':key' => 'key', - ]); - $actual = $query->execute()->fetchField(); - $this->assertSame('"value"', $actual); - // Testing nested strings using '->>' operator. - $query = $this->connection->select('test_json'); - $query->addExpression('test_field -> :nested ->> :key', NULL, [ - ':nested' => 'nested', - ':key' => 'key', - ]); - $actual = $query->execute()->fetchField(); - $this->assertSame('value', $actual); - - // Testing boolean. - $query = $this->connection->select('test_json'); - $query->addExpression('test_field ->> :bool', NULL, [ - ':bool' => 'bool', - ]); - $actual = $query->execute()->fetchField(); - $this->assertSame('true', $actual); - // Testing numbers. - $query = $this->connection->select('test_json'); - $query->addExpression('test_field ->> :number', NULL, [ - ':number' => 'number', - ]); - $actual = $query->execute()->fetchField(); - $this->assertSame('0', $actual); - - // Testing array. - $query = $this->connection->select('test_json'); - $query->addExpression('test_field ->> :list', NULL, [ - ':list' => 'list', - ]); - $actual = $query->execute()->fetchField(); - $this->assertSame('["a","b","c"]', $actual); + $test_data = [ + ['key', 'value1'], + [['nested', 'key'], 'value2'], + ['bool', 'true'], + ['number', '0'], + ['list', '["a","b","c"]'], + ]; + foreach ($test_data as $data) { + $path = $data[0]; + $expected = $data[1]; + $query = $this->connection->select('test_json'); + if (is_array($path)) { + $query->addExpression('[test_field] -> :path1 ->> :path2', NULL, [ + ':path1' => $path[0], + ':path2' => $path[1], + ]); + } + else { + $query->addExpression('[test_field] ->> :path', NULL, [ + ':path' => $path, + ]); + } + $actual = $query->execute()->fetchField(); + $this->assertSame($expected, $actual, (string)$query); + } // Testing array value. $query = $this->connection->select('test_json'); - $query->addExpression('test_field -> :list ->> 1', NULL, [ + $query->addExpression('test_field -> :list -> 1', NULL, [ ':list' => 'list', ]); $actual = $query->execute()->fetchField(); - $this->assertSame('b', $actual); + $this->assertSame('"b"', $actual); } }