Skip to content

Commit 5946559

Browse files
committed
fix: tests run now with pdo_sqlite
1 parent da7a1ff commit 5946559

File tree

8 files changed

+42
-15
lines changed

8 files changed

+42
-15
lines changed

phpmyfaq/src/phpMyFAQ/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public function add(string $name, mixed $value): bool
435435
{
436436
if (!isset($this->config[$name])) {
437437
$insert = sprintf(
438-
"INSERT INTO %s%s VALUES ('%s', '%s')",
438+
"INSERT INTO %s%s (config_name, config_value) VALUES ('%s', '%s')",
439439
Database::getTablePrefix(),
440440
$this->tableName,
441441
$this->getDb()->escape(trim($name)),

phpmyfaq/src/phpMyFAQ/Database/PdoSqlite.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,24 @@ public function error(): string
8181

8282
/**
8383
* Escapes a string for use in a query.
84+
*
85+
* Note: Unlike PDO::quote(), this method intentionally does NOT add surrounding quotes.
86+
* It mirrors the behavior of other drivers (e.g., Sqlite3::escapeString), returning
87+
* only the escaped content so callers can uniformly wrap values in SQL strings.
8488
*/
8589
public function escape(string $string): string
8690
{
87-
return $this->pdo->quote($string);
91+
// For SQLite, escaping single quotes by doubling them is enough.
92+
return str_replace("'", "''", $string);
8893
}
8994

9095
/**
9196
* Fetch a result row as an associative array.
9297
*/
9398
public function fetchArray(mixed $result): ?array
9499
{
95-
return $result->fetch(PDO::FETCH_ASSOC);
100+
$row = $result->fetch(PDO::FETCH_ASSOC);
101+
return $row === false || $row === null ? null : $row;
96102
}
97103

98104
/**
@@ -126,15 +132,37 @@ public function fetchAll(mixed $result): ?array
126132
*/
127133
public function fetchObject(mixed $result): mixed
128134
{
129-
return $result->fetch(PDO::FETCH_OBJ);
135+
$obj = $result->fetch(PDO::FETCH_OBJ);
136+
return $obj === false ? null : $obj;
130137
}
131138

132139
/**
133140
* Number of rows in a result.
134141
*/
135142
public function numRows(mixed $result): int
136143
{
137-
return $result->rowCount();
144+
try {
145+
$sql = $result->queryString ?? '';
146+
if (is_string($sql) && $sql !== '' && preg_match('/^\s*SELECT\b/i', $sql) === 1) {
147+
$inner = rtrim($sql, " \t\n\r\0\x0B;");
148+
$countSql = 'SELECT COUNT(*) AS c FROM (' . $inner . ') AS _pmf_cnt';
149+
$stmt = $this->pdo->query($countSql);
150+
if ($stmt === false) {
151+
return 0;
152+
}
153+
$row = $stmt->fetch(PDO::FETCH_NUM);
154+
return isset($row[0]) ? (int) $row[0] : 0;
155+
}
156+
} catch (\Throwable) {
157+
// ignore
158+
}
159+
160+
// Fallback: for non-SELECT statements rely on rowCount (INSERT/UPDATE/DELETE)
161+
try {
162+
return (int) $result->rowCount();
163+
} catch (\Throwable) {
164+
return 0;
165+
}
138166
}
139167

140168
/**

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
//
7272
$setup = [
7373
'dbServer' => PMF_TEST_DIR . '/test.db',
74-
'dbType' => 'sqlite3',
74+
'dbType' => 'pdo_sqlite',
7575
'dbPort' => null,
7676
'dbDatabaseName' => '',
7777
'loginname' => 'admin',

tests/phpMyFAQ/Configuration/DatabaseConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public function testDBConfigProperties(): void
1616
$this->assertEquals('', $config->getPassword());
1717
$this->assertEquals('', $config->getDatabase());
1818
$this->assertEquals('', $config->getPrefix());
19-
$this->assertEquals('sqlite3', $config->getType());
19+
$this->assertEquals('pdo_sqlite', $config->getType());
2020
}
2121
}

tests/phpMyFAQ/ConfigurationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class ConfigurationTest extends TestCase
1717
{
1818
/** @var Configuration */
19-
private $configuration;
19+
private Configuration $configuration;
2020

2121
/**
2222
* Prepares the environment before running a test.
@@ -74,7 +74,6 @@ private function setupTestDatabase(): void
7474
*/
7575
protected function tearDown(): void
7676
{
77-
$this->configuration = null;
7877
parent::tearDown();
7978
}
8079

tests/phpMyFAQ/RelationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace phpMyFAQ;
44

55
use phpMyFAQ\Configuration\DatabaseConfiguration;
6-
use phpMyFAQ\Database\Sqlite3;
6+
use phpMyFAQ\Database\PdoSqlite;use phpMyFAQ\Database\Sqlite3;
77
use PHPUnit\Framework\MockObject\Exception;
88
use PHPUnit\Framework\TestCase;
99
use stdClass;
1010
use Symfony\Component\HttpFoundation\Session\Session;
1111

1212
class RelationTest extends TestCase
1313
{
14-
private Sqlite3 $db;
14+
private PdoSqlite $db;
1515

1616
protected function tearDown(): void
1717
{

tests/phpMyFAQ/Search/SearchFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class SearchFactoryTest extends TestCase
1717
{
1818
/** @var Configuration */
19-
private Configuration $configuration;
19+
private $configuration;
2020

2121
/**
2222
* Prepares the environment before running a test.
@@ -54,6 +54,6 @@ public function testCreate(): void
5454
{
5555
$search = SearchFactory::create($this->configuration, array('database' => Database::getType()));
5656

57-
$this->assertInstanceOf('phpMyFAQ\Search\Database\Sqlite3', $search);
57+
$this->assertInstanceOf('phpMyFAQ\Search\Database\PdoSqlite', $search);
5858
}
5959
}

tests/phpMyFAQ/SitemapTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace phpMyFAQ;
44

55
use phpMyFAQ\Configuration\DatabaseConfiguration;
6-
use phpMyFAQ\Database\Sqlite3;
6+
use phpMyFAQ\Database\PdoSqlite;use phpMyFAQ\Database\Sqlite3;
77
use PHPUnit\Framework\MockObject\Exception;
88
use PHPUnit\Framework\TestCase;
99
use stdClass;
@@ -13,7 +13,7 @@ class SitemapTest extends TestCase
1313
{
1414
private Sitemap $sitemap;
1515

16-
private Sqlite3 $db;
16+
private PdoSqlite $db;
1717

1818
/**
1919
* @throws Exception

0 commit comments

Comments
 (0)