Skip to content

Commit

Permalink
[dbunit] remove dependency on discontinued dbunit
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 24, 2019
1 parent 1432a64 commit b986389
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 96 deletions.
60 changes: 10 additions & 50 deletions tests/fixtures/contacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ civicrm_contact:
id: 3
contact_type: Individual
contact_sub_type:
do_not_email:
do_not_phone:
do_not_mail:
do_not_sms:
do_not_trade:
do_not_email: 0
do_not_phone: 0
do_not_mail: 0
do_not_sms: 0
do_not_trade: 0
is_opt_out: 0
legal_identifier:
external_identifier:
sort_name: Site Administrator
display_name: Site Administrator
nick_name:
Expand All @@ -24,38 +23,18 @@ civicrm_contact:
first_name: Site
middle_name:
last_name: Administrator
prefix_id:
suffix_id:
email_greeting_id:
email_greeting_custom:
postal_greeting_id:
postal_greeting_custom:
addressee_id:
addressee_custom:
job_title:
gender_id:
birth_date:
is_deceased:
deceased_date:
household_name:
primary_contact_id:
organization_name:
sic_code:
user_unique_id:
employer_id:

-
id: 17
contact_type: Individual
contact_sub_type:
do_not_email:
do_not_phone:
do_not_mail:
do_not_sms:
do_not_trade:
do_not_email: 0
do_not_phone: 0
do_not_mail: 0
do_not_sms: 0
do_not_trade: 0
is_opt_out: 0
legal_identifier:
external_identifier:
sort_name:
display_name: Test Contact
nick_name:
Expand All @@ -69,22 +48,3 @@ civicrm_contact:
first_name: Test
middle_name:
last_name: Contact
prefix_id:
suffix_id:
email_greeting_id:
email_greeting_custom:
postal_greeting_id:
postal_greeting_custom:
addressee_id:
addressee_custom:
job_title:
gender_id:
birth_date:
is_deceased: 0
deceased_date:
household_name:
primary_contact_id:
organization_name:
sic_code:
user_unique_id:
employer_id:
70 changes: 24 additions & 46 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

use Civi\Payment\System;
use Symfony\Component\Yaml\Yaml;

/**
* Include class definitions
Expand All @@ -51,27 +52,14 @@
* Common functions for unit tests
* @package CiviCRM
*/
class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
class CiviUnitTestCase extends PHPUnit_Framework_TestCase {

use \Civi\Test\Api3DocTrait;
use \Civi\Test\GenericAssertionsTrait;
use \Civi\Test\DbTestTrait;
use \Civi\Test\ContactTestTrait;
use \Civi\Test\MailingTestTrait;

/**
* Database has been initialized.
*
* @var bool
*/
private static $dbInit = FALSE;

/**
* Database connection.
*
* @var PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
protected $_dbconn;

/**
* The database name.
Expand Down Expand Up @@ -236,29 +224,6 @@ public static function getDBName() {
return $dbName;
}

/**
* Create database connection for this instance.
*
* Initialize the test database if it hasn't been initialized
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection connection
*/
protected function getConnection() {
$dbName = self::$_dbName;
if (!self::$dbInit) {
$dbName = self::getDBName();

// install test database
echo PHP_EOL . "Installing {$dbName} database" . PHP_EOL;

static::_populateDB(FALSE, $this);

self::$dbInit = TRUE;
}

return $this->createDefaultDBConnection(Civi\Test::pdo(), $dbName);
}

/**
* Required implementation of abstract method.
*/
Expand Down Expand Up @@ -316,9 +281,6 @@ protected function setUp() {
exit(1);
}

// Get and save a connection to the database
$this->_dbconn = $this->getConnection();

// reload database before each test
// $this->_populateDB();

Expand Down Expand Up @@ -371,17 +333,33 @@ protected function setUp() {
public function loadAllFixtures() {
$fixturesDir = __DIR__ . '/../../fixtures';

$this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 0;");
CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");

$yamlFiles = glob($fixturesDir . '/*.yaml');
foreach ($yamlFiles as $yamlFixture) {
$op = new PHPUnit_Extensions_Database_Operation_Insert();
$dataset = new PHPUnit_Extensions_Database_DataSet_YamlDataSet($yamlFixture);
$this->_tablesToTruncate = array_merge($this->_tablesToTruncate, $dataset->getTableNames());
$op->execute($this->_dbconn, $dataset);
$yaml = Yaml::parse(file_get_contents($yamlFixture));
foreach ($yaml as $tableName => $vars) {
if ($tableName === 'civicrm_contact') {
CRM_Core_DAO::executeQuery('DELETE c FROM civicrm_contact c LEFT JOIN civicrm_domain d ON d.contact_id = c.id WHERE d.id IS NULL');
}
else {
CRM_Core_DAO::executeQuery("TRUNCATE $tableName");
}
foreach ($vars as $entity) {
$keys = $values = [];
foreach ($entity as $key => $value) {
$keys[] = $key;
$values[] = is_numeric($value) ? $value : "'{$value}'";
}
CRM_Core_DAO::executeQuery("
INSERT INTO $tableName (" . implode(',', $keys) . ') VALUES(' . implode(',', $values) . ')'
);
}

}
}

$this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 1;");
CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
}

/**
Expand Down

0 comments on commit b986389

Please sign in to comment.