Skip to content

Commit f9d3ed6

Browse files
authored
Merge pull request #12 from Codeception/codecept5
Support Codeception 5
2 parents a55da49 + 7984606 commit f9d3ed6

File tree

6 files changed

+67
-74
lines changed

6 files changed

+67
-74
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
php: [7.4, 8.0]
16+
php: [8.0, 8.1]
1717

1818
steps:
1919
- name: Checkout code
@@ -34,7 +34,6 @@ jobs:
3434

3535
- name: Install dependencies
3636
run: |
37-
composer require mongodb/mongodb --no-update
3837
composer install --prefer-dist --no-progress --no-suggest
3938
4039
- name: Run test suite

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
],
1313
"minimum-stability": "RC",
1414
"require": {
15-
"php": "^7.4 || ^8.0",
16-
"codeception/codeception": "^4.0"
15+
"php": "^8.0",
16+
"codeception/codeception": "^5.0",
17+
"mongodb/mongodb": "^1.12"
1718
},
1819
"autoload": {
1920
"classmap": [
@@ -22,5 +23,8 @@
2223
},
2324
"config": {
2425
"classmap-authoritative": true
26+
},
27+
"require-dev": {
28+
"codeception/stub": "^4.0"
2529
}
2630
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ composer require "codeception/module-mongodb" --dev
1515

1616
## Requirements
1717

18-
* `PHP 7.4` or higher.
18+
* `PHP 8.0` or higher.
1919

2020
## Documentation
2121

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: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Codeception\Module;
66

7-
use Codeception\Lib\Interfaces\RequiresPackage;
87
use Codeception\Module;
98
use Codeception\Configuration;
109
use Codeception\Exception\ModuleConfigException;
@@ -13,6 +12,8 @@
1312
use Codeception\TestInterface;
1413
use Exception;
1514
use MongoConnectionException;
15+
use PHPUnit\Framework\Assert;
16+
use PHPUnit\Framework\ExpectationFailedException;
1617

1718
/**
1819
* Works with MongoDb database.
@@ -57,7 +58,7 @@
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

Comments
 (0)