From fbda92d3cc4bedadcf9f82400a8b48c439249639 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 22 Jun 2013 10:24:51 +1200 Subject: [PATCH] tidy up on survey test to make it a good template --- api/v3/examples/Survey/ChainedGetDelete.php | 25 ++++--- api/v3/examples/SurveyCreate.php | 25 ++++--- api/v3/examples/SurveyDelete.php | 10 ++- api/v3/examples/SurveyGet.php | 23 +++--- tests/phpunit/CiviTest/CiviUnitTestCase.php | 62 ++++++++++++++-- tests/phpunit/api/v3/SurveyTest.php | 80 +++++++++++++-------- 6 files changed, 160 insertions(+), 65 deletions(-) diff --git a/api/v3/examples/Survey/ChainedGetDelete.php b/api/v3/examples/Survey/ChainedGetDelete.php index da6c658e5e..1d14272881 100644 --- a/api/v3/examples/Survey/ChainedGetDelete.php +++ b/api/v3/examples/Survey/ChainedGetDelete.php @@ -4,10 +4,11 @@ demonstrates get + delete in the same call */ function survey_get_example(){ -$params = array( - 'version' => 3, +$params = array( 'title' => 'survey title', 'api.survey.delete' => 1, + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'survey','get',$params ); @@ -20,23 +21,27 @@ $params = array( */ function survey_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, + 'undefined_fields' => array( + '0' => 'title', + '1' => 'api.survey.delete', + ), 'version' => 3, 'count' => 1, - 'id' => 2, - 'values' => array( - '2' => array( - 'id' => '2', + 'id' => 1, + 'values' => array( + '1' => array( + 'id' => '1', 'title' => 'survey title', - 'activity_type_id' => '30', + 'activity_type_id' => '35', 'instructions' => 'Call people, ask for money', 'max_number_of_contacts' => '12', 'is_active' => '1', 'is_default' => 0, - 'created_date' => '2013-02-04 22:43:26', + 'created_date' => '20120130621222105', 'bypass_confirm' => 0, - 'api.survey.delete' => array( + 'api.survey.delete' => array( 'is_error' => 0, 'version' => 3, 'count' => 1, diff --git a/api/v3/examples/SurveyCreate.php b/api/v3/examples/SurveyCreate.php index 9902d0c540..03f140f4e9 100644 --- a/api/v3/examples/SurveyCreate.php +++ b/api/v3/examples/SurveyCreate.php @@ -4,12 +4,13 @@ */ function survey_create_example(){ -$params = array( - 'version' => 3, +$params = array( 'title' => 'survey title', - 'activity_type_id' => '30', + 'activity_type_id' => '35', 'max_number_of_contacts' => 12, 'instructions' => 'Call people, ask for money', + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'survey','create',$params ); @@ -22,17 +23,25 @@ $params = array( */ function survey_create_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, + 'undefined_fields' => array( + '0' => 'title', + '1' => 'activity_type_id', + '2' => 'max_number_of_contacts', + '3' => 'instructions', + '4' => 'created_id', + '5' => 'created_date', + ), 'version' => 3, 'count' => 1, 'id' => 1, - 'values' => array( - '1' => array( + 'values' => array( + '1' => array( 'id' => '1', 'title' => 'survey title', 'campaign_id' => '', - 'activity_type_id' => '30', + 'activity_type_id' => '35', 'recontact_interval' => '', 'instructions' => 'Call people, ask for money', 'release_frequency' => '', @@ -41,7 +50,7 @@ function survey_create_expectedresult(){ 'is_active' => '', 'is_default' => '', 'created_id' => '', - 'created_date' => '20130204224326', + 'created_date' => '20120130621222105', 'last_modified_id' => '', 'last_modified_date' => '', 'result_id' => '', diff --git a/api/v3/examples/SurveyDelete.php b/api/v3/examples/SurveyDelete.php index 1fdc509be0..04ace82588 100644 --- a/api/v3/examples/SurveyDelete.php +++ b/api/v3/examples/SurveyDelete.php @@ -4,12 +4,10 @@ */ function survey_delete_example(){ -$params = array( +$params = array( + 'id' => 1, 'version' => 3, - 'title' => 'survey title', - 'activity_type_id' => '30', - 'max_number_of_contacts' => 12, - 'instructions' => 'Call people, ask for money', + 'debug' => 0, ); $result = civicrm_api( 'survey','delete',$params ); @@ -22,7 +20,7 @@ $params = array( */ function survey_delete_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 1, diff --git a/api/v3/examples/SurveyGet.php b/api/v3/examples/SurveyGet.php index d052cfd534..0c3cfe919c 100644 --- a/api/v3/examples/SurveyGet.php +++ b/api/v3/examples/SurveyGet.php @@ -4,12 +4,13 @@ */ function survey_get_example(){ -$params = array( - 'version' => 3, +$params = array( 'title' => 'survey title', - 'activity_type_id' => '30', + 'activity_type_id' => '35', 'max_number_of_contacts' => 12, 'instructions' => 'Call people, ask for money', + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'survey','get',$params ); @@ -22,21 +23,27 @@ $params = array( */ function survey_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, + 'undefined_fields' => array( + '0' => 'title', + '1' => 'activity_type_id', + '2' => 'max_number_of_contacts', + '3' => 'instructions', + ), 'version' => 3, 'count' => 1, 'id' => 1, - 'values' => array( - '1' => array( + 'values' => array( + '1' => array( 'id' => '1', 'title' => 'survey title', - 'activity_type_id' => '30', + 'activity_type_id' => '35', 'instructions' => 'Call people, ask for money', 'max_number_of_contacts' => '12', 'is_active' => '1', 'is_default' => 0, - 'created_date' => '2013-02-04 22:43:26', + 'created_date' => '20120130621222105', 'bypass_confirm' => 0, ), ), diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 375b75e430..b87a9d3dc2 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -635,15 +635,48 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param string $file - pass this in to create a generated example */ function callAPISuccess($entity, $action, $params) { - $params += array( - 'version' => API_LATEST_VERSION, - 'debug' => 1, + $params = array_merge(array( + 'version' => API_LATEST_VERSION, + 'debug' => 1, + ), + $params ); $result = civicrm_api($entity, $action, $params); $this->assertAPISuccess($result, "Failure in api call for $entity $action"); return $result; } + /** + * 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 string $type - per http://php.net/manual/en/function.gettype.php possible types + * - boolean + * - integer + * - double + * - string + * - array + * - object + */ + function callAPISuccessGetValue($entity, $params, $type = NULL) { + $params += array( + 'version' => API_LATEST_VERSION, + 'debug' => 1, + ); + $result = civicrm_api($entity, 'getvalue', $params); + if($type){ + if($type == 'integer'){ + // api seems to return integers as strings + $this->assertTrue(is_numeric($result), "expected a numeric value but got " . print_r($result, 1)); + } + else{ + $this->assertType($type, $result, "returned result should have been of type $type but was " ); + } + } + 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 @@ -655,6 +688,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param string $file - pass this in to create a generated example */ function callAPIAndDocument($entity, $action, $params, $function, $file, $description = "", $subfile = NULL, $actionName = NULL){ + $params['version'] = API_LATEST_VERSION; + if(!isset($params['debug'])){ + // don't debug by default to keep examples tidy + $params['debug'] = 0; + } $result = $this->callAPISuccess($entity, $action, $params); $this->documentMe($params, $result, $function, $file, $description, $subfile, $actionName); return $result; @@ -678,6 +716,18 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return $result; } + /** + * Create required data based on $this->entity & $this->params + * This is just a way to set up the test data for delete & get functions + * so the distinction between set + * up & tested functions is clearer + * + * @return array api Result + */ + public function createTestEntity(){ + return $entity = $this->callAPISuccess($this->entity, 'create', $this->params); + } + /** * Generic function to create Organisation, to be used in test cases * @@ -2040,7 +2090,9 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $fieldsToChange = array( 'hash' => '67eac7789eaee00', 'modified_date' => '2012-11-14 16:02:35', + 'created_date' => '20120130621222105', ); + //swap out keys that change too often foreach ($fieldsToChange as $changeKey => $changeValue) { if (isset($result['values']) && is_array($result['values'])) { @@ -2219,13 +2271,13 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) $result = civicrm_api($entity, 'GetSingle', array( 'id' => $id, - 'version' => $this->_apiversion, + 'version' => API_LATEST_VERSION, )); if ($delete) { civicrm_api($entity, 'Delete', array( 'id' => $id, - 'version' => $this->_apiversion, + 'version' => API_LATEST_VERSION, )); } $dateFields = $keys = array(); diff --git a/tests/phpunit/api/v3/SurveyTest.php b/tests/phpunit/api/v3/SurveyTest.php index 145480bade..f191b3eb59 100644 --- a/tests/phpunit/api/v3/SurveyTest.php +++ b/tests/phpunit/api/v3/SurveyTest.php @@ -36,18 +36,27 @@ require_once 'CiviTest/CiviUnitTestCase.php'; */ require_once 'CiviTest/CiviUnitTestCase.php'; + +/** + * All API should contain at minimum a success test for each + * function - in this case - create, get & delete + * In addition any extra functionality should be tested & documented + * + * Failure tests should be added for specific api behaviours but note that + * many generic patterns are tested in the syntax conformance test + * + * @author eileen + * + */ class api_v3_SurveyTest extends CiviUnitTestCase { - protected $_apiversion; protected $params; - protected $id; + protected $entity = 'survey'; public $DBResetRequired = FALSE; + public $_eNoticeCompliant = TRUE; function setUp() { - $this->_apiversion = 3; - $phoneBankActivity = civicrm_api('Option_value', 'Get', array('label' => 'PhoneBank', 'version' => $this->_apiversion, 'sequential' => 1)); - $phoneBankActivityTypeID = $phoneBankActivity['values'][0]['value']; + $phoneBankActivityTypeID = $this->callAPISuccessGetValue('Option_value', array('label' => 'PhoneBank', 'return' => 'id'), 'integer'); $this->params = array( - 'version' => 3, 'title' => "survey title", 'activity_type_id' => $phoneBankActivityTypeID, 'max_number_of_contacts' => 12, @@ -56,50 +65,65 @@ class api_v3_SurveyTest extends CiviUnitTestCase { parent::setUp(); } - function tearDown() {} +/** + * Here we clean up any test data we created. + * Note that the quickCleanup function turns off Foreign keys first + * so will not remove related entities + */ + function tearDown() { + $tablesToTruncate = array('civicrm_survey'); + $this->quickCleanup($tablesToTruncate); + } + /** + * test create function succeeds + */ public function testCreateSurvey() { - $result = civicrm_api('survey', 'create', $this->params); - $this->documentMe($this->params, $result, __FUNCTION__, __FILE__); - $this->assertAPISuccess($result, 'In line ' . __LINE__); - $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); - $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); + $result = $this->callAPIAndDocument('survey', 'create', $this->params, __FUNCTION__, __FILE__); + $this->getAndCheck($this->params, $result['id'], $this->entity); } + /** + * Test get function succeeds (this is actually largely tested in the get + * action on create. Add extra checks for any 'special' return values or + * behaviours + * + */ public function testGetSurvey() { - - $result = civicrm_api('survey', 'get', $this->params); - $this->documentMe($this->params, $result, __FUNCTION__, __FILE__); - $this->assertAPISuccess($result, 'In line ' . __LINE__); + $this->createTestEntity(); + $result = $this->callAPIAndDocument('survey', 'get', $this->params, __FUNCTION__, __FILE__); $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); - $this->id = $result['id']; } +/** + * Check the delete function succeeds + */ public function testDeleteSurvey() { - $entity = civicrm_api('survey', 'get', $this->params); - $result = civicrm_api('survey', 'delete', array('version' => 3, 'id' => $entity['id'])); - $this->documentMe($this->params, $result, __FUNCTION__, __FILE__); - $this->assertAPISuccess($result, 'In line ' . __LINE__); - $checkDeleted = civicrm_api('survey', 'get', array( - 'version' => 3, + $entity = $this->createTestEntity(); + $result = $this->callAPIAndDocument('survey', 'delete', array('id' => $entity['id']), __FUNCTION__, __FILE__); + $checkDeleted = $this->callAPISuccess($this->entity, 'get', array( )); $this->assertEquals(0, $checkDeleted['count'], 'In line ' . __LINE__); } + /** + * test & document chained delete pattern. Note that explanation of the pattern + * is best put in the $description variable as it will then be displayed in the + * test generated examples. (these are to be found in the api/examples folder) + * + */ public function testGetSurveyChainDelete() { $description = "demonstrates get + delete in the same call"; $subfile = 'ChainedGetDelete'; $params = array( - 'version' => 3, 'title' => "survey title", 'api.survey.delete' => 1, ); - $result = civicrm_api('survey', 'create', $this->params); - $result = civicrm_api('survey', 'get', $params); - $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); - $this->assertAPISuccess($result, 'In line ' . __LINE__); + $result = $this->callAPISuccess('survey', 'create', $this->params); + $result = $this->callAPIAndDocument('survey', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); $this->assertEquals(0, civicrm_api('survey', 'getcount', array('version' => 3)), 'In line ' . __LINE__); } + } -- 2.25.1