Skip to content

Commit

Permalink
Test class tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jan 7, 2021
1 parent eba1a1d commit ae16be8
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\ContributionRecur;

/**
* Class CRM_Contribute_BAO_ContributionRecurTest
* @group headless
Expand All @@ -20,11 +22,11 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase {
/**
* Set up for test.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$this->_ids['payment_processor'] = $this->paymentProcessorCreate();
$this->ids['payment_processor'] = $this->paymentProcessorCreate();
$this->_params = [
'contact_id' => $this->individualCreate(),
'amount' => 3.00,
Expand All @@ -47,7 +49,7 @@ public function setUp() {
'failure_retry_date' => NULL,
'auto_renew' => 0,
'currency' => 'USD',
'payment_processor_id' => $this->_ids['payment_processor'],
'payment_processor_id' => $this->ids['payment_processor'],
'is_email_receipt' => 1,
'financial_type_id' => 1,
'payment_instrument_id' => 1,
Expand All @@ -60,7 +62,7 @@ public function setUp() {
*
* @throws \CRM_Core_Exception
*/
public function teardown() {
public function teardown():void {
$this->quickCleanUpFinancialEntities();
}

Expand All @@ -71,7 +73,7 @@ public function teardown() {
*
* @throws \CRM_Core_Exception
*/
public function testFindSave() {
public function testFindSave(): void {
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
$dao = new CRM_Contribute_BAO_ContributionRecur();
$dao->id = $contributionRecur['id'];
Expand All @@ -87,17 +89,17 @@ public function testFindSave() {
*
* @throws \CRM_Core_Exception
*/
public function testCancelRecur() {
public function testCancelRecur(): void {
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution(['id' => $contributionRecur['id']]);
}

/**
* Test checking if contribution recur object can allow for changes to financial types.
*
* @throws \CRM_Core_Exception
* @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
*/
public function testSupportFinancialTypeChange() {
public function testSupportFinancialTypeChange(): void {
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
$this->callAPISuccess('Contribution', 'create', [
'contribution_recur_id' => $contributionRecur['id'],
Expand All @@ -117,7 +119,7 @@ public function testSupportFinancialTypeChange() {
*
* @throws \CRM_Core_Exception
*/
public function testUpdateRecur() {
public function testUpdateRecur(): void {
$createParams = $this->_params;
$createParams['currency'] = 'XAU';
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $createParams);
Expand All @@ -140,7 +142,7 @@ public function testUpdateRecur() {
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testGetTemplateContributionMatchTest1() {
public function testGetTemplateContributionMatchTest1(): void {
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
// Create a first contrib
$firstContrib = $this->callAPISuccess('Contribution', 'create', [
Expand Down Expand Up @@ -177,7 +179,7 @@ public function testGetTemplateContributionMatchTest1() {
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testGetTemplateContributionMatchTest() {
public function testGetTemplateContributionMatchTest(): void {
$params = $this->_params;
$params['is_test'] = 1;
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $params);
Expand Down Expand Up @@ -218,7 +220,7 @@ public function testGetTemplateContributionMatchTest() {
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testGetTemplateContributionNewTemplate() {
public function testGetTemplateContributionNewTemplate(): void {
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
// Create the template
$templateContrib = $this->callAPISuccess('Contribution', 'create', [
Expand Down Expand Up @@ -260,9 +262,9 @@ public function testGetTemplateContributionNewTemplate() {
/**
* Test to check if correct membership is auto renewed.
*
* @throws \CRM_Core_Exception
* @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
*/
public function testAutoRenewalWhenOneMemberIsDeceased() {
public function testAutoRenewalWhenOneMemberIsDeceased(): void {
$contactId1 = $this->individualCreate();
$contactId2 = $this->individualCreate();
$membershipOrganizationId = $this->organizationCreate();
Expand All @@ -277,7 +279,7 @@ public function testAutoRenewalWhenOneMemberIsDeceased() {
]);

// create membership type
$membershipTypeId1 = $this->callAPISuccess('MembershipType', 'create', [
$membershipTypeId1 = (int) $this->callAPISuccess('MembershipType', 'create', [
'domain_id' => 1,
'member_of_contact_id' => $membershipOrganizationId,
'financial_type_id' => 'Member Dues',
Expand All @@ -288,7 +290,7 @@ public function testAutoRenewalWhenOneMemberIsDeceased() {
'name' => 'Parent',
])['id'];

$membershipTypeID = $this->callAPISuccess('MembershipType', 'create', [
$membershipTypeID = (int) $this->callAPISuccess('MembershipType', 'create', [
'domain_id' => 1,
'member_of_contact_id' => $membershipOrganizationId,
'financial_type_id' => 'Member Dues',
Expand Down Expand Up @@ -320,7 +322,7 @@ public function testAutoRenewalWhenOneMemberIsDeceased() {

foreach ($priceFields as $priceField) {
$lineItems = [];
$contactId = array_search($priceField['membership_type_id'], $contactIDs);
$contactId = array_search((int) $priceField['membership_type_id'], $contactIDs, TRUE);
$lineItems[1] = [
'price_field_id' => $priceField['priceFieldID'],
'price_field_value_id' => $priceField['priceFieldValueID'],
Expand Down Expand Up @@ -476,7 +478,7 @@ public function testAutoRenewalWhenOneMemberIsDeceased() {
*
* @throws \CRM_Core_Exception
*/
public function validateAllCounts($membershipId, $count) {
public function validateAllCounts(int $membershipId, int $count): void {
$memPayParams = [
'membership_id' => $membershipId,
];
Expand All @@ -498,16 +500,16 @@ public function validateAllCounts($membershipId, $count) {
* we want Nov 31.
*
* @param int $offset
* @param int $year Optional input year to start
* @param int $month Optional input month to start
* @param int|null $year Optional input year to start
* @param int|null $month Optional input month to start
*
* @return array
* ['year' => int, 'month' => int]
*/
private function getYearAndMonthFromOffset(int $offset, int $year = NULL, int $month = NULL) {
private function getYearAndMonthFromOffset(int $offset, int $year = NULL, int $month = NULL): array {
$dateInfo = [
'year' => $year ?? date('Y'),
'month' => ($month ?? date('m')) + $offset,
'year' => $year ?? (int) date('Y'),
'month' => ($month ?? (int) date('m')) + $offset,
];
if ($dateInfo['month'] > 12) {
$dateInfo['year']++;
Expand All @@ -522,19 +524,20 @@ private function getYearAndMonthFromOffset(int $offset, int $year = NULL, int $m

/**
* Test getYearAndMonthFromOffset
*
* @dataProvider yearMonthProvider
*
* @param array $input
* @param array $expected
*/
public function testGetYearAndMonthFromOffset($input, $expected) {
public function testGetYearAndMonthFromOffset(array $input, array $expected): void {
$this->assertEquals($expected, $this->getYearAndMonthFromOffset($input[0], $input[1], $input[2]));
}

/**
* data provider for testGetYearAndMonthFromOffset
*/
public function yearMonthProvider() {
public function yearMonthProvider(): array {
return [
// input = offset, year, current month
['input' => [4, 2020, 1], 'output' => ['year' => '2020', 'month' => '05']],
Expand Down Expand Up @@ -569,26 +572,27 @@ public function yearMonthProvider() {
*
* @throws \CRM_Core_Exception
*/
public function testContributionEmailReceipt() {
public function testContributionEmailReceipt(): void {
$createParams = $this->_params;
unset($createParams['trxn_id'], $createParams['invoice_id']);

// pass null value to is_email_receipt
$createParams['is_email_receipt'] = NULL;
$recurring1 = CRM_Contribute_BAO_ContributionRecur::add($createParams);
$recurring1Get = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $recurring1->id]);
$recurring1 = $this->callAPISuccess('ContributionRecur', 'create', $createParams);
$recurring1Get = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $recurring1['id']]);
// default is_email_receipt column value is 1
$this->assertEquals('1', $recurring1Get['is_email_receipt']);

// pass empty value to is_email_receipt
$createParams['is_email_receipt'] = '';
$recurring2 = CRM_Contribute_BAO_ContributionRecur::add($createParams);
$this->assertEquals('null', $recurring2->is_email_receipt);
$recurring2 = $this->callAPISuccess('ContributionRecur', 'create', $createParams);
$recurring2 = ContributionRecur::get(FALSE)->addWhere('id', '=', $recurring2['id'])->addSelect('is_email_receipt')->execute()->first();
$this->assertEquals(NULL, $recurring2['is_email_receipt']);

// pass 0 value to is_email_receipt
// Pass 0 value to is_email_receipt.
$createParams['is_email_receipt'] = 0;
$recurring3 = CRM_Contribute_BAO_ContributionRecur::add($createParams);
$recurring3Get = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $recurring3->id]);
$recurring3 = $this->callAPISuccess('ContributionRecur', 'create', $createParams);
$recurring3Get = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $recurring3['id']]);
$this->assertEquals('0', $recurring3Get['is_email_receipt']);
}

Expand Down

0 comments on commit ae16be8

Please sign in to comment.