1212use Codeception \TestInterface ;
1313use Exception ;
1414use MongoConnectionException ;
15+ use PHPUnit \Framework \Assert ;
16+ use PHPUnit \Framework \ExpectationFailedException ;
1517
1618/**
1719 * Works with MongoDb database.
@@ -73,20 +75,11 @@ class MongoDb extends Module
7375 */
7476 public const DUMP_TYPE_MONGODUMP_TAR_GZ = 'mongodump-tar-gz ' ;
7577
76- /**
77- * @api
78- * @var
79- */
80- public $ dbh ;
81-
82- protected ?string $ dumpFile = null ;
78+ protected string $ dumpFile ;
8379
8480 protected bool $ isDumpFileEmpty = true ;
8581
86- /**
87- * @var mixed|null
88- */
89- protected $ dbHash ;
82+ protected ?string $ dbHash ;
9083
9184 /**
9285 * @var array<string, mixed>
@@ -111,7 +104,7 @@ class MongoDb extends Module
111104 */
112105 protected array $ requiredFields = ['dsn ' ];
113106
114- public function _initialize ()
107+ public function _initialize (): void
115108 {
116109 try {
117110 $ this ->driver = MongoDbDriver::create (
@@ -207,15 +200,15 @@ private function validateDump(): void
207200 }
208201 }
209202
210- public function _before (TestInterface $ test )
203+ public function _before (TestInterface $ test ): void
211204 {
212205 if ($ this ->shouldCleanup ()) {
213206 $ this ->cleanup ();
214207 $ this ->loadDump ();
215208 }
216209 }
217210
218- public function _after (TestInterface $ test )
211+ public function _after (TestInterface $ test ): void
219212 {
220213 $ this ->populated = false ;
221214 }
@@ -300,6 +293,8 @@ public function useDatabase(string $dbName): void
300293 * $I->haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']);
301294 * $user_id = $I->haveInCollection('users', ['email' => 'john@coltrane.com']);
302295 * ```
296+ *
297+ * @param array<string, mixed> $data
303298 */
304299 public function haveInCollection (string $ collection , array $ data ): string
305300 {
@@ -316,12 +311,14 @@ public function haveInCollection(string $collection, array $data): string
316311 * <?php
317312 * $I->seeInCollection('users', ['name' => 'miles']);
318313 * ```
314+ *
315+ * @param array<string, mixed> $criteria
319316 */
320317 public function seeInCollection (string $ collection , array $ criteria = []): void
321318 {
322319 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
323320 $ res = $ collection ->count ($ criteria );
324- \ PHPUnit \ Framework \ Assert::assertGreaterThan (0 , $ res );
321+ Assert::assertGreaterThan (0 , $ res );
325322 }
326323
327324 /**
@@ -331,12 +328,14 @@ public function seeInCollection(string $collection, array $criteria = []): void
331328 * <?php
332329 * $I->dontSeeInCollection('users', ['name' => 'miles']);
333330 * ```
331+ *
332+ * @param array<string, mixed> $criteria
334333 */
335334 public function dontSeeInCollection (string $ collection , array $ criteria = []): void
336335 {
337336 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
338337 $ res = $ collection ->count ($ criteria );
339- \ PHPUnit \ Framework \ Assert::assertLessThan (1 , $ res );
338+ Assert::assertLessThan (1 , $ res );
340339 }
341340
342341 /**
@@ -347,9 +346,9 @@ public function dontSeeInCollection(string $collection, array $criteria = []): v
347346 * $user = $I->grabFromCollection('users', ['name' => 'miles']);
348347 * ```
349348 *
350- * @return \MongoDB\Model\BSONDocument| mixed
349+ * @param array<string, mixed> $criteria
351350 */
352- public function grabFromCollection (string $ collection , array $ criteria = [])
351+ public function grabFromCollection (string $ collection , array $ criteria = []): array | object | null
353352 {
354353 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
355354 return $ collection ->findOne ($ criteria );
@@ -364,6 +363,8 @@ public function grabFromCollection(string $collection, array $criteria = [])
364363 * // or
365364 * $count = $I->grabCollectionCount('users', ['isAdmin' => true]);
366365 * ```
366+ *
367+ * @param array<string, mixed> $criteria
367368 */
368369 public function grabCollectionCount (string $ collection , array $ criteria = []): int
369370 {
@@ -378,8 +379,10 @@ public function grabCollectionCount(string $collection, array $criteria = []): i
378379 * <?php
379380 * $I->seeElementIsArray('users', ['name' => 'John Doe'], 'data.skills');
380381 * ```
382+ *
383+ * @param array<string, mixed> $criteria
381384 */
382- public function seeElementIsArray (string $ collection , array $ criteria = [] , string $ elementToCheck = null ): void
385+ public function seeElementIsArray (string $ collection , array $ criteria , string $ elementToCheck ): void
383386 {
384387 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
385388
@@ -393,11 +396,11 @@ public function seeElementIsArray(string $collection, array $criteria = [], stri
393396 )
394397 );
395398 if ($ res > 1 ) {
396- throw new \ PHPUnit \ Framework \ ExpectationFailedException (
399+ throw new ExpectationFailedException (
397400 'Error: you should test against a single element criteria when asserting that elementIsArray '
398401 );
399402 }
400- \ PHPUnit \ Framework \ Assert::assertSame (1 , $ res , 'Specified element is not a Mongo Object ' );
403+ Assert::assertSame (1 , $ res , 'Specified element is not a Mongo Object ' );
401404 }
402405
403406 /**
@@ -407,8 +410,10 @@ public function seeElementIsArray(string $collection, array $criteria = [], stri
407410 * <?php
408411 * $I->seeElementIsObject('users', ['name' => 'John Doe'], 'data');
409412 * ```
413+ *
414+ * @param array<string, mixed> $criteria
410415 */
411- public function seeElementIsObject (string $ collection , array $ criteria = [] , string $ elementToCheck = null ): void
416+ public function seeElementIsObject (string $ collection , array $ criteria , string $ elementToCheck ): void
412417 {
413418 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
414419
@@ -422,11 +427,11 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
422427 )
423428 );
424429 if ($ res > 1 ) {
425- throw new \ PHPUnit \ Framework \ ExpectationFailedException (
430+ throw new ExpectationFailedException (
426431 'Error: you should test against a single element criteria when asserting that elementIsObject '
427432 );
428433 }
429- \ PHPUnit \ Framework \ Assert::assertSame (1 , $ res , 'Specified element is not a Mongo Object ' );
434+ Assert::assertSame (1 , $ res , 'Specified element is not a Mongo Object ' );
430435 }
431436
432437 /**
@@ -437,11 +442,13 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
437442 * $I->seeNumElementsInCollection('users', 2);
438443 * $I->seeNumElementsInCollection('users', 1, ['name' => 'miles']);
439444 * ```
445+ *
446+ * @param array<string, mixed> $criteria
440447 */
441448 public function seeNumElementsInCollection (string $ collection , int $ expected , array $ criteria = []): void
442449 {
443450 $ collection = $ this ->driver ->getDbh ()->selectCollection ($ collection );
444451 $ res = $ collection ->count ($ criteria );
445- \ PHPUnit \ Framework \ Assert::assertSame ($ expected , $ res );
452+ Assert::assertSame ($ expected , $ res );
446453 }
447454}
0 commit comments