From beffb5e6f4b4f8a6c627eb5ff008858f7b41a5e5 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 23 May 2021 08:54:28 +1000 Subject: [PATCH] =?UTF-8?q?Update=20Unit=20Test=20code=20to=20work=20with?= =?UTF-8?q?=20PHPUnit8=20whilst=20maintaining=20suppor=E2=80=A6=20(#355)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpunit.xml.dist | 2 +- tests/phpunit/CRM/Iats/BaseTestClass.php | 166 +----------------- .../phpunit/CRM/Iats/ContributionIATSTest.php | 8 +- tests/phpunit/bootstrap.php | 1 - 4 files changed, 7 insertions(+), 170 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 754565f4..2e4e326f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + ./tests/phpunit diff --git a/tests/phpunit/CRM/Iats/BaseTestClass.php b/tests/phpunit/CRM/Iats/BaseTestClass.php index 75a51e98..f9759022 100644 --- a/tests/phpunit/CRM/Iats/BaseTestClass.php +++ b/tests/phpunit/CRM/Iats/BaseTestClass.php @@ -17,7 +17,8 @@ * b. Disable TransactionalInterface, and handle all setup/teardown yourself. */ abstract class BaseTestClass extends \PHPUnit\Framework\TestCase implements HeadlessInterface, HookInterface, TransactionalInterface { - //class BaseTestClass extends \PHPUnit_Framework_TestCase implements HeadlessInterface, HookInterface { + + use \Civi\Test\Api3TestTrait; /** * Configure the headless environment. @@ -30,167 +31,4 @@ public function setUpHeadless() { ->apply(); } - private $_apiversion = 3; - - /** - * 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 mixed $checkAgainst - * Optional value to check result against, implemented for getvalue,. - * getcount, getsingle. Note that for getvalue the type is checked rather than the value - * for getsingle the array is compared against an array passed in - the id is not compared (for - * better or worse ) - * - * @return array|int - */ - public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL) { - $params = array_merge(array( - 'version' => $this->_apiversion, - 'debug' => 1, - ), - $params - ); - switch (strtolower($action)) { - case 'getvalue': - return $this->callAPISuccessGetValue($entity, $params, $checkAgainst); - - case 'getsingle': - return $this->callAPISuccessGetSingle($entity, $params, $checkAgainst); - - case 'getcount': - return $this->callAPISuccessGetCount($entity, $params, $checkAgainst); - } - $result = $this->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 - * - * @return array|int - */ - public function callAPISuccessGetValue($entity, $params, $type = NULL) { - $params += array( - 'version' => $this->_apiversion, - 'debug' => 1, - ); - $result = $this->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 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 - * @return array|int - */ - public function callAPISuccessGetCount($entity, $params, $count = NULL) { - $params += array( - 'version' => $this->_apiversion, - 'debug' => 1, - ); - $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)); - } - if (is_int($count)) { - $this->assertEquals($count, $result, "incorrect count returned from $entity getcount"); - } - return $result; - } - /** - * This function exists to wrap api getsingle function & check the result - * so we can ensure they succeed & throw exceptions without litterering the test with checks - * - * @param string $entity - * @param array $params - * @param array $checkAgainst - * Array to compare result against. - * - boolean - * - integer - * - double - * - string - * - array - * - object - * - * @throws Exception - * @return array|int - */ - public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) { - $params += array( - 'version' => $this->_apiversion, - 'debug' => 1, - ); - $result = $this->civicrm_api($entity, 'getsingle', $params); - if (!is_array($result) || !empty($result['is_error']) || isset($result['values'])) { - throw new Exception('Invalid getsingle result' . print_r($result, TRUE)); - } - if ($checkAgainst) { - // @todo - have gone with the fn that unsets id? should we check id? - $this->checkArrayEquals($result, $checkAgainst); - } - return $result; - } - /** - * Check that api returned 'is_error' => 0. - * - * @param array $apiResult - * Api result. - * @param string $prefix - * Extra test to add to message. - */ - public function assertAPISuccess($apiResult, $prefix = '') { - if (!empty($prefix)) { - $prefix .= ': '; - } - $errorMessage = empty($apiResult['error_message']) ? '' : " " . $apiResult['error_message']; - if (!empty($apiResult['debug_information'])) { - $errorMessage .= "\n " . print_r($apiResult['debug_information'], TRUE); - } - if (!empty($apiResult['trace'])) { - $errorMessage .= "\n" . print_r($apiResult['trace'], TRUE); - } - $this->assertEquals(0, $apiResult['is_error'], $prefix . $errorMessage); - } - /** - * A stub for the API interface. This can be overriden by subclasses to change how the API is called. - * - * @param $entity - * @param $action - * @param array $params - * @return array|int - */ - public function civicrm_api($entity, $action, $params) { - return civicrm_api($entity, $action, $params); - } - } diff --git a/tests/phpunit/CRM/Iats/ContributionIATSTest.php b/tests/phpunit/CRM/Iats/ContributionIATSTest.php index f81d2e57..d4ca0567 100644 --- a/tests/phpunit/CRM/Iats/ContributionIATSTest.php +++ b/tests/phpunit/CRM/Iats/ContributionIATSTest.php @@ -35,19 +35,19 @@ public function setUpHeadless() { ->apply(); } - public function setUp() { + public function setUp(): void { $this->_apiversion = 3; parent::setUp(); } - public function tearDown() { + public function tearDown(): void { parent::tearDown(); } /** * Test a Credit Card Contribution - one time iATS Credit Card - TEST41 - Backend */ - public function testIATSCreditCardBackend() { + public function testIATSCreditCardBackend(): void { $params = array( 'sequential' => 1, @@ -120,7 +120,7 @@ public function testIATSCreditCardBackend() { /** * Test a SWIPE Contribution - one time iATS SWIPE - TEST41 - Backend */ - public function testIATSSWIPEBackend() { + public function testIATSSWIPEBackend(): void { $params = array( 'sequential' => 1, diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index ddb9b30a..df1524da 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -1,7 +1,6 @@