|
| 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\Forge; |
| 15 | + |
| 16 | +use CodeIgniter\Database\OCI8\Connection as OCI8Connection; |
| 17 | +use CodeIgniter\Database\OCI8\Forge as OCI8Forge; |
| 18 | +use CodeIgniter\Database\Postgre\Connection as PostgreConnection; |
| 19 | +use CodeIgniter\Database\Postgre\Forge as PostgreForge; |
| 20 | +use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection; |
| 21 | +use CodeIgniter\Database\SQLSRV\Forge as SQLSRVForge; |
| 22 | +use CodeIgniter\Test\CIUnitTestCase; |
| 23 | +use CodeIgniter\Test\Mock\MockConnection; |
| 24 | +use PHPUnit\Framework\Attributes\Group; |
| 25 | + |
| 26 | +/** |
| 27 | + * @internal |
| 28 | + */ |
| 29 | +#[Group('Others')] |
| 30 | +final class DropKeyAsConstraintTest extends CIUnitTestCase |
| 31 | +{ |
| 32 | + protected $db; |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->db = new MockConnection([]); |
| 39 | + } |
| 40 | + |
| 41 | + public function testPostgreDropKeyAsConstraintEscaping(): void |
| 42 | + { |
| 43 | + $this->setPrivateProperty($this->db, 'schema', "my'schema"); |
| 44 | + |
| 45 | + /** @var PostgreConnection $db */ |
| 46 | + $db = $this->db; |
| 47 | + |
| 48 | + $forge = new PostgreForge($db); |
| 49 | + $method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint'); |
| 50 | + |
| 51 | + $sql = $method("user's", "fk'id"); |
| 52 | + |
| 53 | + $expected = <<<'SQL' |
| 54 | + SELECT con.conname |
| 55 | + FROM pg_catalog.pg_constraint con |
| 56 | + INNER JOIN pg_catalog.pg_class rel |
| 57 | + ON rel.oid = con.conrelid |
| 58 | + INNER JOIN pg_catalog.pg_namespace nsp |
| 59 | + ON nsp.oid = connamespace |
| 60 | + WHERE nsp.nspname = 'my''schema' |
| 61 | + AND rel.relname = 'user''s' |
| 62 | + AND con.conname = 'fk''id' |
| 63 | + SQL; |
| 64 | + |
| 65 | + $this->assertSame($expected, $sql); |
| 66 | + } |
| 67 | + |
| 68 | + public function testOCI8DropKeyAsConstraintEscaping(): void |
| 69 | + { |
| 70 | + /** @var OCI8Connection $db */ |
| 71 | + $db = $this->db; |
| 72 | + |
| 73 | + $forge = new OCI8Forge($db); |
| 74 | + $method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint'); |
| 75 | + |
| 76 | + $sql = $method("user's", "fk'id"); |
| 77 | + |
| 78 | + $expected = <<<'SQL' |
| 79 | + SELECT constraint_name |
| 80 | + FROM all_constraints |
| 81 | + WHERE table_name = 'user''s' |
| 82 | + AND index_name = 'fk''id' |
| 83 | + SQL; |
| 84 | + |
| 85 | + $this->assertSame($expected, $sql); |
| 86 | + } |
| 87 | + |
| 88 | + public function testSQLSRVDropKeyAsConstraintEscaping(): void |
| 89 | + { |
| 90 | + $this->setPrivateProperty($this->db, 'schema', 'dbo'); |
| 91 | + |
| 92 | + /** @var SQLSRVConnection $db */ |
| 93 | + $db = $this->db; |
| 94 | + |
| 95 | + $forge = new SQLSRVForge($db); |
| 96 | + $method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint'); |
| 97 | + |
| 98 | + $sql = $method("user's", "fk'id"); |
| 99 | + |
| 100 | + $expected = <<<'SQL' |
| 101 | + SELECT CONSTRAINT_NAME |
| 102 | + FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS |
| 103 | + WHERE TABLE_NAME = 'user''s' |
| 104 | + AND CONSTRAINT_NAME = 'fk''id' |
| 105 | + SQL; |
| 106 | + |
| 107 | + $this->assertSame($expected, $sql); |
| 108 | + } |
| 109 | +} |
0 commit comments