77use Codeception \Exception \ModuleConfigException ;
88use Codeception \Exception \ModuleException ;
99use Exception ;
10- use MongoClient ;
11- use MongoConnectionException ;
1210use MongoDB \Database ;
13- use MongoException ;
1411
1512class 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 }
0 commit comments