Skip to content

Commit

Permalink
Try adding a strict return function to getAndCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 16, 2021
1 parent df67838 commit fc1fb03
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
19 changes: 10 additions & 9 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1986,10 +1986,11 @@ public function restoreMembershipTypes() {
*
* @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) {
Expand All @@ -2007,23 +2008,23 @@ public function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '')
$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']);
Expand All @@ -2032,14 +2033,14 @@ public function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '')
}

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))));
}
Expand Down
14 changes: 10 additions & 4 deletions tests/phpunit/api/v3/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,9 +1012,13 @@ public function testActivityUpdateWithIncorrectContactActivityType() {
}

/**
* 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 = [
Expand Down Expand Up @@ -1074,15 +1078,17 @@ public function testActivityUpdate() {
'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;
Expand Down
22 changes: 14 additions & 8 deletions tests/phpunit/api/v3/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,31 +228,37 @@ public function testCreateAddressWithMasterRelationshipChangingOrganization($ver
* 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', [
Expand Down
25 changes: 19 additions & 6 deletions tests/phpunit/api/v3/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @package CiviCRM_APIv3
*/

use Civi\Api4\Participant;

/**
* Class api_v3_ParticipantTest
* @group headless
Expand Down Expand Up @@ -482,6 +484,8 @@ public function testCreateUpdateParticipantFeeLevel() {

/**
* 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.
Expand Down Expand Up @@ -602,8 +606,11 @@ public function testCreateParticipantLineItems() {

/**
* 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,
Expand All @@ -613,13 +620,19 @@ public function testUpdate() {
'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');
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/api/v3/PaymentTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function setUp(): void {
* @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);
}

/**
Expand Down

0 comments on commit fc1fb03

Please sign in to comment.