2a10abf7e7357f7fc2a13c7dbbba3c7618ab6e4e
9 * This trait defines a number of helper functions for testing APIv3. Commonly
10 * used helpers include `callAPISuccess()`, `callAPIFailure()`,
11 * `assertAPISuccess()`, and `assertAPIFailure()`.
13 * This trait is intended for use with PHPUnit-based test cases.
18 * Api version - easier to override than just a define
20 protected $_apiversion = 3;
23 * Check that api returned 'is_error' => 1
24 * else provide full message
25 * @param array $result
27 * @param array $valuesToExclude
28 * @param string $prefix
29 * Extra test to add to message.
31 public function assertAPIArrayComparison($result, $expected, $valuesToExclude = array(), $prefix = '') {
32 $valuesToExclude = array_merge($valuesToExclude, array('debug', 'xdebug', 'sequential'));
33 foreach ($valuesToExclude as $value) {
34 if (isset($result[$value])) {
35 unset($result[$value]);
37 if (isset($expected[$value])) {
38 unset($expected[$value]);
41 $this->assertEquals($result, $expected, "api result array comparison failed " . $prefix . print_r($result, TRUE) . ' was compared to ' . print_r($expected, TRUE));
45 * Check that a deleted item has been deleted.
50 public function assertAPIDeleted($entity, $id) {
51 $this->callAPISuccess($entity, 'getcount', array('id' => $id), 0);
55 * Check that api returned 'is_error' => 1.
57 * @param array $apiResult
59 * @param string $prefix
60 * Extra test to add to message.
61 * @param null $expectedError
63 public function assertAPIFailure($apiResult, $prefix = '', $expectedError = NULL) {
64 if (!empty($prefix)) {
67 if ($expectedError && !empty($apiResult['is_error'])) {
68 $this->assertEquals($expectedError, $apiResult['error_message'], 'api error message not as expected' . $prefix);
70 $this->assertEquals(1, $apiResult['is_error'], "api call should have failed but it succeeded " . $prefix . (print_r($apiResult, TRUE)));
71 $this->assertNotEmpty($apiResult['error_message']);
75 * Check that api returned 'is_error' => 0.
77 * @param array $apiResult
79 * @param string $prefix
80 * Extra test to add to message.
82 public function assertAPISuccess($apiResult, $prefix = '') {
83 if (!empty($prefix)) {
86 $errorMessage = empty($apiResult['error_message']) ?
'' : " " . $apiResult['error_message'];
88 if (!empty($apiResult['debug_information'])) {
89 $errorMessage .= "\n " . print_r($apiResult['debug_information'], TRUE);
91 if (!empty($apiResult['trace'])) {
92 $errorMessage .= "\n" . print_r($apiResult['trace'], TRUE);
94 $this->assertEquals(0, $apiResult['is_error'], $prefix . $errorMessage);
98 * This function exists to wrap api functions.
99 * so we can ensure they fail where expected & throw exceptions without litterering the test with checks
100 * @param string $entity
101 * @param string $action
102 * @param array $params
103 * @param string $expectedErrorMessage
105 * @param null $extraOutput
108 public function callAPIFailure($entity, $action, $params, $expectedErrorMessage = NULL, $extraOutput = NULL) {
109 if (is_array($params)) {
111 'version' => $this->_apiversion
,
114 $result = $this->civicrm_api($entity, $action, $params);
115 $this->assertAPIFailure($result, "We expected a failure for $entity $action but got a success", $expectedErrorMessage);
120 * wrap api functions.
121 * so we can ensure they succeed & throw exceptions without litterering the test with checks
123 * @param string $entity
124 * @param string $action
125 * @param array $params
126 * @param mixed $checkAgainst
127 * Optional value to check result against, implemented for getvalue,.
128 * getcount, getsingle. Note that for getvalue the type is checked rather than the value
129 * for getsingle the array is compared against an array passed in - the id is not compared (for
134 public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL) {
135 $params = array_merge(array(
136 'version' => $this->_apiversion
,
141 switch (strtolower($action)) {
143 return $this->callAPISuccessGetValue($entity, $params, $checkAgainst);
146 return $this->callAPISuccessGetSingle($entity, $params, $checkAgainst);
149 return $this->callAPISuccessGetCount($entity, $params, $checkAgainst);
151 $result = $this->civicrm_api($entity, $action, $params);
152 $this->assertAPISuccess($result, "Failure in api call for $entity $action");
157 * This function exists to wrap api getValue function & check the result
158 * so we can ensure they succeed & throw exceptions without litterering the test with checks
159 * There is a type check in this
160 * @param string $entity
161 * @param array $params
166 public function callAPISuccessGetCount($entity, $params, $count = NULL) {
168 'version' => $this->_apiversion
,
171 $result = $this->civicrm_api($entity, 'getcount', $params);
172 if (!is_int($result) ||
!empty($result['is_error']) ||
isset($result['values'])) {
173 throw new \
Exception('Invalid getcount result : ' . print_r($result, TRUE) . " type :" . gettype($result));
175 if (is_int($count)) {
176 $this->assertEquals($count, $result, "incorrect count returned from $entity getcount");
182 * This function exists to wrap api getsingle function & check the result
183 * so we can ensure they succeed & throw exceptions without litterering the test with checks
185 * @param string $entity
186 * @param array $params
187 * @param array $checkAgainst
188 * Array to compare result against.
199 public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) {
201 'version' => $this->_apiversion
,
203 $result = $this->civicrm_api($entity, 'getsingle', $params);
204 if (!is_array($result) ||
!empty($result['is_error']) ||
isset($result['values'])) {
205 throw new \
Exception('Invalid getsingle result' . print_r($result, TRUE));
208 // @todo - have gone with the fn that unsets id? should we check id?
209 $this->checkArrayEquals($result, $checkAgainst);
215 * This function exists to wrap api getValue function & check the result
216 * so we can ensure they succeed & throw exceptions without litterering the test with checks
217 * There is a type check in this
219 * @param string $entity
220 * @param array $params
221 * @param string $type
222 * Per http://php.net/manual/en/function.gettype.php possible types.
232 public function callAPISuccessGetValue($entity, $params, $type = NULL) {
234 'version' => $this->_apiversion
,
237 $result = $this->civicrm_api($entity, 'getvalue', $params);
238 if (is_array($result) && (!empty($result['is_error']) ||
isset($result['values']))) {
239 throw new \
Exception('Invalid getvalue result' . print_r($result, TRUE));
242 if ($type == 'integer') {
243 // api seems to return integers as strings
244 $this->assertTrue(is_numeric($result), "expected a numeric value but got " . print_r($result, 1));
247 $this->assertType($type, $result, "returned result should have been of type $type but was ");
254 * A stub for the API interface. This can be overriden by subclasses to change how the API is called.
258 * @param array $params
261 public function civicrm_api($entity, $action, $params) {
262 return civicrm_api($entity, $action, $params);