From 08d620bcbcf3057d2535cde42675239f3a28d24b Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 24 May 2019 12:00:25 +1200 Subject: [PATCH] [dbunit] remove dbunit dependency from MembershipRenewalTest --- .../CRM/Member/Form/MembershipRenewalTest.php | 22 ++++------- tests/phpunit/CiviTest/CiviUnitTestCase.php | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php index 45935a2df9..d05442136c 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php @@ -36,10 +36,8 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { protected $_individualId; protected $_contribution; protected $_financialTypeId = 1; - protected $_apiversion; protected $_entity = 'Membership'; protected $_params; - protected $_ids = array(); protected $_paymentProcessorID; /** @@ -54,7 +52,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { * * @var array */ - protected $_processorParams = array(); + protected $_processorParams = []; /** * ID of created membership. @@ -68,7 +66,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { * * @var array */ - protected $paymentInstruments = array(); + protected $paymentInstruments = []; /** @@ -83,18 +81,12 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { * and redirect stdin to a temporary file. */ public function setUp() { - $this->_apiversion = 3; parent::setUp(); $this->_individualId = $this->individualCreate(); $this->_paymentProcessorID = $this->processorCreate(); - // Insert test data. - $op = new PHPUnit_Extensions_Database_Operation_Insert(); - $op->execute($this->_dbconn, - $this->createFlatXMLDataSet( - dirname(__FILE__) . '/dataset/data.xml' - ) - ); + + $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/data.xml'); $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array( 'domain_id' => 1, 'name' => "AnnualFixed", @@ -114,7 +106,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { )); $this->_membershipID = $membership['id']; - $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); + $instruments = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']); $this->paymentInstruments = $instruments['values']; } @@ -144,7 +136,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { public function testSubmit() { $form = $this->getForm(); $this->createLoggedInUser(); - $params = array( + $params = [ 'cid' => $this->_individualId, 'join_date' => date('m/d/Y', time()), 'start_date' => '', @@ -178,7 +170,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { 'billing_state_province_id-5' => '1003', 'billing_postal_code-5' => '90210', 'billing_country_id-5' => '1228', - ); + ]; $form->_contactID = $this->_individualId; $form->testSubmit($params); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 73b9f178f6..257ce52f96 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -384,6 +384,44 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 1;"); } + /** + * Load the data that used to be handled by the discontinued dbunit class. + * + * This could do with further tidy up - the initial priority is simply to get rid of + * the dbunity package which is no longer supported. + * + * @param string $fileName + */ + protected function loadXMLDataSet($fileName) { + CRM_Core_DAO::executeQuery('SET FOREIGN_KEY_CHECKS = 0'); + $xml = json_decode(json_encode(simplexml_load_file($fileName)), TRUE); + foreach ($xml as $tableName => $element) { + if (!empty($element)) { + foreach ($element as $row) { + $keys = $values = []; + if (isset($row['@attributes'])) { + foreach ($row['@attributes'] as $key => $value) { + $keys[] = $key; + $values[] = is_numeric($value) ? $value : "'{$value}'"; + } + } + else { + // cos we copied it & it is inconsistent.... + foreach ($row as $key => $value) { + $keys[] = $key; + $values[] = is_numeric($value) ? $value : "'{$value}'"; + } + } + + CRM_Core_DAO::executeQuery(" + INSERT INTO $tableName (" . implode(',', $keys) . ') VALUES(' . implode(',', $values) . ')' + ); + } + } + } + CRM_Core_DAO::executeQuery('SET FOREIGN_KEY_CHECKS = 1'); + } + /** * Create default domain contacts for the two domains added during test class. * database population. -- 2.25.1