44
55namespace Codeception \Module ;
66
7- use Codeception \Lib \Interfaces \RequiresPackage ;
87use Codeception \Module ;
98use Codeception \Configuration ;
109use Codeception \Exception \ModuleConfigException ;
1312use Codeception \TestInterface ;
1413use Exception ;
1514use MongoConnectionException ;
15+ use PHPUnit \Framework \Assert ;
16+ use PHPUnit \Framework \ExpectationFailedException ;
1617
1718/**
1819 * Works with MongoDb database.
5758 * checked using the [dbHash](https://docs.mongodb.com/manual/reference/command/dbHash/) command.
5859 *
5960 */
60- class MongoDb extends Module implements RequiresPackage
61+ class MongoDb extends Module
6162{
6263 /**
6364 * @var string
@@ -74,25 +75,16 @@ class MongoDb extends Module implements RequiresPackage
7475 */
7576 public const DUMP_TYPE_MONGODUMP_TAR_GZ = 'mongodump-tar-gz ' ;
7677
77- /**
78- * @api
79- * @var
80- */
81- public $ dbh ;
82-
83- protected ?string $ dumpFile = null ;
78+ protected string $ dumpFile ;
8479
8580 protected bool $ isDumpFileEmpty = true ;
8681
87- /**
88- * @var mixed|null
89- */
90- protected $ dbHash ;
82+ protected ?string $ dbHash ;
9183
9284 /**
93- * @var array
85+ * @var array<string, mixed>
9486 */
95- protected $ config = [
87+ protected array $ config = [
9688 'populate ' => true ,
9789 'cleanup ' => true ,
9890 'dsn ' => '' ,
@@ -110,9 +102,9 @@ class MongoDb extends Module implements RequiresPackage
110102 /**
111103 * @var string[]
112104 */
113- protected $ requiredFields = ['dsn ' ];
105+ protected array $ requiredFields = ['dsn ' ];
114106
115- public function _initialize ()
107+ public function _initialize (): void
116108 {
117109 try {
118110 $ this ->driver = MongoDbDriver::create (
@@ -208,15 +200,15 @@ private function validateDump(): void
208200 }
209201 }
210202
211- public function _before (TestInterface $ test )
203+ public function _before (TestInterface $ test ): void
212204 {
213205 if ($ this ->shouldCleanup ()) {
214206 $ this ->cleanup ();
215207 $ this ->loadDump ();
216208 }
217209 }
218210
219- public function _after (TestInterface $ test )
211+ public function _after (TestInterface $ test ): void
220212 {
221213 $ this ->populated = false ;
222214 }
@@ -301,6 +293,8 @@ public function useDatabase(string $dbName): void
301293 * $I->haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']);
302294 * $user_id = $I->haveInCollection('users', ['email' => 'john@coltrane.com']);
303295 * ```
296+ *
297+ * @param array<string, mixed> $data
304298 */
305299 public function haveInCollection (string $ collection , array $ data ): string
306300 {
@@ -317,12 +311,14 @@ public function haveInCollection(string $collection, array $data): string
317311 * <?php
318312 * $I->seeInCollection('users', ['name' => 'miles']);
319313 * ```
314+ *
315+ * @param array<string, mixed> $criteria
320316 */
321317 public function seeInCollection (string $ collection , array $ criteria = []): void
322318 {
323319 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
324320 $ res = $ collection ->count ($ criteria );
325- \ PHPUnit \ Framework \ Assert::assertGreaterThan (0 , $ res );
321+ Assert::assertGreaterThan (0 , $ res );
326322 }
327323
328324 /**
@@ -332,12 +328,14 @@ public function seeInCollection(string $collection, array $criteria = []): void
332328 * <?php
333329 * $I->dontSeeInCollection('users', ['name' => 'miles']);
334330 * ```
331+ *
332+ * @param array<string, mixed> $criteria
335333 */
336334 public function dontSeeInCollection (string $ collection , array $ criteria = []): void
337335 {
338336 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
339337 $ res = $ collection ->count ($ criteria );
340- \ PHPUnit \ Framework \ Assert::assertLessThan (1 , $ res );
338+ Assert::assertLessThan (1 , $ res );
341339 }
342340
343341 /**
@@ -348,9 +346,9 @@ public function dontSeeInCollection(string $collection, array $criteria = []): v
348346 * $user = $I->grabFromCollection('users', ['name' => 'miles']);
349347 * ```
350348 *
351- * @return \MongoDB\Model\BSONDocument| mixed
349+ * @param array<string, mixed> $criteria
352350 */
353- public function grabFromCollection (string $ collection , array $ criteria = [])
351+ public function grabFromCollection (string $ collection , array $ criteria = []): array | object | null
354352 {
355353 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
356354 return $ collection ->findOne ($ criteria );
@@ -365,6 +363,8 @@ public function grabFromCollection(string $collection, array $criteria = [])
365363 * // or
366364 * $count = $I->grabCollectionCount('users', ['isAdmin' => true]);
367365 * ```
366+ *
367+ * @param array<string, mixed> $criteria
368368 */
369369 public function grabCollectionCount (string $ collection , array $ criteria = []): int
370370 {
@@ -379,8 +379,10 @@ public function grabCollectionCount(string $collection, array $criteria = []): i
379379 * <?php
380380 * $I->seeElementIsArray('users', ['name' => 'John Doe'], 'data.skills');
381381 * ```
382+ *
383+ * @param array<string, mixed> $criteria
382384 */
383- public function seeElementIsArray (string $ collection , array $ criteria = [] , string $ elementToCheck = null ): void
385+ public function seeElementIsArray (string $ collection , array $ criteria , string $ elementToCheck ): void
384386 {
385387 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
386388
@@ -394,12 +396,11 @@ public function seeElementIsArray(string $collection, array $criteria = [], stri
394396 )
395397 );
396398 if ($ res > 1 ) {
397- throw new \ PHPUnit \ Framework \ ExpectationFailedException (
399+ throw new ExpectationFailedException (
398400 'Error: you should test against a single element criteria when asserting that elementIsArray '
399401 );
400402 }
401-
402- \PHPUnit \Framework \Assert::assertEquals (1 , $ res , 'Specified element is not a Mongo Object ' );
403+ Assert::assertSame (1 , $ res , 'Specified element is not a Mongo Object ' );
403404 }
404405
405406 /**
@@ -409,8 +410,10 @@ public function seeElementIsArray(string $collection, array $criteria = [], stri
409410 * <?php
410411 * $I->seeElementIsObject('users', ['name' => 'John Doe'], 'data');
411412 * ```
413+ *
414+ * @param array<string, mixed> $criteria
412415 */
413- public function seeElementIsObject (string $ collection , array $ criteria = [] , string $ elementToCheck = null ): void
416+ public function seeElementIsObject (string $ collection , array $ criteria , string $ elementToCheck ): void
414417 {
415418 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
416419
@@ -424,12 +427,11 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
424427 )
425428 );
426429 if ($ res > 1 ) {
427- throw new \ PHPUnit \ Framework \ ExpectationFailedException (
430+ throw new ExpectationFailedException (
428431 'Error: you should test against a single element criteria when asserting that elementIsObject '
429432 );
430433 }
431-
432- \PHPUnit \Framework \Assert::assertEquals (1 , $ res , 'Specified element is not a Mongo Object ' );
434+ Assert::assertSame (1 , $ res , 'Specified element is not a Mongo Object ' );
433435 }
434436
435437 /**
@@ -440,21 +442,13 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
440442 * $I->seeNumElementsInCollection('users', 2);
441443 * $I->seeNumElementsInCollection('users', 1, ['name' => 'miles']);
442444 * ```
445+ *
446+ * @param array<string, mixed> $criteria
443447 */
444448 public function seeNumElementsInCollection (string $ collection , int $ expected , array $ criteria = []): void
445449 {
446450 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
447451 $ res = $ collection ->count ($ criteria );
448- \PHPUnit \Framework \Assert::assertSame ($ expected , $ res );
449- }
450-
451- /**
452- * Returns list of classes and corresponding packages required for this module
453- *
454- * @return array<string, string>
455- */
456- public function _requires ()
457- {
458- return ['MongoDB\Client ' => '"mongodb/mongodb": "^1.0" ' ];
452+ Assert::assertSame ($ expected , $ res );
459453 }
460454}
0 commit comments