Skip to content

Commit 7984606

Browse files
committed
Specified more types
1 parent c02add3 commit 7984606

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/Codeception/Lib/Driver/MongoDb.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Codeception\Exception\ModuleConfigException;
88
use Codeception\Exception\ModuleException;
99
use Exception;
10+
use MongoDB\Client;
1011
use MongoDB\Database;
12+
use MongoDB\Driver\Exception\Exception as MongoDbDriverException;
1113

1214
class MongoDb
1315
{
@@ -16,15 +18,12 @@ class MongoDb
1618
*/
1719
public const DEFAULT_PORT = 27017;
1820

19-
private ?Database $dbh;
20-
21-
private ?string $dbName = null;
21+
private ?Client $client;
22+
private Database $dbh;
23+
private ?string $dbName;
2224
private string $host;
2325
private string $user;
2426
private string $password;
25-
26-
private ?\MongoDB\Client $client = null;
27-
2827
private string $quiet = '';
2928

3029
/**
@@ -33,10 +32,10 @@ class MongoDb
3332
protected function setupMongoDB(string $dsn, array $options): void
3433
{
3534
try {
36-
$this->client = new \MongoDB\Client($dsn, $options);
35+
$this->client = new Client($dsn, $options);
3736
$this->dbh = $this->client->selectDatabase($this->dbName);
38-
} catch (\MongoDB\Driver\Exception $exception) {
39-
throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $exception->getMessage()));
37+
} catch (MongoDbDriverException $e) {
38+
throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $e->getMessage()));
4039
}
4140
}
4241

@@ -47,7 +46,7 @@ protected function cleanupMongoDB(): void
4746
{
4847
try {
4948
$this->dbh->drop();
50-
} catch (\MongoDB\Driver\Exception $e) {
49+
} catch (MongoDbDriverException $e) {
5150
throw new Exception(sprintf('Failed to drop the DB: %s', $e->getMessage()), $e->getCode(), $e);
5251
}
5352
}
@@ -70,7 +69,7 @@ public function __construct(string $dsn, string $user, string $password)
7069
}
7170

7271
/* defining host */
73-
if (strpos($dsn, 'mongodb://') !== false) {
72+
if (str_starts_with($dsn, 'mongodb://')) {
7473
$this->host = str_replace('mongodb://', '', preg_replace('#\?.*#', '', $dsn));
7574
} else {
7675
$this->host = $dsn;
@@ -177,10 +176,7 @@ private function createUserPasswordCmdString(): string
177176
return '';
178177
}
179178

180-
/**
181-
* @return \Codeception\Lib\Driver\MongoDb|\MongoDB\Database|null
182-
*/
183-
public function getDbh()
179+
public function getDbh(): Database
184180
{
185181
return $this->dbh;
186182
}
@@ -190,7 +186,7 @@ public function setDatabase(string $dbName): void
190186
$this->dbh = $this->client->selectDatabase($dbName);
191187
}
192188

193-
public function getDbHash()
189+
public function getDbHash(): ?string
194190
{
195191
$result = $this->dbh->command(['dbHash' => 1]);
196192

@@ -202,7 +198,7 @@ public function getDbHash()
202198
}
203199

204200
/**
205-
* @return string[]|int[]
201+
* @return array<string|int>
206202
*/
207203
private function getHostPort(): array
208204
{

src/Codeception/Module/MongoDb.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Codeception\TestInterface;
1313
use Exception;
1414
use 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

Comments
 (0)