Add tests to improve coverage of untested or under-tested APIs#1975
Add tests to improve coverage of untested or under-tested APIs#1975GromNaN merged 4 commits intomongodb:v2.2from
Conversation
- 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
There was a problem hiding this comment.
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 combinedstartTransaction()options. - Added exception tests for
hasErrorLabel()coverage andBulkWriteCommandException::getWriteConcernErrors(). - Added option validation/acceptance tests for
Query::__construct()andCommand::__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.
- 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
There was a problem hiding this comment.
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.
3d27b22 to
71f8081
Compare
There was a problem hiding this comment.
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.
| $labels = ['test', 'foo']; | ||
|
|
||
| $reflection = new ReflectionClass($exception); | ||
| $errorLabelsProperty = $reflection->getProperty('errorLabels'); |
There was a problem hiding this comment.
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.
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.phptSession::getServer()had no test at all. Documents that the method returnsnullwhen the session is not pinned to a server. Note: mongos pinning for sharded transactions was removed in MongoDB 6.0+, sogetServer()now consistently returnsnullin 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 bygetTransactionOptions().tests/exception/executiontimeoutexception-haserrorlabel-001.phpthasErrorLabel()was tested onRuntimeExceptionandBulkWriteExceptionbut not onExecutionTimeoutExceptiondespite inheriting the method. Uses the sameReflectionClasspattern as existing tests.tests/exception/connectiontimeoutexception-haserrorlabel-001.phptSame gap as above for
ConnectionTimeoutException.tests/bulkwritecommand/bulkwritecommand-ctor-ordered-002.phpt(modified)BulkWriteCommandException::getWriteConcernErrors()appeared in debug output but was never explicitly asserted. Addedvar_dump($e->getWriteConcernErrors())to the existingordered=falsetest, 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.phptmaxAwaitTimeMSinQuery::__construct()had range validation tests but no test verifying that valid values are accepted without error. Covers0and1000.tests/query/query-ctor-maxAwaitTimeMS-002.phptComplements 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 asquery-ctor_error-006.phpt).tests/bulkwritecommand/bulkwritecommand-ctor-options-combined-001.phptBulkWriteCommandconstructor 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.phptCommand::__construct()validatesmaxAwaitTimeMS >= 0inCommand.c(same logic asQuery), but had no corresponding test.