Skip to content

Commit 5bbf3af

Browse files
authored
Merge pull request #8433 from kenjis/test-mysqli-getFieldData
test: add test for MySQLi getFieldData()
2 parents 314e293 + 4411e44 commit 5bbf3af

File tree

3 files changed

+213
-51
lines changed

3 files changed

+213
-51
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live;
15+
16+
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\Forge;
18+
use CodeIgniter\Test\CIUnitTestCase;
19+
use Config\Database;
20+
21+
abstract class AbstractGetFieldDataTest extends CIUnitTestCase
22+
{
23+
/**
24+
* @var BaseConnection
25+
*/
26+
protected $db;
27+
28+
protected Forge $forge;
29+
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
$this->db = Database::connect($this->DBGroup);
35+
36+
$this->createForge();
37+
$this->createTable();
38+
}
39+
40+
/**
41+
* Make sure that $db and $forge are instantiated.
42+
*/
43+
abstract protected function createForge(): void;
44+
45+
protected function tearDown(): void
46+
{
47+
parent::tearDown();
48+
49+
$this->forge->dropTable('test1', true);
50+
}
51+
52+
protected function createTable()
53+
{
54+
$this->forge->dropTable('test1', true);
55+
56+
$this->forge->addField([
57+
'id' => [
58+
'type' => 'INT',
59+
'auto_increment' => true,
60+
],
61+
'text_not_null' => [
62+
'type' => 'VARCHAR',
63+
'constraint' => 64,
64+
],
65+
'text_null' => [
66+
'type' => 'VARCHAR',
67+
'constraint' => 64,
68+
'null' => true,
69+
],
70+
'int_default_0' => [
71+
'type' => 'INT',
72+
'default' => 0,
73+
],
74+
'text_default_null' => [
75+
'type' => 'VARCHAR',
76+
'constraint' => 64,
77+
'default' => null,
78+
],
79+
'text_default_text_null' => [
80+
'type' => 'VARCHAR',
81+
'constraint' => 64,
82+
'default' => 'null',
83+
],
84+
'text_default_abc' => [
85+
'type' => 'VARCHAR',
86+
'constraint' => 64,
87+
'default' => 'abc',
88+
],
89+
]);
90+
$this->forge->addKey('id', true);
91+
$this->forge->createTable('test1');
92+
}
93+
94+
abstract public function testGetFieldData(): void;
95+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Database\Live\MySQLi;
15+
16+
use CodeIgniter\Database\Live\AbstractGetFieldDataTest;
17+
use Config\Database;
18+
19+
/**
20+
* @group DatabaseLive
21+
*
22+
* @internal
23+
*/
24+
final class GetFieldDataTest extends AbstractGetFieldDataTest
25+
{
26+
protected function createForge(): void
27+
{
28+
if ($this->db->DBDriver !== 'MySQLi') {
29+
$this->markTestSkipped('This test is only for MySQLi.');
30+
}
31+
32+
$this->forge = Database::forge($this->db);
33+
}
34+
35+
/**
36+
* As of MySQL 8.0.17, the display width attribute for integer data types
37+
* is deprecated and is not reported back anymore.
38+
*
39+
* @see https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html
40+
*/
41+
private function isOldMySQL(): bool
42+
{
43+
return ! (
44+
version_compare($this->db->getVersion(), '8.0.17', '>=')
45+
&& strpos($this->db->getVersion(), 'MariaDB') === false
46+
);
47+
}
48+
49+
public function testGetFieldData(): void
50+
{
51+
$fields = $this->db->getFieldData('test1');
52+
53+
$this->assertJsonStringEqualsJsonString(
54+
json_encode([
55+
(object) [
56+
'name' => 'id',
57+
'type' => 'int',
58+
'max_length' => $this->isOldMySQL() ? 11 : null,
59+
'default' => null, // The default value is not defined.
60+
'primary_key' => 1,
61+
'nullable' => false,
62+
],
63+
(object) [
64+
'name' => 'text_not_null',
65+
'type' => 'varchar',
66+
'max_length' => 64,
67+
'default' => null, // The default value is not defined.
68+
'primary_key' => 0,
69+
'nullable' => false,
70+
],
71+
(object) [
72+
'name' => 'text_null',
73+
'type' => 'varchar',
74+
'max_length' => 64,
75+
'default' => null, // The default value is not defined.
76+
'primary_key' => 0,
77+
'nullable' => true,
78+
],
79+
(object) [
80+
'name' => 'int_default_0',
81+
'type' => 'int',
82+
'max_length' => $this->isOldMySQL() ? 11 : null,
83+
'default' => '0', // int 0
84+
'primary_key' => 0,
85+
'nullable' => false,
86+
],
87+
(object) [
88+
'name' => 'text_default_null',
89+
'type' => 'varchar',
90+
'max_length' => 64,
91+
'default' => null, // NULL value
92+
'primary_key' => 0,
93+
'nullable' => true,
94+
],
95+
(object) [
96+
'name' => 'text_default_text_null',
97+
'type' => 'varchar',
98+
'max_length' => 64,
99+
'default' => 'null', // string "null"
100+
'primary_key' => 0,
101+
'nullable' => false,
102+
],
103+
(object) [
104+
'name' => 'text_default_abc',
105+
'type' => 'varchar',
106+
'max_length' => 64,
107+
'default' => 'abc', // string "abc"
108+
'primary_key' => 0,
109+
'nullable' => false,
110+
],
111+
]),
112+
json_encode($fields)
113+
);
114+
}
115+
}

tests/system/Database/Live/SQLite3/GetFieldDataTest.php

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,18 @@
1111

1212
namespace CodeIgniter\Database\Live\SQLite3;
1313

14-
use CodeIgniter\Database\SQLite3\Connection;
15-
use CodeIgniter\Database\SQLite3\Forge;
16-
use CodeIgniter\Test\CIUnitTestCase;
14+
use CodeIgniter\Database\Live\AbstractGetFieldDataTest;
1715
use Config\Database;
1816

1917
/**
2018
* @group DatabaseLive
2119
*
2220
* @internal
2321
*/
24-
final class GetFieldDataTest extends CIUnitTestCase
22+
final class GetFieldDataTest extends AbstractGetFieldDataTest
2523
{
26-
/**
27-
* @var Connection
28-
*/
29-
protected $db;
30-
31-
private Forge $forge;
32-
33-
protected function setUp(): void
24+
protected function createForge(): void
3425
{
35-
parent::setUp();
36-
37-
$this->db = Database::connect($this->DBGroup);
3826
if ($this->db->DBDriver !== 'SQLite3') {
3927
$this->markTestSkipped('This test is only for SQLite3.');
4028
}
@@ -50,40 +38,6 @@ protected function setUp(): void
5038

5139
public function testGetFieldData(): void
5240
{
53-
$this->forge->dropTable('test1', true);
54-
55-
$this->forge->addField([
56-
'id' => [
57-
'type' => 'INT',
58-
'auto_increment' => true,
59-
],
60-
'text_not_null' => [
61-
'type' => 'VARCHAR',
62-
],
63-
'text_null' => [
64-
'type' => 'VARCHAR',
65-
'null' => true,
66-
],
67-
'int_default_0' => [
68-
'type' => 'INT',
69-
'default' => 0,
70-
],
71-
'text_default_null' => [
72-
'type' => 'VARCHAR',
73-
'default' => null,
74-
],
75-
'text_default_text_null' => [
76-
'type' => 'VARCHAR',
77-
'default' => 'null',
78-
],
79-
'text_default_abc' => [
80-
'type' => 'VARCHAR',
81-
'default' => 'abc',
82-
],
83-
]);
84-
$this->forge->addKey('id', true);
85-
$this->forge->createTable('test1');
86-
8741
$fields = $this->db->getFieldData('test1');
8842

8943
$this->assertJsonStringEqualsJsonString(
@@ -147,7 +101,5 @@ public function testGetFieldData(): void
147101
]),
148102
json_encode($fields)
149103
);
150-
151-
$this->forge->dropTable('test1', true);
152104
}
153105
}

0 commit comments

Comments
 (0)