diff options
| author | Mario <mario@mariovavti.com> | 2025-11-13 16:14:06 +0000 |
|---|---|---|
| committer | Mario <mario@mariovavti.com> | 2025-11-13 16:14:06 +0000 |
| commit | 4b389ddba933a100b88a49c66328289349f48697 (patch) | |
| tree | 4240608186fa8ad669a8e950888fd38ed527a262 /tests/unit/includes/dba/DbaPdoTest.php | |
| parent | b6153edf6b5b083e9664733b5d1d8fce5f2c6e07 (diff) | |
| parent | 1e92aeb7f9a23eee65440bee24c153f10a750a23 (diff) | |
| download | volse-hubzilla-dev.tar.gz volse-hubzilla-dev.tar.bz2 volse-hubzilla-dev.zip | |
Improve dba_pdo::insert function
See merge request hubzilla/core!2241
Diffstat (limited to 'tests/unit/includes/dba/DbaPdoTest.php')
| -rw-r--r-- | tests/unit/includes/dba/DbaPdoTest.php | 50 |
1 files changed, 16 insertions, 34 deletions
diff --git a/tests/unit/includes/dba/DbaPdoTest.php b/tests/unit/includes/dba/DbaPdoTest.php index 8a1a2b197..1468ce4ed 100644 --- a/tests/unit/includes/dba/DbaPdoTest.php +++ b/tests/unit/includes/dba/DbaPdoTest.php @@ -12,39 +12,16 @@ namespace Zotlabs\Tests\Unit\includes; use DBA; use PDO; -use PDOStatement; +use PDOException; use PHPUnit\Framework\Attributes\DataProvider; use Zotlabs\Tests\Unit\UnitTestCase; class DbaPdoTest extends UnitTestCase { - public function testInsertingRowWithRturningClauseReturnsInsertedRow(): void - { - // MySQL does not support the `returning` clause, so we skip the test - // for that DB backend. - $this->skipIfMySQL(); - - // Let's manually insert a row in the config table. - // This is just because it's a conventient table to test - // against - $res = q(<<<SQL - INSERT INTO config (cat, k, v) - VALUES ('test', 'a key', 'A value') - RETURNING * - SQL); - - $this->assertIsArray($res); - $this->assertIsArray($res[0]); - $this->assertTrue($res[0]['id'] > 0); - $this->assertEquals('test', $res[0]['cat']); - $this->assertEquals('a key', $res[0]['k']); - $this->assertEquals('A value', $res[0]['v']); - } - #[DataProvider('insertRowProvider')] public function testInsertRow(string $table, array $data, string $id): void { - $res = DBA::$dba->insert($table, $data, $id); + $res = DBA::$dba->insert($table, $data); $this->assertIsArray($res); @@ -57,18 +34,23 @@ class DbaPdoTest extends UnitTestCase } #[DataProvider('insertRowProvider')] - public function testInsertShouldReturnFalseIfInsertFails( - string $table, - array $data, - string $id - ): void + public function testInsertThrowsOnDuplicateId(string $table, array $data): void { - $res1 = DBA::$dba->insert($table, $data, $id); + $this->expectException(PDOException::class); + if (DBA::$dba->is_postgres()) { + // Postgres uses 23505 to signal a unique violation + $this->expectExceptionCode(23505); + } else { + // MySQL and MariaDB just signal a constraint violation without + // being more specific + $this->expectExceptionCode(23000); + } + + $res1 = DBA::$dba->insert($table, $data); $this->assertIsArray($res1); - // Inserting the same row again should fail. - $res2 = DBA::$dba->insert($table, $data, $id); - $this->assertFalse($res2); + // Inserting the same row again should throw a PDOException. + $res2 = DBA::$dba->insert($table, $data); } /** |
