Skip to content

Commit fcc654d

Browse files
authored
Add cleanup: dirty config option (#1)
* Added `cleanup: dirty` config option. If cleanup is set to 'dirty', the dump is only reloaded if any data has been written to the db during a test. This is checked using the [dbHash](https://docs.mongodb.com/manual/reference/command/dbHash/) command. * Fix documentation of 'cleanup: dirty'. Update docblock above class definition.
1 parent 1dea74c commit fcc654d

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

documentation.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ HINT: This module can be used with [Mongofill](https://github.com/mongofill/mong
3838
default: MongoDb::DUMP_TYPE_JS).
3939
* dump - path to database dump
4040
* populate: true - should the dump be loaded before test suite is started.
41-
* cleanup: true - should the dump be reloaded after each test
41+
* cleanup: true - should the dump be reloaded after each test.
42+
Boolean or 'dirty'. If cleanup is set to 'dirty', the dump is only reloaded if any data has been written to the db during a test. This is
43+
checked using the [dbHash](https://docs.mongodb.com/manual/reference/command/dbHash/) command.
4244

4345

4446
## Actions

src/Codeception/Lib/Driver/MongoDb.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ public function setDatabase($dbName)
235235
$this->dbh = $this->client->{$this->legacy ? 'selectDB' : 'selectDatabase'}($dbName);
236236
}
237237

238+
public function getDbHash()
239+
{
240+
$result = $this->dbh->command(['dbHash' => 1]);
241+
242+
if (!is_array($result)) {
243+
$result = iterator_to_array($result);
244+
}
245+
246+
return isset($result[0]->md5) ? $result[0]->md5 : null;
247+
}
248+
238249
/**
239250
* Determine if this driver is using the legacy extension or not.
240251
*

src/Codeception/Module/MongoDb.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
* default: MongoDb::DUMP_TYPE_JS).
4848
* * dump - path to database dump
4949
* * populate: true - should the dump be loaded before test suite is started.
50-
* * cleanup: true - should the dump be reloaded after each test
50+
* * cleanup: true - should the dump be reloaded after each test.
51+
* Boolean or 'dirty'. If cleanup is set to 'dirty', the dump is only reloaded if any data has been written to the db during a test. This is
52+
* checked using the [dbHash](https://docs.mongodb.com/manual/reference/command/dbHash/) command.
5153
*
5254
*/
5355
class MongoDb extends CodeceptionModule implements RequiresPackage
@@ -69,6 +71,8 @@ class MongoDb extends CodeceptionModule implements RequiresPackage
6971
protected $dumpFile;
7072
protected $isDumpFileEmpty = true;
7173

74+
protected $dbHash;
75+
7276
protected $config = [
7377
'populate' => true,
7478
'cleanup' => true,
@@ -180,7 +184,7 @@ private function validateDump()
180184

181185
public function _before(TestInterface $test)
182186
{
183-
if ($this->config['cleanup'] && !$this->populated) {
187+
if ($this->shouldCleanup()) {
184188
$this->cleanup();
185189
$this->loadDump();
186190
}
@@ -191,6 +195,17 @@ public function _after(TestInterface $test)
191195
$this->populated = false;
192196
}
193197

198+
protected function shouldCleanup()
199+
{
200+
if ($this->populated) {
201+
return false;
202+
}
203+
204+
return $this->config['cleanup'] === 'dirty'
205+
? ($this->dbHash === null || $this->driver->getDbHash() !== $this->dbHash)
206+
: (bool)$this->config['cleanup'];
207+
}
208+
194209
protected function cleanup()
195210
{
196211
$dbh = $this->driver->getDbh();
@@ -230,6 +245,10 @@ protected function loadDump()
230245
} catch (\Exception $e) {
231246
throw new ModuleException(__CLASS__, $e->getMessage());
232247
}
248+
249+
if ($this->config['cleanup'] === 'dirty') {
250+
$this->dbHash = $this->driver->getDbHash();
251+
}
233252
}
234253

235254
/**

tests/unit/Codeception/Module/MongoDbTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ protected function _setUp()
3636
$this->markTestSkipped('MongoDB is not installed');
3737
}
3838

39+
$cleanupDirty = in_array('cleanup-dirty', $this->getGroups());
40+
$config = $this->mongoConfig + ['cleanup' => $cleanupDirty ? 'dirty' : true];
41+
3942
$mongo = new \MongoDB\Client();
4043

4144
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
4245
$this->module = new MongoDb($container);
43-
$this->module->_setConfig($this->mongoConfig);
46+
$this->module->_setConfig($config);
4447
try {
4548
$this->module->_initialize();
4649
} catch (ModuleException $e) {
@@ -49,7 +52,10 @@ protected function _setUp()
4952

5053
$this->db = $mongo->selectDatabase('test');
5154
$this->userCollection = $this->db->users;
52-
$this->userCollection->insertOne(array('id' => 1, 'email' => '[email protected]'));
55+
56+
if (!$cleanupDirty) {
57+
$this->userCollection->insertOne(array('id' => 1, 'email' => '[email protected]'));
58+
}
5359
}
5460

5561
protected function _tearDown()
@@ -168,4 +174,43 @@ public function testLoadDump()
168174
$this->module->haveInCollection('96_bulls', $testRecord);
169175
}
170176
}
177+
178+
/**
179+
* @group cleanup-dirty
180+
*/
181+
public function testCleanupDirty()
182+
{
183+
$test = $this->createMock('Codeception\TestInterface');
184+
$collection = $this->db->selectCollection('96_bulls');
185+
186+
$hash1 = $this->module->driver->getDbHash();
187+
$this->module->seeNumElementsInCollection('96_bulls', 7);
188+
$this->assertEquals($hash1, $this->module->driver->getDbHash());
189+
190+
$this->module->_after($test);
191+
192+
$this->module->_before($test); // No cleanup expected
193+
194+
$this->assertEquals($hash1, $this->module->driver->getDbHash());
195+
$collection->insertOne(array('name' => 'Coby White','position' => 'pg'));
196+
197+
$hashDirty = $this->module->driver->getDbHash();
198+
$this->assertNotEquals($hash1, $hashDirty);
199+
200+
$this->module->_after($test);
201+
202+
$this->module->_before($test); // Cleanup expected
203+
$this->module->seeNumElementsInCollection('96_bulls', 7);
204+
205+
$hash2 = $this->module->driver->getDbHash();
206+
207+
$this->assertNotEquals($hash1, $hash2);
208+
$this->assertNotEquals($hashDirty, $hash2);
209+
210+
$this->module->_after($test);
211+
212+
$this->module->_before($test); // No cleanup expected
213+
214+
$this->assertEquals($hash2, $this->module->driver->getDbHash());
215+
}
171216
}

0 commit comments

Comments
 (0)