From 023f9e8adef80fec6500080722809f69a46f7617 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 7 Jun 2019 12:27:32 +1200 Subject: [PATCH] [NFC] test cleanup. Uses CRM_Core_Exceptions, properly outputs unfiltered results The comment blocks should show all exceptions thrown by a function so trying to stdise a little. Also fixed failed output of unfiltered results when retrieval fails --- Civi/Test/Api3TestTrait.php | 16 ++++++++++------ Civi/Test/ContactTestTrait.php | 7 ++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Civi/Test/Api3TestTrait.php b/Civi/Test/Api3TestTrait.php index aeb4247275..9ae2c7d172 100644 --- a/Civi/Test/Api3TestTrait.php +++ b/Civi/Test/Api3TestTrait.php @@ -169,10 +169,13 @@ trait Api3TestTrait { * This function exists to wrap api getValue function & check the result * so we can ensure they succeed & throw exceptions without litterering the test with checks * There is a type check in this + * * @param string $entity * @param array $params - * @param null $count - * @throws \Exception + * @param int $count + * + * @throws \CRM_Core_Exception + * * @return array|int */ public function callAPISuccessGetCount($entity, $params, $count = NULL) { @@ -182,7 +185,7 @@ trait Api3TestTrait { ]; $result = $this->civicrm_api($entity, 'getcount', $params); if (!is_int($result) || !empty($result['is_error']) || isset($result['values'])) { - throw new \Exception('Invalid getcount result : ' . print_r($result, TRUE) . " type :" . gettype($result)); + throw new \CRM_Core_Exception('Invalid getcount result : ' . print_r($result, TRUE) . " type :" . gettype($result)); } if (is_int($count)) { $this->assertEquals($count, $result, "incorrect count returned from $entity getcount"); @@ -205,7 +208,8 @@ trait Api3TestTrait { * - array * - object * - * @throws \Exception + * @throws \CRM_Core_Exception + * * @return array|int */ public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) { @@ -214,8 +218,8 @@ trait Api3TestTrait { ]; $result = $this->civicrm_api($entity, 'getsingle', $params); if (!is_array($result) || !empty($result['is_error']) || isset($result['values'])) { - $unfilteredResult = $this->civicrm_api($entity, 'get', $params); - throw new \Exception( + $unfilteredResult = $this->civicrm_api($entity, 'get', ['version' => $this->_apiversion]); + throw new \CRM_Core_Exception( 'Invalid getsingle result' . print_r($result, TRUE) . "\n entity: $entity . \n params \n " . print_r($params, TRUE) . "\n entities retrieved with blank params \n" . print_r($unfilteredResult, TRUE) diff --git a/Civi/Test/ContactTestTrait.php b/Civi/Test/ContactTestTrait.php index c98e457b05..8fee27bc2d 100644 --- a/Civi/Test/ContactTestTrait.php +++ b/Civi/Test/ContactTestTrait.php @@ -71,7 +71,8 @@ trait ContactTestTrait { * * @return int * id of Individual created - * @throws \Exception + * + * @throws \CRM_Core_Exception */ public function individualCreate($params = array(), $seq = 0, $random = FALSE) { $params = array_merge($this->sampleContact('Individual', $seq, $random), $params); @@ -151,7 +152,7 @@ trait ContactTestTrait { * @param array $params * For civicrm_contact_add api function call. * - * @throws \Exception + * @throws CRM_Core_Exception * * @return int * id of Household created @@ -159,7 +160,7 @@ trait ContactTestTrait { private function _contactCreate($params) { $result = $this->callAPISuccess('contact', 'create', $params); if (!empty($result['is_error']) || empty($result['id'])) { - throw new \Exception('Could not create test contact, with message: ' . \CRM_Utils_Array::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array::value('trace', $result)); + throw new \CRM_Core_Exception('Could not create test contact, with message: ' . \CRM_Utils_Array::value('error_message', $result) . "\nBacktrace:" . \CRM_Utils_Array::value('trace', $result)); } return $result['id']; } -- 2.25.1