Skip to content

Commit

Permalink
test: add assertSameFieldData()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jan 30, 2024
1 parent 23909fd commit f3fd479
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 283 deletions.
13 changes: 13 additions & 0 deletions tests/system/Database/Live/AbstractGetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,17 @@ protected function createTableForDefault()
}

abstract public function testGetFieldDataDefault(): void;

protected function assertSameFieldData(array $expected, array $actual)
{
$expected = json_decode(json_encode($expected), true);
$names = array_column($expected, 'name');
array_multisort($names, SORT_ASC, $expected);

$fields = json_decode(json_encode($actual), true);
$names = array_column($fields, 'name');
array_multisort($names, SORT_ASC, $fields);

$this->assertSame($expected, $fields);
}
}
120 changes: 59 additions & 61 deletions tests/system/Database/Live/MySQLi/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,66 +52,64 @@ public function testGetFieldDataDefault(): void

$fields = $this->db->getFieldData($this->table);

$this->assertJsonStringEqualsJsonString(
json_encode([
(object) [
'name' => 'id',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 11 : null,
'default' => null, // The default value is not defined.
'primary_key' => 1,
'nullable' => false,
],
(object) [
'name' => 'text_not_null',
'type' => 'varchar',
'max_length' => 64,
'default' => null, // The default value is not defined.
'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_null',
'type' => 'varchar',
'max_length' => 64,
'default' => null, // The default value is not defined.
'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'int_default_0',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 11 : null,
'default' => '0', // int 0
'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_null',
'type' => 'varchar',
'max_length' => 64,
'default' => null, // NULL value
'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'text_default_text_null',
'type' => 'varchar',
'max_length' => 64,
'default' => 'null', // string "null"
'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_abc',
'type' => 'varchar',
'max_length' => 64,
'default' => 'abc', // string "abc"
'primary_key' => 0,
'nullable' => false,
],
]),
json_encode($fields)
);
$expected = [
(object) [
'name' => 'id',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 11 : null,
'nullable' => false,
'default' => null, // The default value is not defined.
'primary_key' => 1,
],
(object) [
'name' => 'text_not_null',
'type' => 'varchar',
'max_length' => 64,
'nullable' => false,
'default' => null, // The default value is not defined.
'primary_key' => 0,
],
(object) [
'name' => 'text_null',
'type' => 'varchar',
'max_length' => 64,
'nullable' => true,
'default' => null, // The default value is not defined.
'primary_key' => 0,
],
(object) [
'name' => 'int_default_0',
'type' => 'int',
'max_length' => $this->isOldMySQL() ? 11 : null,
'nullable' => false,
'default' => '0', // int 0
'primary_key' => 0,
],
(object) [
'name' => 'text_default_null',
'type' => 'varchar',
'max_length' => 64,
'nullable' => true,
'default' => null, // NULL value
'primary_key' => 0,
],
(object) [
'name' => 'text_default_text_null',
'type' => 'varchar',
'max_length' => 64,
'nullable' => false,
'default' => 'null', // string "null"
'primary_key' => 0,
],
(object) [
'name' => 'text_default_abc',
'type' => 'varchar',
'max_length' => 64,
'nullable' => false,
'default' => 'abc', // string "abc"
'primary_key' => 0,
],
];
$this->assertSameFieldData($expected, $fields);
}
}
13 changes: 3 additions & 10 deletions tests/system/Database/Live/OCI8/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testGetFieldDataDefault(): void
$idDefault = $data['id']->default;
$this->assertMatchesRegularExpression('/"ORACLE"."ISEQ\$\$_[0-9]+".nextval/', $idDefault);

$expected = json_decode(json_encode([
$expected = [
(object) [
'name' => 'id',
'type' => 'NUMBER',
Expand Down Expand Up @@ -104,14 +104,7 @@ public function testGetFieldDataDefault(): void
'default' => "'abc' ", // string "abc"
// 'primary_key' => 0,
],
]), true);
$names = array_column($expected, 'name');
array_multisort($names, SORT_ASC, $expected);

$fields = json_decode(json_encode($fields), true);
$names = array_column($fields, 'name');
array_multisort($names, SORT_ASC, $fields);

$this->assertSame($expected, $fields);
];
$this->assertSameFieldData($expected, $fields);
}
}
120 changes: 59 additions & 61 deletions tests/system/Database/Live/Postgre/GetFieldDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,66 +38,64 @@ public function testGetFieldDataDefault(): void

$fields = $this->db->getFieldData('test1');

$this->assertJsonStringEqualsJsonString(
json_encode([
(object) [
'name' => 'id',
'type' => 'integer',
'max_length' => '32',
'default' => "nextval('db_test1_id_seq'::regclass)", // The default value is not defined.
// 'primary_key' => 1,
'nullable' => false,
],
(object) [
'name' => 'text_not_null',
'type' => 'character varying',
'max_length' => '64',
'default' => null, // The default value is not defined.
// 'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_null',
'type' => 'character varying',
'max_length' => '64',
'default' => null, // The default value is not defined.
// 'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'int_default_0',
'type' => 'integer',
'max_length' => '32',
'default' => '0', // int 0
// 'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_null',
'type' => 'character varying',
'max_length' => '64',
'default' => 'NULL::character varying', // NULL value
// 'primary_key' => 0,
'nullable' => true,
],
(object) [
'name' => 'text_default_text_null',
'type' => 'character varying',
'max_length' => '64',
'default' => "'null'::character varying", // string "null"
// 'primary_key' => 0,
'nullable' => false,
],
(object) [
'name' => 'text_default_abc',
'type' => 'character varying',
'max_length' => '64',
'default' => "'abc'::character varying", // string "abc"
// 'primary_key' => 0,
'nullable' => false,
],
]),
json_encode($fields)
);
$expected = [
(object) [
'name' => 'id',
'type' => 'integer',
'max_length' => '32',
'nullable' => false,
// 'primary_key' => 1,
'default' => "nextval('db_test1_id_seq'::regclass)", // The default value is not defined.
],
(object) [
'name' => 'text_not_null',
'type' => 'character varying',
'max_length' => '64',
'nullable' => false,
// 'primary_key' => 0,
'default' => null, // The default value is not defined.
],
(object) [
'name' => 'text_null',
'type' => 'character varying',
'max_length' => '64',
'nullable' => true,
// 'primary_key' => 0,
'default' => null, // The default value is not defined.
],
(object) [
'name' => 'int_default_0',
'type' => 'integer',
'max_length' => '32',
'nullable' => false,
// 'primary_key' => 0,
'default' => '0', // int 0
],
(object) [
'name' => 'text_default_null',
'type' => 'character varying',
'max_length' => '64',
'nullable' => true,
// 'primary_key' => 0,
'default' => 'NULL::character varying', // NULL value
],
(object) [
'name' => 'text_default_text_null',
'type' => 'character varying',
'max_length' => '64',
'nullable' => false,
// 'primary_key' => 0,
'default' => "'null'::character varying", // string "null"
],
(object) [
'name' => 'text_default_abc',
'type' => 'character varying',
'max_length' => '64',
'nullable' => false,
// 'primary_key' => 0,
'default' => "'abc'::character varying", // string "abc"
],
];
$this->assertSameFieldData($expected, $fields);
}
}
Loading

0 comments on commit f3fd479

Please sign in to comment.