Skip to content

Commit 5a2df25

Browse files
committed
Remove support for legacy mongoDb extension, added more typehints
1 parent 32f9e53 commit 5a2df25

File tree

3 files changed

+41
-264
lines changed

3 files changed

+41
-264
lines changed

src/Codeception/Lib/Driver/MongoDb.php

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@
77
use Codeception\Exception\ModuleConfigException;
88
use Codeception\Exception\ModuleException;
99
use Exception;
10-
use MongoClient;
11-
use MongoConnectionException;
1210
use MongoDB\Database;
13-
use MongoException;
1411

1512
class MongoDb
1613
{
1714
/**
1815
* @var int
1916
*/
2017
const DEFAULT_PORT = 27017;
21-
/**
22-
* @var bool
23-
*/
24-
private $isLegacy;
2518
/**
2619
* @var \Codeception\Lib\Driver\MongoDB|null
2720
*/
@@ -34,23 +27,18 @@ class MongoDb
3427
private $user;
3528
private $password;
3629
/**
37-
* @var \MongoDB\Client|MongoClient|null
30+
* @var \MongoDB\Client|null
3831
*/
3932
private $client;
4033
/**
4134
* @var string
4235
*/
4336
private $quiet = '';
4437

45-
public static function connect($dsn, $user, $password): void
46-
{
47-
throw new Exception(__CLASS__ . '::connect() - hm, it looked like this method had become obsolete...');
48-
}
49-
5038
/**
5139
* Connect to the Mongo server using the MongoDB extension.
5240
*/
53-
protected function setupMongoDB($dsn, $options): void
41+
protected function setupMongoDB(string $dsn, array $options): void
5442
{
5543
try {
5644
$this->client = new \MongoDB\Client($dsn, $options);
@@ -60,19 +48,6 @@ protected function setupMongoDB($dsn, $options): void
6048
}
6149
}
6250

63-
/**
64-
* Connect to the Mongo server using the legacy mongo extension.
65-
*/
66-
protected function setupMongo($dsn, $options): void
67-
{
68-
try {
69-
$this->client = new MongoClient($dsn, $options);
70-
$this->dbh = $this->client->selectDB($this->dbName);
71-
} catch (MongoConnectionException $e) {
72-
throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $e->getMessage()));
73-
}
74-
}
75-
7651
/**
7752
* Clean up the Mongo database using the MongoDB extension.
7853
*/
@@ -85,43 +60,16 @@ protected function cleanupMongoDB(): void
8560
}
8661
}
8762

88-
/**
89-
* Clean up the Mongo database using the legacy Mongo extension.
90-
*/
91-
protected function cleanupMongo(): void
92-
{
93-
try {
94-
$list = $this->dbh->listCollections();
95-
} catch (MongoException $e) {
96-
throw new Exception(sprintf('Failed to list collections of the DB: %s', $e->getMessage()), $e->getCode(), $e);
97-
}
98-
foreach ($list as $collection) {
99-
try {
100-
$collection->drop();
101-
} catch (MongoException $e) {
102-
throw new Exception(sprintf('Failed to drop collection: %s', $e->getMessage()), $e->getCode(), $e);
103-
}
104-
}
105-
}
106-
10763
/**
10864
* $dsn has to contain db_name after the host. E.g. "mongodb://localhost:27017/mongo_test_db"
10965
*
11066
* @static
11167
*
112-
* @param $dsn
113-
* @param $user
114-
* @param $password
115-
*
11668
* @throws ModuleConfigException
11769
* @throws Exception
11870
*/
119-
public function __construct($dsn, $user, $password)
71+
public function __construct(string $dsn, string $user, string $password)
12072
{
121-
$this->isLegacy = !extension_loaded('mongodb') &&
122-
class_exists('\\MongoClient') &&
123-
strpos(MongoClient::VERSION, 'mongofill') === false;
124-
12573
/* defining DB name */
12674
$this->dbName = preg_replace('#\?.*#', '', substr($dsn, strrpos($dsn, '/') + 1));
12775

@@ -148,35 +96,31 @@ class_exists('\\MongoClient') &&
14896
];
14997
}
15098

151-
$this->{$this->isLegacy ? 'setupMongo' : 'setupMongoDB'}($dsn, $options);
99+
$this->setupMongoDB($dsn, $options);
152100
$this->user = $user;
153101
$this->password = $password;
154102
}
155103

156104
/**
157105
* @static
158-
*
159-
* @param $dsn
160-
* @param $user
161-
* @param $password
162106
*/
163-
public static function create($dsn, $user, $password): \Codeception\Lib\Driver\MongoDb
107+
public static function create(string $dsn, string $user, string $password): \Codeception\Lib\Driver\MongoDb
164108
{
165109
return new MongoDb($dsn, $user, $password);
166110
}
167111

168112
public function cleanup(): void
169113
{
170-
$this->{$this->isLegacy ? 'cleanupMongo' : 'cleanupMongoDB'}();
114+
$this->cleanupMongoDB();
171115
}
172116

173117
/**
174118
* dump file has to be a javascript document where one can use all the mongo shell's commands
175119
* just FYI: this file can be easily created be RockMongo's export button
176120
*
177-
* @param $dumpFile
121+
* @param string $dumpFile
178122
*/
179-
public function load($dumpFile): void
123+
public function load(string $dumpFile): void
180124
{
181125
$cmd = sprintf(
182126
'mongo %s %s%s',
@@ -187,7 +131,7 @@ public function load($dumpFile): void
187131
shell_exec($cmd);
188132
}
189133

190-
public function loadFromMongoDump($dumpFile): void
134+
public function loadFromMongoDump(string $dumpFile): void
191135
{
192136
[$host, $port] = $this->getHostPort();
193137
$cmd = sprintf(
@@ -202,7 +146,7 @@ public function loadFromMongoDump($dumpFile): void
202146
shell_exec($cmd);
203147
}
204148

205-
public function loadFromTarGzMongoDump($dumpFile): void
149+
public function loadFromTarGzMongoDump(string $dumpFile): void
206150
{
207151
[$host, $port] = $this->getHostPort();
208152
$getDirCmd = sprintf(
@@ -251,9 +195,9 @@ public function getDbh()
251195
return $this->dbh;
252196
}
253197

254-
public function setDatabase($dbName): void
198+
public function setDatabase(string $dbName): void
255199
{
256-
$this->dbh = $this->client->{$this->isLegacy ? 'selectDB' : 'selectDatabase'}($dbName);
200+
$this->dbh = $this->client->selectDatabase($dbName);
257201
}
258202

259203
public function getDbHash()
@@ -267,14 +211,6 @@ public function getDbHash()
267211
return $result[0]->md5 ?? null;
268212
}
269213

270-
/**
271-
* Determine if this driver is using the legacy extension or not.
272-
*/
273-
public function isLegacy(): bool
274-
{
275-
return $this->isLegacy;
276-
}
277-
278214
/**
279215
* @return string[]|int[]
280216
*/
@@ -290,7 +226,7 @@ private function getHostPort(): array
290226
throw new ModuleException($this, '$dsn MUST be like (mongodb://)<host>:<port>/<db name>');
291227
}
292228

293-
public function setQuiet($quiet): void
229+
public function setQuiet(bool $quiet): void
294230
{
295231
$this->quiet = $quiet ? '--quiet' : '';
296232
}

src/Codeception/Module/MongoDb.php

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ class MongoDb extends Module implements RequiresPackage
9696
protected $config = [
9797
'populate' => true,
9898
'cleanup' => true,
99+
'dsn' => '',
99100
'dump' => null,
100101
'dump_type' => self::DUMP_TYPE_JS,
101-
'user' => null,
102-
'password' => null,
102+
'user' => '',
103+
'password' => '',
103104
'quiet' => false,
104105
];
105106

@@ -120,7 +121,6 @@ class MongoDb extends Module implements RequiresPackage
120121

121122
public function _initialize()
122123
{
123-
124124
try {
125125
$this->driver = MongoDbDriver::create(
126126
$this->config['dsn'],
@@ -280,37 +280,34 @@ protected function loadDump(): void
280280
/**
281281
* Specify the database to use
282282
*
283-
* ``` php
283+
* ```php
284284
* <?php
285285
* $I->useDatabase('db_1');
286286
* ```
287287
*
288-
* @param $dbName
288+
* @param string $dbName
289289
*/
290-
public function useDatabase($dbName): void
290+
public function useDatabase(string $dbName): void
291291
{
292292
$this->driver->setDatabase($dbName);
293293
}
294294

295295
/**
296296
* Inserts data into collection
297297
*
298-
* ``` php
298+
* ```php
299299
* <?php
300300
* $I->haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']);
301301
* $user_id = $I->haveInCollection('users', ['email' => 'john@coltrane.com']);
302302
* ```
303303
*
304-
* @param $collection
305-
* @return mixed|string
304+
* @param string $collection
305+
* @param array $data
306+
* @return string
306307
*/
307-
public function haveInCollection($collection, array $data)
308+
public function haveInCollection(string $collection, array $data): string
308309
{
309310
$collection = $this->driver->getDbh()->selectCollection($collection);
310-
if ($this->driver->isLegacy()) {
311-
$collection->insert($data);
312-
return $data['_id'];
313-
}
314311

315312
$response = $collection->insertOne($data);
316313
return (string) $response->getInsertedId();
@@ -319,14 +316,14 @@ public function haveInCollection($collection, array $data)
319316
/**
320317
* Checks if collection contains an item.
321318
*
322-
* ``` php
319+
* ```php
323320
* <?php
324321
* $I->seeInCollection('users', ['name' => 'miles']);
325322
* ```
326323
*
327-
* @param $collection
324+
* @param string $collection
328325
*/
329-
public function seeInCollection($collection, array $criteria = []): void
326+
public function seeInCollection(string $collection, array $criteria = []): void
330327
{
331328
$collection = $this->driver->getDbh()->selectCollection($collection);
332329
$res = $collection->count($criteria);
@@ -336,14 +333,14 @@ public function seeInCollection($collection, array $criteria = []): void
336333
/**
337334
* Checks if collection doesn't contain an item.
338335
*
339-
* ``` php
336+
* ```php
340337
* <?php
341338
* $I->dontSeeInCollection('users', ['name' => 'miles']);
342339
* ```
343340
*
344-
* @param $collection
341+
* @param string $collection
345342
*/
346-
public function dontSeeInCollection($collection, array $criteria = []): void
343+
public function dontSeeInCollection(string $collection, array $criteria = []): void
347344
{
348345
$collection = $this->driver->getDbh()->selectCollection($collection);
349346
$res = $collection->count($criteria);
@@ -353,15 +350,15 @@ public function dontSeeInCollection($collection, array $criteria = []): void
353350
/**
354351
* Grabs a data from collection
355352
*
356-
* ``` php
353+
* ```php
357354
* <?php
358355
* $user = $I->grabFromCollection('users', ['name' => 'miles']);
359356
* ```
360357
*
361-
* @param $collection
358+
* @param string $collection
362359
* @return \MongoDB\Model\BSONDocument|mixed
363360
*/
364-
public function grabFromCollection($collection, array $criteria = [])
361+
public function grabFromCollection(string $collection, array $criteria = [])
365362
{
366363
$collection = $this->driver->getDbh()->selectCollection($collection);
367364
return $collection->findOne($criteria);
@@ -370,16 +367,16 @@ public function grabFromCollection($collection, array $criteria = [])
370367
/**
371368
* Grabs the documents count from a collection
372369
*
373-
* ``` php
370+
* ```php
374371
* <?php
375372
* $count = $I->grabCollectionCount('users');
376373
* // or
377374
* $count = $I->grabCollectionCount('users', ['isAdmin' => true]);
378375
* ```
379376
*
380-
* @param $collection
377+
* @param string $collection
381378
*/
382-
public function grabCollectionCount($collection, array $criteria = []): int
379+
public function grabCollectionCount(string $collection, array $criteria = []): int
383380
{
384381
$collection = $this->driver->getDbh()->selectCollection($collection);
385382
return $collection->count($criteria);
@@ -388,7 +385,7 @@ public function grabCollectionCount($collection, array $criteria = []): int
388385
/**
389386
* Asserts that an element in a collection exists and is an Array
390387
*
391-
* ``` php
388+
* ```php
392389
* <?php
393390
* $I->seeElementIsArray('users', ['name' => 'John Doe'], 'data.skills');
394391
* ```
@@ -417,7 +414,7 @@ public function seeElementIsArray(string $collection, array $criteria = [], stri
417414
/**
418415
* Asserts that an element in a collection exists and is an Object
419416
*
420-
* ``` php
417+
* ```php
421418
* <?php
422419
* $I->seeElementIsObject('users', ['name' => 'John Doe'], 'data');
423420
* ```
@@ -446,15 +443,15 @@ public function seeElementIsObject(string $collection, array $criteria = [], str
446443
/**
447444
* Count number of records in a collection
448445
*
449-
* ``` php
446+
* ```php
450447
* <?php
451448
* $I->seeNumElementsInCollection('users', 2);
452449
* $I->seeNumElementsInCollection('users', 1, ['name' => 'miles']);
453450
* ```
454451
*
455-
* @param $collection
452+
* @param string $collection
456453
*/
457-
public function seeNumElementsInCollection($collection, int $expected, array $criteria = []): void
454+
public function seeNumElementsInCollection(string $collection, int $expected, array $criteria = []): void
458455
{
459456
$collection = $this->driver->getDbh()->selectCollection($collection);
460457
$res = $collection->count($criteria);

0 commit comments

Comments
 (0)