From: Eileen McNaughton Date: Wed, 16 Jun 2021 03:06:19 +0000 (+1200) Subject: Try adding a strict return function to getAndCheck X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fc1fb038ae029db0988136893f0ca263881a25eb;p=civicrm-core.git Try adding a strict return function to getAndCheck --- diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index d7eb28e659..50c94f7a1f 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1986,10 +1986,11 @@ VALUES * * @throws CRM_Core_Exception */ - public function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '') { + public function getAndCheck(array $params, int $id, $entity, int $delete = 1, string $errorText = ''): void { $result = $this->callAPISuccessGetSingle($entity, [ 'id' => $id, + 'return' => array_keys($params), ]); if ($delete) { @@ -2007,23 +2008,23 @@ VALUES $keys[CRM_Utils_Array::value('name', $settings, $field)] = CRM_Utils_Array::value('name', $settings, $field); } $type = $settings['type'] ?? NULL; - if ($type == CRM_Utils_Type::T_DATE) { + if ($type === CRM_Utils_Type::T_DATE) { $dateFields[] = $settings['name']; // we should identify both real names & unique names as dates - if ($field != $settings['name']) { + if ($field !== $settings['name']) { $dateFields[] = $field; } } - if ($type == CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME) { + if ($type === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME) { $dateTimeFields[] = $settings['name']; // we should identify both real names & unique names as dates - if ($field != $settings['name']) { + if ($field !== $settings['name']) { $dateTimeFields[] = $field; } } } - if (strtolower($entity) == 'contribution') { + if (strtolower($entity) === 'contribution') { $params['receive_date'] = date('Y-m-d', strtotime($params['receive_date'])); // this is not returned in id format unset($params['payment_instrument_id']); @@ -2032,14 +2033,14 @@ VALUES } foreach ($params as $key => $value) { - if ($key == 'version' || substr($key, 0, 3) == 'api' || (!array_key_exists($key, $keys) || !array_key_exists($keys[$key], $result))) { + if ($key === 'version' || strpos($key, 'api') === 0 || (!array_key_exists($key, $keys) || !array_key_exists($keys[$key], $result))) { continue; } - if (in_array($key, $dateFields)) { + if (in_array($key, $dateFields, TRUE)) { $value = date('Y-m-d', strtotime($value)); $result[$key] = date('Y-m-d', strtotime($result[$key])); } - if (in_array($key, $dateTimeFields)) { + if (in_array($key, $dateTimeFields, TRUE)) { $value = date('Y-m-d H:i:s', strtotime($value)); $result[$keys[$key]] = date('Y-m-d H:i:s', strtotime(CRM_Utils_Array::value($keys[$key], $result, CRM_Utils_Array::value($key, $result)))); } diff --git a/tests/phpunit/api/v3/ActivityTest.php b/tests/phpunit/api/v3/ActivityTest.php index 082f142714..2e8740f512 100644 --- a/tests/phpunit/api/v3/ActivityTest.php +++ b/tests/phpunit/api/v3/ActivityTest.php @@ -1012,9 +1012,13 @@ class api_v3_ActivityTest extends CiviUnitTestCase { } /** - * Test api updates an existing activity, including removing activity contacts in apiv3 style. + * Test api updates an existing activity, including removing activity + * contacts in apiv3 style. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testActivityUpdate() { + public function testActivityUpdate(): void { $result = $this->callAPISuccess('activity', 'create', $this->_params); $params = [ @@ -1074,15 +1078,17 @@ class api_v3_ActivityTest extends CiviUnitTestCase { 'record_type_id' => ['IN' => ['Activity Assignees', 'Activity Targets']], ], 0); - unset($params['source_contact_id']); + unset($params['source_contact_id'], $params['assignee_contact_id'], $params['target_contact_id']); $this->getAndCheck($params, $result['id'], 'activity'); } /** * Test civicrm_activity_update() with valid parameters * and some custom data + * + * @throws \CRM_Core_Exception */ - public function testActivityUpdateCustom() { + public function testActivityUpdateCustom(): void { $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); $params = $this->_params; diff --git a/tests/phpunit/api/v3/AddressTest.php b/tests/phpunit/api/v3/AddressTest.php index 69817d4bfd..3f7ed4f873 100644 --- a/tests/phpunit/api/v3/AddressTest.php +++ b/tests/phpunit/api/v3/AddressTest.php @@ -228,31 +228,37 @@ class api_v3_AddressTest extends CiviUnitTestCase { * Is_primary should be set as a default. * * ie. create the address, unset the params & recreate. - * is_primary should be 0 before & after the update. ie - having no other address - * is_primary is invalid. + * is_primary should be 0 before & after the update. ie - having no other + * address is_primary is invalid. + * * @param int $version + * * @dataProvider versionThreeAndFour + * @throws \CRM_Core_Exception */ - public function testCreateAddressTestDefaultWithID($version) { + public function testCreateAddressTestDefaultWithID(int $version): void { $this->_apiversion = $version; $params = $this->_params; $params['is_primary'] = 0; - $result = $this->callAPISuccess('address', 'create', $params); + $result = $this->callAPISuccess('Address', 'create', $params); unset($params['is_primary']); $params['id'] = $result['id']; - $this->callAPISuccess('address', 'create', $params); - $result = $this->callAPISuccess('address', 'get', ['contact_id' => $params['contact_id']]); + $this->callAPISuccess('Address', 'create', $params); + $result = $this->callAPISuccess('Address', 'get', ['contact_id' => $params['contact_id']]); $this->assertEquals(1, $result['count']); $this->assertEquals(1, $result['values'][$result['id']]['is_primary']); - $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__); + $this->getAndCheck($params, $result['id'], 'address'); } /** * test address deletion. + * * @param int $version + * * @dataProvider versionThreeAndFour + * @throws \CRM_Core_Exception */ - public function testDeleteAddress($version) { + public function testDeleteAddress($version): void { $this->_apiversion = $version; //check there are no address to start with $get = $this->callAPISuccess('address', 'get', [ diff --git a/tests/phpunit/api/v3/ParticipantTest.php b/tests/phpunit/api/v3/ParticipantTest.php index 954c34e9bb..f9d20b6c0f 100644 --- a/tests/phpunit/api/v3/ParticipantTest.php +++ b/tests/phpunit/api/v3/ParticipantTest.php @@ -15,6 +15,8 @@ * @package CiviCRM_APIv3 */ +use Civi\Api4\Participant; + /** * Class api_v3_ParticipantTest * @group headless @@ -482,6 +484,8 @@ class api_v3_ParticipantTest extends CiviUnitTestCase { /** * Test the line items for participant fee with multiple price field values. + * + * @throws \CRM_Core_Exception */ public function testCreateParticipantLineItems() { // Create a price set for this event. @@ -602,8 +606,11 @@ class api_v3_ParticipantTest extends CiviUnitTestCase { /** * Check with complete array. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception */ - public function testUpdate() { + public function testUpdate(): void { $participantId = $this->participantCreate([ 'contactID' => $this->_individualId, 'eventID' => $this->_eventID, @@ -613,13 +620,19 @@ class api_v3_ParticipantTest extends CiviUnitTestCase { 'contact_id' => $this->_individualId, 'event_id' => $this->_eventID, 'status_id' => 3, - 'role_id' => 3, - 'register_date' => '2006-01-21', + 'role_id' => [3], + 'register_date' => '2006-01-21 00:00:00', 'source' => 'US Open', ]; - $participant = $this->callAPISuccess('participant', 'create', $params); - $this->getAndCheck($params, $participant['id'], 'participant'); - $result = $this->participantDelete($params['id']); + $participantID = $this->callAPISuccess('Participant', 'create', $params)['id']; + $participant = Participant::get() + ->setSelect(array_keys($params)) + ->addWhere('id', '=', $participantID) + ->execute()->first(); + + foreach ($params as $key => $value) { + $this->assertEquals($value, $participant[$key], $key . ' mismatch'); + } } /** diff --git a/tests/phpunit/api/v3/PaymentTokenTest.php b/tests/phpunit/api/v3/PaymentTokenTest.php index fce4a48ecd..b4a0a97100 100644 --- a/tests/phpunit/api/v3/PaymentTokenTest.php +++ b/tests/phpunit/api/v3/PaymentTokenTest.php @@ -44,12 +44,12 @@ class api_v3_PaymentTokenTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testCreatePaymentToken(): void { - $description = "Create a payment token - Note use of relative dates here: - @link http://www.php.net/manual/en/datetime.formats.relative.php."; + $description = 'Create a payment token - Note use of relative dates here: + @link http://www.php.net/manual/en/datetime.formats.relative.php.'; $result = $this->callAPIAndDocument('payment_token', 'create', $this->params, __FUNCTION__, __FILE__, $description); $this->assertEquals(1, $result['count']); $this->assertNotNull($result['values'][$result['id']]['id']); - $this->getAndCheck(array_merge($this->params, [$this->params]), $result['id'], 'payment_token', TRUE); + $this->getAndCheck($this->params, $result['id'], 'payment_token', TRUE); } /**