-
Notifications
You must be signed in to change notification settings - Fork 2
Add test for count method in MongoDB integration #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
6d149c5
f2b48fa
7189b96
eebbd5b
a582d06
2138409
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -707,10 +707,34 @@ public function delete(string $collection, array $filters = [], int $limit = 1, | |||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| public function count(string $collection, array $filters, array $options): int | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| $result = $this->find($collection, $filters, $options); | ||||||||||||||||||||||||||||||
| $list = $result->cursor->firstBatch; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return \count($list); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Use MongoDB's native count command with the working format instad of running find and count the results | ||||||||||||||||||||||||||||||
| $command = [ | ||||||||||||||||||||||||||||||
| self::COMMAND_COUNT => $collection, | ||||||||||||||||||||||||||||||
| 'query' => $this->toObject($filters), | ||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Add limit if specified | ||||||||||||||||||||||||||||||
| if (isset($options['limit'])) { | ||||||||||||||||||||||||||||||
| $command['limit'] = (int)$options['limit']; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Add skip if specified | ||||||||||||||||||||||||||||||
| if (isset($options['skip'])) { | ||||||||||||||||||||||||||||||
| $command['skip'] = (int)$options['skip']; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Add maxTimeMS if specified | ||||||||||||||||||||||||||||||
| if (isset($options['maxTimeMS'])) { | ||||||||||||||||||||||||||||||
| $command['maxTimeMS'] = (int)$options['maxTimeMS']; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| $result = $this->query($command); | ||||||||||||||||||||||||||||||
| return (int)$result; | ||||||||||||||||||||||||||||||
| } catch (Exception $e) { | ||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+732
to
+738
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Don’t swallow exceptions in count(); let them surface or gate by an option Returning 0 on any error hides real issues and is inconsistent with other methods that throw. Prefer bubbling the exception, or make suppression opt-in (e.g., options['suppressExceptions']). - try {
- $result = $this->query($command);
- return (int)$result;
- } catch (Exception $e) {
- return 0;
- }
+ if (!empty($options['suppressExceptions'])) {
+ try {
+ return (int) $this->query($command);
+ } catch (Exception $e) {
+ return 0;
+ }
+ }
+ return (int) $this->query($command);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Align count() with find(): add defaults and cleanFilters()
Make filters/options optional and normalize filters like find() to correctly handle $and/$or/$nor. This prevents malformed queries and keeps API parity.
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shimonewman Do we need the
cleanFilterssince we don't use find anymore?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still need to apply cleanFilters to the filters to be used by count, same as in find()
Added it back