|
| 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('DROP TABLE IF EXISTS `test-parent`'); |
| 46 | + $this->db->query('DROP TABLE IF EXISTS `test-child`'); |
| 47 | + |
| 48 | + $this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)'); |
| 49 | + $this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))'); |
| 50 | + |
| 51 | + $foreignKeys = $this->db->getForeignKeyData('test-child'); |
| 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 | + $this->db->query('DROP TABLE IF EXISTS `test-child`'); |
| 61 | + $this->db->query('DROP TABLE IF EXISTS `test-parent`'); |
| 62 | + } |
| 63 | + |
| 64 | + public function testGetForeignKeyDataWithAttachedDatabase(): void |
| 65 | + { |
| 66 | + $this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`'); |
| 67 | + $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`'); |
| 68 | + $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`'); |
| 69 | + |
| 70 | + $this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)'); |
| 71 | + $this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))'); |
| 72 | + |
| 73 | + $foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child'); |
| 74 | + |
| 75 | + $this->assertCount(1, $foreignKeys); |
| 76 | + $firstKey = reset($foreignKeys); |
| 77 | + $this->assertSame('test_schema.attached-child', $firstKey->table_name); |
| 78 | + $this->assertSame(['p_id'], $firstKey->column_name); |
| 79 | + $this->assertSame('attached-parent', $firstKey->foreign_table_name); |
| 80 | + $this->assertSame(['id'], $firstKey->foreign_column_name); |
| 81 | + |
| 82 | + $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`'); |
| 83 | + $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`'); |
| 84 | + $this->db->query('DETACH DATABASE `test_schema`'); |
| 85 | + } |
| 86 | +} |
0 commit comments