Skip to content

test: add database layer unit tests via SQLite + MySQL-shaped PDO decorator#7127

Merged
TheWitness merged 5 commits into
Cacti:developfrom
somethingwithproof:feat/db-unit-tests
May 12, 2026
Merged

test: add database layer unit tests via SQLite + MySQL-shaped PDO decorator#7127
TheWitness merged 5 commits into
Cacti:developfrom
somethingwithproof:feat/db-unit-tests

Conversation

@somethingwithproof
Copy link
Copy Markdown
Contributor

Depends on PR-A (#7124).

Adds FakeMySQLPDO, a PDO decorator over an in-memory SQLite
handle that surfaces enough MySQL-shaped behaviour to drive the
existing db_* helpers without a live MariaDB. Drivers can ask
for information_schema-style column reads, query/prepare counters,
and configurable failure injection for retry tests.

Test files added:

  • DbStringFunctionsTest
  • DbExecutePreparedTest
  • DbReplaceSqlSaveTest
  • DbSchemaIntrospectionTest
  • DbConnectRetryTest

No production code is touched.

…orator

Introduces FakeMySQLPDO, a PDO decorator over an in-memory SQLite
handle that surfaces just enough MySQL-shaped behaviour
(information_schema-style column reads, prepared statement
execute counters, configurable failure injection) to drive the
existing db_* helpers in tests without a live MariaDB.

Adds five test files covering string sanitisers, prepared
execution, replace/save semantics, schema introspection, and
connection retry behaviour.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new unit tests around Cacti’s lib/database.php helpers, aiming to run them without a live MariaDB by using an in-memory SQLite connection and a small MySQL-syntax translation layer.

Changes:

  • Added FakeMySQLPDO (SQLite-backed) to translate a subset of MySQL-specific SQL (e.g., SHOW TABLES, SHOW COLUMNS, ON DUPLICATE KEY UPDATE) for DB unit tests.
  • Added new unit tests for string quoting/formatting helpers, schema introspection helpers, prepared execution helpers, sql_save/db_replace behavior, and connection retry behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
tests/Unit/DbStringFunctionsTest.php Adds unit coverage for array_to_sql_or, db_qstr, db_strip_control_chars, db_format_index_create.
tests/Unit/DbSchemaIntrospectionTest.php Adds unit coverage for db_table_exists / db_column_exists using the SQLite-backed decorator.
tests/Unit/DbReplaceSqlSaveTest.php Adds unit coverage for sql_save and db_replace using the decorator to emulate MySQL syntax.
tests/Unit/DbExecutePreparedTest.php Adds unit coverage for db_execute, db_execute_prepared, fetch helpers, affected rows, and insert id against SQLite.
tests/Unit/DbConnectRetryTest.php Adds a retry/failure-path test for db_connect_real() against an unreachable endpoint.
tests/Helpers/FakeMySQLPDO.php Introduces a test-only PDO subclass translating specific MySQL SQL patterns into SQLite-compatible queries.

Comment thread tests/Unit/DbStringFunctionsTest.php
Comment thread tests/Unit/DbSchemaIntrospectionTest.php
Comment thread tests/Unit/DbReplaceSqlSaveTest.php
Comment thread tests/Unit/DbExecutePreparedTest.php
Comment thread tests/Unit/DbConnectRetryTest.php
Comment thread tests/Unit/DbStringFunctionsTest.php Outdated
Comment thread tests/Helpers/FakeMySQLPDO.php Outdated
Comment thread tests/Helpers/FakeMySQLPDO.php
Comment thread tests/Helpers/FakeMySQLPDO.php
Comment thread tests/Unit/DbConnectRetryTest.php Outdated
Same as PR-A: appends 11 PHPStan ignoreErrors entries that exist on
upstream develop but are not yet baselined, so this branch's CI does
not regress on phpstan analyse --level 6.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@somethingwithproof
Copy link
Copy Markdown
Contributor Author

Note: the five "tests/Helpers/UnitStubs.php does not exist" review comments are an artifact of the stacked-PR split. That helper is added in PR-A (#7124), which this PR depends on. CI will go green here once #7124 lands.

@somethingwithproof
Copy link
Copy Markdown
Contributor Author

Queue note: this should follow #7124. After #7124 lands, rebase this PR and drop duplicated tests/Helpers/UnitStubs.php and unrelated phpstan baseline churn.

@TheWitness TheWitness merged commit 1e820e2 into Cacti:develop May 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants