|
| 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\Postgre; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use PHPUnit\Framework\Attributes\Group; |
| 18 | +use stdClass; |
| 19 | + |
| 20 | +/** |
| 21 | + * Mock pg_version in the Postgre namespace to test insertID without live DB connection. |
| 22 | + * |
| 23 | + * @param mixed $connection |
| 24 | + * |
| 25 | + * @return array{server: string} |
| 26 | + */ |
| 27 | +function pg_version($connection = null): array |
| 28 | +{ |
| 29 | + return ['server' => '15.0']; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Mock pg_escape_literal in the Postgre namespace for testing without live DB connection. |
| 34 | + * |
| 35 | + * @param mixed $connection |
| 36 | + */ |
| 37 | +function pg_escape_literal($connection, string $string): string |
| 38 | +{ |
| 39 | + return "'" . str_replace("'", "''", $string) . "'"; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * @internal |
| 44 | + */ |
| 45 | +#[Group('Others')] |
| 46 | +final class InsertIDTest extends CIUnitTestCase |
| 47 | +{ |
| 48 | + public function testInsertIDQuotesTableColumnAndSequenceNames(): void |
| 49 | + { |
| 50 | + $connection = new class () extends Connection { |
| 51 | + /** |
| 52 | + * @var list<string> |
| 53 | + */ |
| 54 | + public array $queries = []; |
| 55 | + |
| 56 | + public function __construct() |
| 57 | + { |
| 58 | + $this->DBDriver = 'Postgre'; |
| 59 | + $this->connID = false; |
| 60 | + } |
| 61 | + |
| 62 | + public function initialize(): void |
| 63 | + { |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param array<array-key, mixed>|string|null $binds |
| 68 | + */ |
| 69 | + public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = ''): Result |
| 70 | + { |
| 71 | + $this->queries[] = $sql; |
| 72 | + |
| 73 | + $row = new stdClass(); |
| 74 | + $row->seq = "user's_id_seq"; |
| 75 | + $row->ins_id = 123; |
| 76 | + |
| 77 | + $result = new class ($this->connID, $this->resultID) extends Result { |
| 78 | + public function setRowData(stdClass $row): void |
| 79 | + { |
| 80 | + $this->resultObject = [$row]; |
| 81 | + } |
| 82 | + }; |
| 83 | + $result->setRowData($row); |
| 84 | + |
| 85 | + return $result; |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + $id = $connection->insertID("user's", "id'col"); |
| 90 | + |
| 91 | + $this->assertSame(123, $id); |
| 92 | + $this->assertCount(2, $connection->queries); |
| 93 | + $this->assertSame( |
| 94 | + "SELECT pg_get_serial_sequence('user''s', 'id''col') AS seq", |
| 95 | + $connection->queries[0], |
| 96 | + ); |
| 97 | + $this->assertSame( |
| 98 | + "SELECT CURRVAL('user''s_id_seq') AS ins_id", |
| 99 | + $connection->queries[1], |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments