|
| 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\SQLite3; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use Config\Database; |
| 18 | +use PHPUnit\Framework\Attributes\Group; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Group('DatabaseLive')] |
| 24 | +final class GetForeignKeyDataTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + parent::setUp(); |
| 29 | + |
| 30 | + $this->db = Database::connect($this->DBGroup); |
| 31 | + if ($this->db->DBDriver !== 'SQLite3') { |
| 32 | + $this->markTestSkipped('This test is only for SQLite3.'); |
| 33 | + } |
| 34 | + |
| 35 | + $config = [ |
| 36 | + 'DBDriver' => 'SQLite3', |
| 37 | + 'database' => 'database.db', |
| 38 | + 'DBDebug' => true, |
| 39 | + ]; |
| 40 | + $this->db = db_connect($config, false); |
| 41 | + } |
| 42 | + |
| 43 | + public function testGetForeignKeyDataWithSpecialTableName(): void |
| 44 | + { |
| 45 | + $this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)'); |
| 46 | + $this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))'); |
| 47 | + |
| 48 | + $foreignKeys = $this->db->getForeignKeyData('test-child'); |
| 49 | + |
| 50 | + $this->db->query('DROP TABLE IF EXISTS `test-child`'); |
| 51 | + $this->db->query('DROP TABLE IF EXISTS `test-parent`'); |
| 52 | + |
| 53 | + $this->assertCount(1, $foreignKeys); |
| 54 | + $firstKey = reset($foreignKeys); |
| 55 | + $this->assertSame('test-child', $firstKey->table_name); |
| 56 | + $this->assertSame(['parent_id'], $firstKey->column_name); |
| 57 | + $this->assertSame('test-parent', $firstKey->foreign_table_name); |
| 58 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGetForeignKeyDataWithAttachedDatabase(): void |
| 62 | + { |
| 63 | + $this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`'); |
| 64 | + |
| 65 | + $this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)'); |
| 66 | + $this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))'); |
| 67 | + |
| 68 | + $foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child'); |
| 69 | + |
| 70 | + $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`'); |
| 71 | + $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`'); |
| 72 | + $this->db->query('DETACH DATABASE `test_schema`'); |
| 73 | + |
| 74 | + $this->assertCount(1, $foreignKeys); |
| 75 | + $firstKey = reset($foreignKeys); |
| 76 | + $this->assertSame('test_schema.attached-child', $firstKey->table_name); |
| 77 | + $this->assertSame(['p_id'], $firstKey->column_name); |
| 78 | + $this->assertSame('attached-parent', $firstKey->foreign_table_name); |
| 79 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 80 | + } |
| 81 | + |
| 82 | + public function testGetForeignKeyDataWithParenthesesInTableName(): void |
| 83 | + { |
| 84 | + $this->db->query('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY)'); |
| 85 | + $this->db->query('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id))'); |
| 86 | + |
| 87 | + $foreignKeys = $this->db->getForeignKeyData('child(v2)'); |
| 88 | + |
| 89 | + $this->db->query('DROP TABLE IF EXISTS "child(v2)"'); |
| 90 | + $this->db->query('DROP TABLE IF EXISTS "parent(v1)"'); |
| 91 | + |
| 92 | + $this->assertCount(1, $foreignKeys); |
| 93 | + $firstKey = reset($foreignKeys); |
| 94 | + $this->assertSame('child(v2)', $firstKey->table_name); |
| 95 | + $this->assertSame(['parent_id'], $firstKey->column_name); |
| 96 | + $this->assertSame('parent(v1)', $firstKey->foreign_table_name); |
| 97 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 98 | + } |
| 99 | + |
| 100 | + public function testGetForeignKeyDataWithQuotesAndSpacesInTableName(): void |
| 101 | + { |
| 102 | + $this->db->query('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY)'); |
| 103 | + $this->db->query('CREATE TABLE "child\'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id))'); |
| 104 | + |
| 105 | + $foreignKeys = $this->db->getForeignKeyData("child'quote with space"); |
| 106 | + |
| 107 | + $this->db->query('DROP TABLE IF EXISTS "child\'quote with space"'); |
| 108 | + $this->db->query('DROP TABLE IF EXISTS "parent table"'); |
| 109 | + |
| 110 | + $this->assertCount(1, $foreignKeys); |
| 111 | + $firstKey = reset($foreignKeys); |
| 112 | + $this->assertSame("child'quote with space", $firstKey->table_name); |
| 113 | + $this->assertSame(['p_id'], $firstKey->column_name); |
| 114 | + $this->assertSame('parent table', $firstKey->foreign_table_name); |
| 115 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 116 | + } |
| 117 | + |
| 118 | + public function testGetForeignKeyDataWithNumericAndReservedWordTableName(): void |
| 119 | + { |
| 120 | + $this->db->query('CREATE TABLE "order" (id INTEGER PRIMARY KEY)'); |
| 121 | + $this->db->query('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id))'); |
| 122 | + |
| 123 | + $foreignKeys = $this->db->getForeignKeyData('12345'); |
| 124 | + |
| 125 | + $this->db->query('DROP TABLE IF EXISTS "12345"'); |
| 126 | + $this->db->query('DROP TABLE IF EXISTS "order"'); |
| 127 | + |
| 128 | + $this->assertCount(1, $foreignKeys); |
| 129 | + $firstKey = reset($foreignKeys); |
| 130 | + $this->assertSame('12345', $firstKey->table_name); |
| 131 | + $this->assertSame(['order_id'], $firstKey->column_name); |
| 132 | + $this->assertSame('order', $firstKey->foreign_table_name); |
| 133 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 134 | + } |
| 135 | + |
| 136 | + public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers(): void |
| 137 | + { |
| 138 | + $this->db->query('ATTACH DATABASE ":memory:" AS "schema-db(1)"'); |
| 139 | + |
| 140 | + $this->db->query('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY)'); |
| 141 | + $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))'); |
| 142 | + |
| 143 | + $foreignKeys = $this->db->getForeignKeyData('schema-db(1).child(v2)'); |
| 144 | + |
| 145 | + $this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"'); |
| 146 | + $this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"'); |
| 147 | + $this->db->query('DETACH DATABASE "schema-db(1)"'); |
| 148 | + |
| 149 | + $this->assertCount(1, $foreignKeys); |
| 150 | + $firstKey = reset($foreignKeys); |
| 151 | + $this->assertSame('schema-db(1).child(v2)', $firstKey->table_name); |
| 152 | + $this->assertSame(['p_id'], $firstKey->column_name); |
| 153 | + $this->assertSame('parent(v1)', $firstKey->foreign_table_name); |
| 154 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 155 | + } |
| 156 | +} |
0 commit comments