Skip to content

Add tests to improve coverage of untested or under-tested APIs#1975

Merged
GromNaN merged 4 commits intomongodb:v2.2from
GromNaN:add-test-coverage
Apr 8, 2026
Merged

Add tests to improve coverage of untested or under-tested APIs#1975
GromNaN merged 4 commits intomongodb:v2.2from
GromNaN:add-test-coverage

Conversation

@GromNaN
Copy link
Copy Markdown
Member

@GromNaN GromNaN commented Apr 7, 2026

Context

This PR is the result of a systematic test coverage audit across all public PHP classes in the extension. The audit searched for methods and behaviors with no dedicated tests or only tested indirectly (e.g., via debug output or exception message strings).

Tests added

tests/session/session-getServer-001.phpt

Session::getServer() had no test at all. Documents that the method returns null when the session is not pinned to a server. Note: mongos pinning for sharded transactions was removed in MongoDB 6.0+, so getServer() now consistently returns null in all topologies.

tests/session/session-getTransactionOptions-001.phpt (modified)

The existing test covered each transaction option (maxCommitTimeMS, readConcern, readPreference, writeConcern) in isolation. A combined case was added to verify all four options coexist correctly and are all reflected by getTransactionOptions().

tests/exception/executiontimeoutexception-haserrorlabel-001.phpt

hasErrorLabel() was tested on RuntimeException and BulkWriteException but not on ExecutionTimeoutException despite inheriting the method. Uses the same ReflectionClass pattern as existing tests.

tests/exception/connectiontimeoutexception-haserrorlabel-001.phpt

Same gap as above for ConnectionTimeoutException.

tests/bulkwritecommand/bulkwritecommand-ctor-ordered-002.phpt (modified)

BulkWriteCommandException::getWriteConcernErrors() appeared in debug output but was never explicitly asserted. Added var_dump($e->getWriteConcernErrors()) to the existing ordered=false test, which already triggers the same duplicate key scenario, verifying it returns an empty array when only write errors (not write concern errors) occur.

tests/query/query-ctor-maxAwaitTimeMS-001.phpt

maxAwaitTimeMS in Query::__construct() had range validation tests but no test verifying that valid values are accepted without error. Covers 0 and 1000.

tests/query/query-ctor-maxAwaitTimeMS-002.phpt

Complements the above with the upper boundary value UINT32_MAX (4294967295), guarded with a 64-bit platform skip to avoid integer overflow on 32-bit builds (same pattern as query-ctor_error-006.phpt).

tests/bulkwritecommand/bulkwritecommand-ctor-options-combined-001.phpt

BulkWriteCommand constructor options (ordered, comment, verboseResults) were each tested in isolation. This test verifies they work correctly when combined, using APM to assert the options appear in the sent command.

tests/command/command-ctor_error-002.phpt

Command::__construct() validates maxAwaitTimeMS >= 0 in Command.c (same logic as Query), but had no corresponding test.

- Session::getServer() — no test existed; documents that the method
  returns null when the session is not pinned to a server (pinning
  was removed for sharded clusters in MongoDB 6.0+)
- Session::startTransaction() with all options combined — each option
  (maxCommitTimeMS, readConcern, readPreference, writeConcern) was
  only tested in isolation; this test verifies they coexist correctly
  and are all reflected by getTransactionOptions()
- ExecutionTimeoutException::hasErrorLabel() — hasErrorLabel() was
  tested on RuntimeException and BulkWriteException but not on this
  subclass
- ConnectionTimeoutException::hasErrorLabel() — same gap as above
- BulkWriteCommandException::getWriteConcernErrors() — the method
  appeared in debug output but was never explicitly asserted; this
  test verifies it returns an empty array when only write errors occur
- Query construction with maxAwaitTimeMS — only error/range validation
  tests existed; this test verifies valid values are accepted
- BulkWriteCommand constructor with multiple options combined — each
  option (ordered, comment, verboseResults) was tested in isolation;
  this test verifies they work together
- Command construction with invalid maxAwaitTimeMS — the >= 0
  validation existed in Command.c but had no corresponding test
@GromNaN GromNaN requested a review from a team as a code owner April 7, 2026 15:00
@GromNaN GromNaN requested review from alcaeus and Copilot and removed request for a team April 7, 2026 15:00
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

This PR adds new PHPT tests to cover previously untested or under-tested public APIs in the PHP MongoDB extension, focusing on sessions, transaction options, exception helpers, and option validation.

Changes:

  • Added session tests for Session::getServer() and combined startTransaction() options.
  • Added exception tests for hasErrorLabel() coverage and BulkWriteCommandException::getWriteConcernErrors().
  • Added option validation/acceptance tests for Query::__construct() and Command::__construct() (maxAwaitTimeMS).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/session/session-startTransaction-002.phpt Verifies all transaction options can be set together and reflected via getTransactionOptions().
tests/session/session-getServer-001.phpt Adds direct coverage for Session::getServer() returning null when not pinned.
tests/query/query-ctor-maxAwaitTimeMS-001.phpt Ensures valid maxAwaitTimeMS values are accepted by Query constructor.
tests/exception/executiontimeoutexception-haserrorlabel-001.phpt Covers ExecutionTimeoutException::hasErrorLabel() behavior via reflection-set labels.
tests/exception/connectiontimeoutexception-haserrorlabel-001.phpt Covers ConnectionTimeoutException::hasErrorLabel() behavior via reflection-set labels.
tests/exception/bulkwritecommandexception-getwriteconcernerrors-001.phpt Asserts getWriteConcernErrors() returns an empty array for duplicate key write errors.
tests/command/command-ctor_error-002.phpt Adds missing negative-range validation test for Command’s maxAwaitTimeMS.
tests/bulkwritecommand/bulkwritecommand-ctor-options-combined-001.phpt Verifies combined BulkWriteCommand options (ordered, comment, verboseResults) using APM + result assertions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/query/query-ctor-maxAwaitTimeMS-001.phpt Outdated
Comment thread tests/session/session-getServer-001.phpt Outdated
GromNaN added 2 commits April 7, 2026 17:19
- query-ctor-maxAwaitTimeMS-001: remove UINT32_MAX case (4294967295
  overflows PHP_INT_MAX on 32-bit platforms, causing float coercion)
- session-getServer-001: update comment to accurately reflect that
  mongos pinning was removed in MongoDB 6.0+
- bulkwritecommand-ctor-ordered-002: add getWriteConcernErrors() assertion
  (verifies empty array when only write errors occur); removes the need
  for a separate bulkwritecommandexception-getwriteconcernerrors-001.phpt
- session-getTransactionOptions-001: add combined-options case (all four
  transaction options together); removes the need for a separate
  session-startTransaction-002.phpt
Copilot AI review requested due to automatic review settings April 7, 2026 15:44
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

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/query/query-ctor-maxAwaitTimeMS-001.phpt
Comment thread tests/bulkwritecommand/bulkwritecommand-ctor-options-combined-001.phpt Outdated
Copilot AI review requested due to automatic review settings April 7, 2026 16:36
@GromNaN GromNaN force-pushed the add-test-coverage branch from 3d27b22 to 71f8081 Compare April 7, 2026 16:36
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

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@alcaeus
Copy link
Copy Markdown
Member

alcaeus commented Apr 8, 2026

Test failure is related to #1953 which was merged to v2.x only. @GromNaN I think we can merge this despite the failure or alternatively target 2.x with the changes.

$labels = ['test', 'foo'];

$reflection = new ReflectionClass($exception);
$errorLabelsProperty = $reflection->getProperty('errorLabels');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll note that this property will become public as part of the properties rework. I'll make a note to update the tests and remove reflection where we no longer need it.

@GromNaN GromNaN merged commit bfb5fb9 into mongodb:v2.2 Apr 8, 2026
46 of 47 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