From 190507009fd7c6da972e1aa0acd36abed8f96064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 20 Jul 2023 15:44:44 +0200 Subject: [PATCH] Use countDocument or estimatedDocumentsCount when no filter is provided Add functional test --- CHANGELOG.md | 2 +- src/Query/Builder.php | 6 +++++- tests/TransactionTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c0986c..9eeebaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file. - Throw an exception for unsupported `Query\Builder` methods [#9](https://github.com/GromNaN/laravel-mongodb-private/pull/9) by [@GromNaN](https://github.com/GromNaN). - Throw an exception when `Query\Builder::orderBy()` is used with invalid direction [#7](https://github.com/GromNaN/laravel-mongodb-private/pull/7) by [@GromNaN](https://github.com/GromNaN). - Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN). -- Remove call to deprecated `Collection::count` for `countDocuments` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN). +- Remove call to deprecated `Collection::count` for `countDocuments` and `estimatedDocumentCount` [#18](https://github.com/GromNaN/laravel-mongodb-private/pull/18) by [@GromNaN](https://github.com/GromNaN). ## [3.9.2] - 2022-09-01 diff --git a/src/Query/Builder.php b/src/Query/Builder.php index cc1853c..0866510 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -273,7 +273,11 @@ public function toMql(): array $aggregations = blank($this->aggregate['columns']) ? [] : $this->aggregate['columns']; if (in_array('*', $aggregations) && $function == 'count') { - return ['estimatedDocumentCount' => [$wheres, []]]; + if ($wheres) { + return ['countDocuments' => [$wheres, []]]; + } + + return ['estimatedDocumentCount' => [[], []]]; } elseif ($function == 'count') { // Translate count into sum. $group['aggregate'] = ['$sum' => 1]; diff --git a/tests/TransactionTest.php b/tests/TransactionTest.php index 46fbf2e..8c7d5d1 100644 --- a/tests/TransactionTest.php +++ b/tests/TransactionTest.php @@ -291,6 +291,35 @@ public function testDecrementWithRollBack(): void $this->assertTrue(DB::collection('users')->where('name', 'klinson')->where('age', 20)->exists()); } + /** + * @testWith [false] + * [true] + */ + public function testCount(bool $transaction) + { + if ($transaction) { + DB::beginTransaction(); + } + + $this->assertEquals(0, DB::collection('users')->count()); + $this->assertEquals(0, DB::collection('users')->where('age', 20)->count()); + DB::collection('users')->insert(['name' => 'klinson', 'age' => 20, 'title' => 'admin']); + DB::collection('users')->insert(['name' => 'bryan', 'age' => 18, 'title' => 'user']); + $this->assertEquals(2, DB::collection('users')->count()); + + // Counting fetched results + $this->assertEquals(1, DB::collection('users')->where('age', 20)->get()->count()); + + if ($transaction) { + // until transaction is committed, count with filter is incorrect + $this->assertEquals(0, DB::collection('users')->where('age', 20)->count()); + DB::commit(); + } + + $this->assertEquals(1, DB::collection('users')->where('age', 20)->count()); + $this->assertEquals(0, DB::collection('users')->where('age', 10)->count()); + } + public function testQuery() { /** rollback test */