Skip to content

Commit

Permalink
Merge pull request #18 from redmatter/1.0_dump_populate_cleanup
Browse files Browse the repository at this point in the history
v1.0 - Adding dump populate cleanup options
  • Loading branch information
codemedic authored Jun 17, 2016
2 parents 120ef9c + aa08da0 commit e35cc63
Show file tree
Hide file tree
Showing 7 changed files with 679 additions and 23 deletions.
18 changes: 12 additions & 6 deletions examples/codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ modules:
Codeception\Extension\MultiDb:
connectors:
Primary:
dsn: "mysql:host=primary.db.example.com;port=3306;dbname=Demo1"
user: 'demo_primary'
password: ''
dsn: "mysql:host=primary.db.example.com;port=3306;dbname=DemoConfig"
user: 'demo'
password: 'notsecure'
dump: 'tests/_data/primary.sql'
populate: true
cleanup: true
Secondary:
dsn: "mysql:host=secondary.db.example.com;port=3306;dbname=Demo2"
user: 'demo_secondary'
password: ''
dsn: "mysql:host=secondary.db.example.com;port=3306;dbname=DemoWarehouse"
user: 'demo'
password: 'notsecure'
dump: 'tests/_data/secondary.sql'
populate: true
cleanup: true
1 change: 0 additions & 1 deletion examples/tests/_data/dump.sql

This file was deleted.

463 changes: 463 additions & 0 deletions examples/tests/_data/primary.sql

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions examples/tests/_data/secondary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
-- schema

--
-- Table structure for table `Audit`
--

CREATE TABLE IF NOT EXISTS `DemoWarehouse`.`Audit` (
`OrganisationID` int(10) unsigned NOT NULL,
`UserID` int(10) unsigned DEFAULT NULL,
`Time` datetime NOT NULL,
`JSON` text NOT NULL,

KEY `UserID` (`UserID`),
KEY `OrganisationID` (`OrganisationID`),
KEY `Time` (`Time`)
);

ALTER TABLE `Audit`
ADD FOREIGN KEY (`OrganisationID`) REFERENCES `DemoConfig`.`Organisation`(`ID`)
ON DELETE CASCADE ON UPDATE CASCADE;

-- data

-- `DemoWarehouse`.`Audit`
INSERT INTO `DemoWarehouse`.`Audit` VALUES
(1,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(2,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(3,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(4,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(5,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(6,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(7,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(8,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(9,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(10,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(11,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(12,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(13,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(14,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(15,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(16,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(17,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(18,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(19,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(20,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(21,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(22,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(23,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(24,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(25,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(26,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(27,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(28,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(29,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(30,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(31,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(32,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(33,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(34,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(35,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(36,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(37,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(38,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(39,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(40,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(41,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(42,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(43,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(44,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(45,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(46,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(47,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(48,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(49,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(50,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(51,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(52,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(53,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(54,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(55,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(56,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(57,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(58,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(59,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(60,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(61,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(62,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(63,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(64,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(65,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(66,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(67,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(68,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(69,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(70,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(71,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(72,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(73,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(74,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(75,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(76,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(77,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(78,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(79,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(80,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(81,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(82,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(83,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(84,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(85,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(86,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(87,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(88,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(89,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(90,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(91,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(92,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(93,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(94,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(95,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(96,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(97,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(98,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(99,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}'),
(100,NULL,'2016-06-17 12:35:59','{\"Description\": \"Organisation created\", \"OrderNumber\": 15371}');
10 changes: 0 additions & 10 deletions examples/tests/acceptance.suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,3 @@ modules:
config:
PhpBrowser:
url: 'http://localhost/myapp/'
Codeception\Extension\MultiDb:
connectors:
Primary:
dsn: "mysql:host=server1.example.com;port=3306;dbname=PrimaryDb"
user: 'dbuser'
password: ''
Secondary:
dsn: "mysql:host=server2.example.com;port=3306;dbname=SecondaryDb"
user: 'dbuser'
password: ''
20 changes: 14 additions & 6 deletions examples/tests/acceptance/DemoCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ public function tryToTest(AcceptanceTester $I)

$user_id = $I->transaction(function () use ($I) {
$I->executeSql('SELECT NOW()');
$user_id = $I->haveInDb('PrimaryDb.User', [
'UserName'=>'[email protected]',
$user_id = $I->haveInDb('DemoConfig.User', [
'OrganisationID'=>1,
'Name'=>'Some One',
'CreatedAt'=>'@asis NOW()'
'Email'=>'[email protected]',
'Address'=>'PO Box 1213, London, United Kingdom',
'Active'=>'YES',
]);
$I->haveInDb('PrimaryDb.UserPassword', [
$I->haveInDb('DemoConfig.UserPassword', [
'UserID'=>$user_id,
'Password'=>'topsecret',
'CreatedAt'=>'@asis NOW()'
'Hash'=>'@asis UNHEX(SHA1("topsecret"))',
'CreatedAt'=>'@asis NOW()',
'ExpiresAt'=>'@asis DATE_ADD(CreatedAt, INTERVAL 10 DAY)'
]);

return $user_id;
});

$I->amConnectedToDb('Secondary');
codecept_debug(
$I->getFromDb('DemoWarehouse.Audit', ['OrganisationID'=>1])
);
}
}
65 changes: 65 additions & 0 deletions src/Codeception/Extension/MultiDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Codeception\Exception\ModuleConfig as ModuleConfigException;
use Codeception\Module;
use Codeception\TestCase;
use Codeception\Configuration;

/**
* MultiDb - Module that allows tests to perform setup queries and assertions across multiple databases.
Expand Down Expand Up @@ -85,6 +86,70 @@ public function _initialize()
$this->timezone = $this->config['timezone'];

parent::_initialize();

foreach ($this->config['connectors'] as $connector => $connectorConfig) {
if ($connectorConfig['populate']) {
if ($connectorConfig['cleanup']) {
$this->cleanup($connector);
}
$this->loadDump($connector);
}
}
}

/**
* Load SQL dump for a connector
*
* @param string $connector
*
* @throws ModuleConfigException
* @throws ModuleException
*/
protected function loadDump($connector)
{
$config = $this->config['connectors'][$connector];

if ($config['dump'] && ($config['cleanup'] || $config['populate'])) {
if (!file_exists(Configuration::projectDir() . $config['dump'])) {
throw new ModuleConfigException(
__CLASS__,
"\n{$connector} - Dump file doesn't exist. Please check path: {$config['dump']}"
);
}
$sql = file_get_contents(Configuration::projectDir() . $config['dump']);
// remove any comments of the form /* ... */
$sql = preg_replace('%/\*(?!!\d+)(?:(?!\*/).)*\*/%s', '', $sql);
if ($sql) {
$sql = explode("\n", $sql);
}

try {
$this->debugSection(__CLASS__, "{$connector} - Loading dump from {$config['dump']}");
$this->getDriver($connector)->load($sql);
} catch (\PDOException $e) {
throw new ModuleException(
__CLASS__,
$e->getMessage() . "\nSQL query being executed: " . $sql
);
}
}
}

/**
* Cleanup databases
*
* @param $connector
*
* @throws ModuleException
*/
protected function cleanup($connector)
{
try {
$this->debugSection(__CLASS__, "$connector - Cleaning up");
$this->getDriver($connector)->cleanup();
} catch (\Exception $e) {
throw new ModuleException(__CLASS__, $e->getMessage());
}
}

// HOOK: before scenario
Expand Down

0 comments on commit e35cc63

Please sign in to comment.