From 54bd1003164f3290be252531ad1d1a49a139d5a9 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 13 Jun 2013 11:00:18 +1200 Subject: [PATCH] add wrappers for calling api & checking success or failure in call --- tests/phpunit/CiviTest/CiviUnitTestCase.php | 39 +++++++++++++++++++-- tests/phpunit/api/v3/UFFieldTest.php | 6 ++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 624b3125af..08d772602f 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -624,18 +624,51 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return $this->assertInternalType($expected, $actual, $message); } /** - * This function exists to wrap api functions used to set up tests but not part of the test + + /** + * This function exists to wrap api functions * so we can ensure they succeed & throw exceptions without litterering the test with checks * @param string $entity * @param string $action * @param array $params + * @param string $function - pass this in to create a generated example + * @param string $file - pass this in to create a generated example */ - function apiSetup($entity, $action, $params){ + function callAPISuccess($entity, $action, $params){ if(!isset($params['version'])){ $params['version'] = API_LATEST_VERSION; } $result = civicrm_api($entity, $action, $params); - $this->assertAPISuccess($result, "Failure in test setup for $entity $action"); + $this->assertAPISuccess($result, "Failure in api call for $entity $action"); + return $result; + } + + /** + * This function exists to wrap api functions + * so we can ensure they succeed, generate and example & throw exceptions without litterering the test with checks + * + * @param string $entity + * @param string $action + * @param array $params + * @param string $function - pass this in to create a generated example + * @param string $file - pass this in to create a generated example + */ + function callApiWithSuccessAndDocument($entity, $action, $params){ + $result = $this->callAPISuccess($entity, $action, $params); + $this->documentMe($params, $result, $function, $file); + return $result; + } + + /** + * This function exists to wrap api functions + * so we can ensure they fail where expected & throw exceptions without litterering the test with checks + * @param string $entity + * @param string $action + * @param array $params + */ + function callAPIFailure($entity, $action, $params){ + $result = civicrm_api($entity, $action, $params); + $this->assertAPIFailure($result, "We expected a failure for $entity $action but got a success"); return $result; } /** diff --git a/tests/phpunit/api/v3/UFFieldTest.php b/tests/phpunit/api/v3/UFFieldTest.php index 814d73151d..eaea357d13 100644 --- a/tests/phpunit/api/v3/UFFieldTest.php +++ b/tests/phpunit/api/v3/UFFieldTest.php @@ -174,11 +174,9 @@ class api_v3_UFFieldTest extends CiviUnitTestCase { public function testGetUFFieldSuccess() { - $this->apiSetup($this->_entity, 'create', $this->_params); + $this->callAPISuccess($this->_entity, 'create', $this->_params); $params = array('version' => 3); - $result = civicrm_api($this->_entity, 'get', $params); - $this->documentMe($params, $result, __FUNCTION__, __FILE__); - $this->assertAPISuccess($result, 'in line' . __LINE__); + $result = $this->callApiWithSuccessAndDocument($this->_entity, 'get', $params); $this->getAndCheck($this->_params, $result['id'], $this->_entity); } -- 2.25.1