diff --git a/flight/database/PdoWrapper.php b/flight/database/PdoWrapper.php index 6f37b25b..4c036cdd 100644 --- a/flight/database/PdoWrapper.php +++ b/flight/database/PdoWrapper.php @@ -91,11 +91,14 @@ public function runQuery(string $sql, array $params = []): PDOStatement * @param string $sql - Ex: "SELECT id FROM table WHERE something = ?" * @param array $params - Ex: [ $something ] * - * @return mixed + * @return mixed|false */ public function fetchField(string $sql, array $params = []) { $result = $this->fetchRow($sql, $params); + if ($result === null || count($result) === 0) { + return false; + } $data = $result->getData(); return reset($data); } diff --git a/tests/PdoWrapperTest.php b/tests/PdoWrapperTest.php index d7e9311b..132156be 100644 --- a/tests/PdoWrapperTest.php +++ b/tests/PdoWrapperTest.php @@ -68,9 +68,37 @@ public function testRunQueryDeleteStatement(): void public function testFetchField(): void { $id = $this->pdo_wrapper->fetchField('SELECT id FROM test WHERE name = ?', ['two']); + // PDO SQLite may return numeric strings on PHP < 8.1 $this->assertEquals(2, $id); } + public function testFetchFieldReturnsFalseWhenNoResults(): void + { + $id = $this->pdo_wrapper->fetchField('SELECT id FROM test WHERE id = ?', [999]); + $this->assertSame(false, $id); + } + + public function testFetchFieldReturnsNullWhenColumnIsSqlNull(): void + { + $this->pdo_wrapper->exec('INSERT INTO test (name) VALUES (NULL)'); + $value = $this->pdo_wrapper->fetchField('SELECT name FROM test WHERE name IS NULL'); + $this->assertNull($value); + } + + public function testFetchFieldReturnsZero(): void + { + $value = $this->pdo_wrapper->fetchField('SELECT 0'); + // PDO SQLite may return '0' on PHP < 8.1 + $this->assertEquals(0, $value); + } + + public function testFetchFieldReturnsEmptyString(): void + { + $this->pdo_wrapper->exec('INSERT INTO test (name) VALUES ("")'); + $value = $this->pdo_wrapper->fetchField('SELECT name FROM test WHERE name = ?', ['']); + $this->assertSame('', $value); + } + public function testFetchRow(): void { $row = $this->pdo_wrapper->fetchRow('SELECT * FROM test WHERE name = ?', ['two']); diff --git a/tests/SimplePdoTest.php b/tests/SimplePdoTest.php index 08f8e164..0143381d 100644 --- a/tests/SimplePdoTest.php +++ b/tests/SimplePdoTest.php @@ -460,12 +460,40 @@ public function testTransactionRethrowsException(): void public function testFetchFieldReturnsValue(): void { $name = $this->db->fetchField('SELECT name FROM users WHERE id = ?', [1]); - $this->assertEquals('John', $name); + $this->assertSame('John', $name); } public function testFetchFieldReturnsFirstColumn(): void { $id = $this->db->fetchField('SELECT id, name FROM users WHERE id = ?', [1]); + // PDO SQLite may return numeric strings on PHP < 8.1 $this->assertEquals(1, $id); } + + public function testFetchFieldReturnsFalseWhenNoResults(): void + { + $value = $this->db->fetchField('SELECT name FROM users WHERE id = ?', [999]); + $this->assertSame(false, $value); + } + + public function testFetchFieldReturnsNullWhenColumnIsSqlNull(): void + { + $this->db->exec('INSERT INTO users (name, email) VALUES (NULL, "null@example.com")'); + $value = $this->db->fetchField('SELECT name FROM users WHERE email = ?', ['null@example.com']); + $this->assertNull($value); + } + + public function testFetchFieldReturnsZero(): void + { + $value = $this->db->fetchField('SELECT 0'); + // PDO SQLite may return '0' on PHP < 8.1 + $this->assertEquals(0, $value); + } + + public function testFetchFieldReturnsEmptyString(): void + { + $this->db->exec('INSERT INTO users (name, email) VALUES ("", "empty@example.com")'); + $value = $this->db->fetchField('SELECT name FROM users WHERE email = ?', ['empty@example.com']); + $this->assertSame('', $value); + } }