From 6d149c5cb89ee95eac52689466bd80b46b9e26ec Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 31 Aug 2025 16:22:09 +0300 Subject: [PATCH] Add test for count method in MongoDB integration This commit introduces a new test case in MongoTest.php to validate the functionality of the count method in the database. The test covers various scenarios including total count, filtered counts, counts with limits, and aggregation counts, ensuring comprehensive coverage of the counting capabilities. --- src/Client.php | 30 +++++++++++++++++-- tests/MongoTest.php | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 975d6245ed..095d155397 100644 --- a/src/Client.php +++ b/src/Client.php @@ -707,10 +707,34 @@ class Client */ public function count(string $collection, array $filters, array $options): int { - $result = $this->find($collection, $filters, $options); - $list = $result->cursor->firstBatch; + + // 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), + ]; - return \count($list); + // 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; + } } /** diff --git a/tests/MongoTest.php b/tests/MongoTest.php index aba9be3653..1337d03a1e 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -261,4 +261,75 @@ class MongoTest extends TestCase self::assertEquals('USA', $documents[1]->country); self::assertEquals('English', $documents[1]->language); } + + public function testCountMethod() + { + + $collectionName = 'count_test'; + $this->getDatabase()->createCollection($collectionName); + + $documents = []; + for ($i = 1; $i <= 30; $i++) { + $documents[] = [ + 'name' => "Document {$i}", + 'number' => $i, + 'category' => 'test', + 'created_at' => new \DateTime() + ]; + } + + $this->getDatabase()->insertMany($collectionName, $documents); + + $total = $this->getDatabase()->count($collectionName, [], []); + self::assertEquals(30, $total); + + // Test count with filter (should be 1 for each specific number) + $total = $this->getDatabase()->count($collectionName, ['number' => 15], []); + self::assertEquals(1, $total); + + // Test count with range filter (should be 10 for numbers 1-10) + $total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], []); + self::assertEquals(10, $total); + + // Test count with limit (should be 5 for first 5 documents) + $total = $this->getDatabase()->count($collectionName, [], ['limit' => 5]); + self::assertEquals(5, $total); + + // Test count with filter and limit (should be 3 for first 3 documents with number <= 10) + $total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], ['limit' => 3]); + self::assertEquals(3, $total); + + // Test aggregation count - total documents + $aggregationResult = $this->getDatabase()->aggregate($collectionName, [ + ['$count' => 'total'] + ]); + self::assertEquals(30, $aggregationResult->cursor->firstBatch[0]->total); + + // Test aggregation count with filter + $filteredAggregationResult = $this->getDatabase()->aggregate($collectionName, [ + ['$match' => ['number' => ['$lte' => 10]]], + ['$count' => 'total'] + ]); + self::assertEquals(10, $filteredAggregationResult->cursor->firstBatch[0]->total); + + // Test aggregation count with limit + $limitedAggregationResult = $this->getDatabase()->aggregate($collectionName, [ + ['$limit' => 7], + ['$count' => 'total'] + ]); + self::assertEquals(7, $limitedAggregationResult->cursor->firstBatch[0]->total); + + // Test aggregation count with group by + $groupedAggregationResult = $this->getDatabase()->aggregate($collectionName, [ + ['$group' => [ + '_id' => '$category', // Group by category + 'count' => ['$sum' => 1] // Count of documents in the group + ]] + ]); + self::assertEquals(30, $groupedAggregationResult->cursor->firstBatch[0]->count); + self::assertEquals('test', $groupedAggregationResult->cursor->firstBatch[0]->_id); + + + $this->getDatabase()->dropCollection($collectionName); + } }