Repository navigation
Expand file tree
/
Copy pathGetForeignKeyDataTest.php
More file actions
156 lines (123 loc) · 6.5 KB
/
Copy pathGetForeignKeyDataTest.php
File metadata and controls
156 lines (123 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Database\Live\SQLite3;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('DatabaseLive')]
final class GetForeignKeyDataTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->db = Database::connect($this->DBGroup);
if ($this->db->DBDriver !== 'SQLite3') {
$this->markTestSkipped('This test is only for SQLite3.');
}
$config = [
'DBDriver' => 'SQLite3',
'database' => 'database.db',
'DBDebug' => true,
];
$this->db = db_connect($config, false);
}
public function testGetForeignKeyDataWithSpecialTableName(): void
{
$this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))');
$foreignKeys = $this->db->getForeignKeyData('test-child');
$this->db->query('DROP TABLE IF EXISTS `test-child`');
$this->db->query('DROP TABLE IF EXISTS `test-parent`');
$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('test-child', $firstKey->table_name);
$this->assertSame(['parent_id'], $firstKey->column_name);
$this->assertSame('test-parent', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
public function testGetForeignKeyDataWithAttachedDatabase(): void
{
$this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`');
$this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))');
$foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child');
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`');
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`');
$this->db->query('DETACH DATABASE `test_schema`');
$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('test_schema.attached-child', $firstKey->table_name);
$this->assertSame(['p_id'], $firstKey->column_name);
$this->assertSame('attached-parent', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
public function testGetForeignKeyDataWithParenthesesInTableName(): void
{
$this->db->query('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id))');
$foreignKeys = $this->db->getForeignKeyData('child(v2)');
$this->db->query('DROP TABLE IF EXISTS "child(v2)"');
$this->db->query('DROP TABLE IF EXISTS "parent(v1)"');
$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('child(v2)', $firstKey->table_name);
$this->assertSame(['parent_id'], $firstKey->column_name);
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
public function testGetForeignKeyDataWithQuotesAndSpacesInTableName(): void
{
$this->db->query('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "child\'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id))');
$foreignKeys = $this->db->getForeignKeyData("child'quote with space");
$this->db->query('DROP TABLE IF EXISTS "child\'quote with space"');
$this->db->query('DROP TABLE IF EXISTS "parent table"');
$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame("child'quote with space", $firstKey->table_name);
$this->assertSame(['p_id'], $firstKey->column_name);
$this->assertSame('parent table', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
public function testGetForeignKeyDataWithNumericAndReservedWordTableName(): void
{
$this->db->query('CREATE TABLE "order" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id))');
$foreignKeys = $this->db->getForeignKeyData('12345');
$this->db->query('DROP TABLE IF EXISTS "12345"');
$this->db->query('DROP TABLE IF EXISTS "order"');
$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('12345', $firstKey->table_name);
$this->assertSame(['order_id'], $firstKey->column_name);
$this->assertSame('order', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers(): void
{
$this->db->query('ATTACH DATABASE ":memory:" AS "schema-db(1)"');
$this->db->query('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY)');
$this->db->query('CREATE TABLE "schema-db(1)"."child(v2)" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent(v1)"(id))');
$foreignKeys = $this->db->getForeignKeyData('schema-db(1).child(v2)');
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"');
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"');
$this->db->query('DETACH DATABASE "schema-db(1)"');
$this->assertCount(1, $foreignKeys);
$firstKey = reset($foreignKeys);
$this->assertSame('schema-db(1).child(v2)', $firstKey->table_name);
$this->assertSame(['p_id'], $firstKey->column_name);
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
$this->assertSame(['id'], $firstKey->foreign_column_name);
}
}