Skip to content

Commit e029c30

Browse files
committed
test: extract AbstractGetFieldDataTest class
1 parent 7d6a0d0 commit e029c30

File tree

3 files changed

+99
-111
lines changed

3 files changed

+99
-111
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\Live;
13+
14+
use CodeIgniter\Database\BaseConnection;
15+
use CodeIgniter\Database\Forge;
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use Config\Database;
18+
19+
abstract class AbstractGetFieldDataTest extends CIUnitTestCase
20+
{
21+
/**
22+
* @var BaseConnection
23+
*/
24+
protected $db;
25+
26+
protected Forge $forge;
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->db = Database::connect($this->DBGroup);
33+
34+
$this->createForge();
35+
$this->createTable();
36+
}
37+
38+
/**
39+
* Make sure that $db and $forge are instantiated.
40+
*/
41+
abstract protected function createForge(): void;
42+
43+
protected function tearDown(): void
44+
{
45+
parent::tearDown();
46+
47+
$this->forge->dropTable('test1', true);
48+
}
49+
50+
protected function createTable()
51+
{
52+
$this->forge->dropTable('test1', true);
53+
54+
$this->forge->addField([
55+
'id' => [
56+
'type' => 'INT',
57+
'auto_increment' => true,
58+
],
59+
'text_not_null' => [
60+
'type' => 'VARCHAR',
61+
'constraint' => 64,
62+
],
63+
'text_null' => [
64+
'type' => 'VARCHAR',
65+
'constraint' => 64,
66+
'null' => true,
67+
],
68+
'int_default_0' => [
69+
'type' => 'INT',
70+
'default' => 0,
71+
],
72+
'text_default_null' => [
73+
'type' => 'VARCHAR',
74+
'constraint' => 64,
75+
'default' => null,
76+
],
77+
'text_default_text_null' => [
78+
'type' => 'VARCHAR',
79+
'constraint' => 64,
80+
'default' => 'null',
81+
],
82+
'text_default_abc' => [
83+
'type' => 'VARCHAR',
84+
'constraint' => 64,
85+
'default' => 'abc',
86+
],
87+
]);
88+
$this->forge->addKey('id', true);
89+
$this->forge->createTable('test1');
90+
}
91+
92+
abstract public function testGetFieldData(): void;
93+
}

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

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

1212
namespace CodeIgniter\Database\Live\MySQLi;
1313

14-
use CodeIgniter\Database\MySQLi\Connection;
15-
use CodeIgniter\Database\MySQLi\Forge;
16-
use CodeIgniter\Test\CIUnitTestCase;
17-
use CodeIgniter\Test\DatabaseTestTrait;
14+
use CodeIgniter\Database\Live\AbstractGetFieldDataTest;
1815
use Config\Database;
1916

2017
/**
2118
* @group DatabaseLive
2219
*
2320
* @internal
2421
*/
25-
final class GetFieldDataTest extends CIUnitTestCase
22+
final class GetFieldDataTest extends AbstractGetFieldDataTest
2623
{
27-
use DatabaseTestTrait;
28-
29-
protected $migrate = false;
30-
31-
/**
32-
* @var Connection
33-
*/
34-
protected $db;
35-
36-
private Forge $forge;
37-
38-
protected function setUp(): void
24+
protected function createForge(): void
3925
{
40-
parent::setUp();
41-
4226
if ($this->db->DBDriver !== 'MySQLi') {
4327
$this->markTestSkipped('This test is only for MySQLi.');
4428
}
@@ -48,45 +32,6 @@ protected function setUp(): void
4832

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

9237
$this->assertJsonStringEqualsJsonString(
@@ -150,7 +95,5 @@ public function testGetFieldData(): void
15095
]),
15196
json_encode($fields)
15297
);
153-
154-
$this->forge->dropTable('test1', true);
15598
}
15699
}

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)