From 0b6f58fa5007c7b84d970402fd6dc83e5eb4a0b0 Mon Sep 17 00:00:00 2001 From: Adam Roses Wight Date: Thu, 1 May 2014 10:31:30 -0700 Subject: [PATCH] Provide a RoR-like test fixture helper Any unit test may call $this->loadAllFixtures() from the setUp() routine, and that will load all fixtures from the civicrm/tests/fixtures directory. Fixtures can be written using the legacy XML format, or using YAML. --- tests/fixtures/case.yaml | 5 ++ tests/fixtures/case_contact.yaml | 4 + tests/fixtures/case_relationships.yaml | 44 ++++++++++ tests/fixtures/case_types.yaml | 9 +++ tests/fixtures/contacts.yaml | 90 +++++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 34 ++++++++ tests/phpunit/api/v3/CaseTest.php | 75 +---------------- 7 files changed, 188 insertions(+), 73 deletions(-) create mode 100644 tests/fixtures/case.yaml create mode 100644 tests/fixtures/case_contact.yaml create mode 100644 tests/fixtures/case_relationships.yaml create mode 100644 tests/fixtures/case_types.yaml create mode 100644 tests/fixtures/contacts.yaml diff --git a/tests/fixtures/case.yaml b/tests/fixtures/case.yaml new file mode 100644 index 0000000000..25e79471c4 --- /dev/null +++ b/tests/fixtures/case.yaml @@ -0,0 +1,5 @@ +civicrm_case: + - + id: 1 + case_type_id: 1 + status_id: 1 diff --git a/tests/fixtures/case_contact.yaml b/tests/fixtures/case_contact.yaml new file mode 100644 index 0000000000..17d8b90469 --- /dev/null +++ b/tests/fixtures/case_contact.yaml @@ -0,0 +1,4 @@ +civicrm_case_contact: + - + case_id: 1 + contact_id: 3 diff --git a/tests/fixtures/case_relationships.yaml b/tests/fixtures/case_relationships.yaml new file mode 100644 index 0000000000..96323d07bc --- /dev/null +++ b/tests/fixtures/case_relationships.yaml @@ -0,0 +1,44 @@ +civicrm_relationship_type: + - + name_a_b: Homeless Services Coordinator is + label_a_b: Homeless Services Coordinator is + name_b_a: Homeless Services Coordinator + label_b_a: Homeless Services Coordinator + description: Homeless Services Coordinator + contact_type_a: Individual + contact_type_b: Individual + is_reserved: 0 + is_active: 1 + + - + name_a_b: Health Services Coordinator is + label_a_b: Health Services Coordinator is + name_b_a: Health Services Coordinator + label_b_a: Health Services Coordinator + description: Health Services Coordinator + contact_type_a: Individual + contact_type_b: Individual + is_reserved: 0 + is_active: 1 + + - + name_a_b: Senior Services Coordinator is + label_a_b: Senior Services Coordinator is + name_b_a: Senior Services Coordinator + label_b_a: Senior Services Coordinator + description: Senior Services Coordinator + contact_type_a: Individual + contact_type_b: Individual + is_reserved: 0 + is_active: 1 + + - + name_a_b: Benefits Specialist is + label_a_b: Benefits Specialist is + name_b_a: Benefits Specialist + label_b_a: Benefits Specialist + description: Benefits Specialist + contact_type_a: Individual + contact_type_b: Individual + is_reserved: 0 + is_active: 1 diff --git a/tests/fixtures/case_types.yaml b/tests/fixtures/case_types.yaml new file mode 100644 index 0000000000..47c47a887b --- /dev/null +++ b/tests/fixtures/case_types.yaml @@ -0,0 +1,9 @@ +civicrm_case_type: + - + id: 1 + name: housing_support + title: Housing Support + description: Help homeless individuals obtain temporary and long-term housing + is_active: 1 + is_reserved: 0 + weight: 1 diff --git a/tests/fixtures/contacts.yaml b/tests/fixtures/contacts.yaml new file mode 100644 index 0000000000..905ab8e9e1 --- /dev/null +++ b/tests/fixtures/contacts.yaml @@ -0,0 +1,90 @@ +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: + is_opt_out: 0 + legal_identifier: + external_identifier: + sort_name: Site Administrator + display_name: Site Administrator + nick_name: + legal_name: + image_URL: + preferred_communication_method: + preferred_mail_format: + hash: + api_key: + source: + 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: + is_opt_out: 0 + legal_identifier: + external_identifier: + sort_name: + display_name: Test Contact + nick_name: + legal_name: + image_URL: + preferred_communication_method: + preferred_mail_format: + hash: + api_key: + source: + 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: diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index f48d4c401b..64680ad682 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -82,6 +82,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { */ static protected $_dbName; + /** + * Track tables we have modified during a test + */ + protected $_tablesToTruncate = array(); + /** * @var array of temporary directory names */ @@ -368,6 +373,33 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } } + /** + * Read everything from the datasets directory and insert into the db + */ + public function loadAllFixtures() { + $fixturesDir = __DIR__ . '/../../fixtures'; + + $this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 0;"); + + $xmlFiles = glob($fixturesDir . '/*.xml'); + foreach ($xmlFiles as $xmlFixture) { + $op = new PHPUnit_Extensions_Database_Operation_Insert(); + $dataset = new PHPUnit_Extensions_Database_DataSet_XMLDataSet($xmlFixture); + $this->_tablesToTruncate = array_merge($this->_tablesToTruncate, $dataset->getTableNames()); + $op->execute($this->_dbconn, $dataset); + } + + $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); + } + + $this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 1;"); + } + /** * emulate a logged in user since certain functions use that * value to store a record in the DB (like activity) @@ -2092,6 +2124,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $tablesToTruncate[] = 'civicrm_custom_field'; } + $tablesToTruncate = array_unique(array_merge($this->_tablesToTruncate, $tablesToTruncate)); + CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;"); foreach ($tablesToTruncate as $table) { $sql = "TRUNCATE TABLE $table"; diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index 8114c3b22f..e65953d41f 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -113,6 +113,8 @@ class api_v3_CaseTest extends CiviUnitTestCase { $this->quickCleanup($tablesToTruncate); + $this->loadAllFixtures(); + $activityTypes = $this->callAPISuccess('option_value', 'get', array( 'option_group_id' => 2, 'name' => 'Follow Up', @@ -120,79 +122,6 @@ class api_v3_CaseTest extends CiviUnitTestCase { 'sequential' => 1, )); $this->followup_activity_type_value = $activityTypes['values'][0]['value']; - // Insert a row in civicrm_contact creating contact 17 - $op = new PHPUnit_Extensions_Database_Operation_Insert(); - $op->execute($this->_dbconn, - new PHPUnit_Extensions_Database_DataSet_XMLDataSet( - dirname(__FILE__) . '/dataset/contact_17.xml' - ) - ); - - //Create relationship types - $relTypeParams = array( - 'name_a_b' => 'Case Coordinator is', - 'label_a_b' => 'Case Coordinator is', - 'name_b_a' => 'Case Coordinator', - 'label_b_a' => 'Case Coordinator', - 'description' => 'Case Coordinator', - 'contact_type_a' => 'Individual', - 'contact_type_b' => 'Individual', - 'is_reserved' => 0, - 'is_active' => 1, - ); - $this->relationshipTypeCreate($relTypeParams); - - $relTypeParams = array( - 'name_a_b' => 'Homeless Services Coordinator is', - 'label_a_b' => 'Homeless Services Coordinator is', - 'name_b_a' => 'Homeless Services Coordinator', - 'label_b_a' => 'Homeless Services Coordinator', - 'description' => 'Homeless Services Coordinator', - 'contact_type_a' => 'Individual', - 'contact_type_b' => 'Individual', - 'is_reserved' => 0, - 'is_active' => 1, - ); - $this->relationshipTypeCreate($relTypeParams); - - $relTypeParams = array( - 'name_a_b' => 'Health Services Coordinator is', - 'label_a_b' => 'Health Services Coordinator is', - 'name_b_a' => 'Health Services Coordinator', - 'label_b_a' => 'Health Services Coordinator', - 'description' => 'Health Services Coordinator', - 'contact_type_a' => 'Individual', - 'contact_type_b' => 'Individual', - 'is_reserved' => 0, - 'is_active' => 1, - ); - $this->relationshipTypeCreate($relTypeParams); - - $relTypeParams = array( - 'name_a_b' => 'Senior Services Coordinator is', - 'label_a_b' => 'Senior Services Coordinator is', - 'name_b_a' => 'Senior Services Coordinator', - 'label_b_a' => 'Senior Services Coordinator', - 'description' => 'Senior Services Coordinator', - 'contact_type_a' => 'Individual', - 'contact_type_b' => 'Individual', - 'is_reserved' => 0, - 'is_active' => 1, - ); - $this->relationshipTypeCreate($relTypeParams); - - $relTypeParams = array( - 'name_a_b' => 'Benefits Specialist is', - 'label_a_b' => 'Benefits Specialist is', - 'name_b_a' => 'Benefits Specialist', - 'label_b_a' => 'Benefits Specialist', - 'description' => 'Benefits Specialist', - 'contact_type_a' => 'Individual', - 'contact_type_b' => 'Individual', - 'is_reserved' => 0, - 'is_active' => 1, - ); - $this->relationshipTypeCreate($relTypeParams); // enable the default custom templates for the case type xml files $this->customDirectories(array('template_path' => TRUE)); -- 2.25.1