From eebb19c2e1d2b2bf6ccf152d4bf50b691e311c69 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 28 Jun 2022 23:26:48 +1200 Subject: [PATCH] Minor test cleanup --- .../CRM/Contribute/BAO/ContributionTest.php | 4 ++-- .../Contribute/Form/AdditionalPaymentTest.php | 3 --- .../CRM/Core/BAO/FinancialTrxnTest.php | 2 +- tests/phpunit/CiviTest/CiviMailUtils.php | 4 +--- tests/phpunit/CiviTest/CiviUnitTestCase.php | 22 ++++++++++++------- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 498426807a..85db0f6d74 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -967,7 +967,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; /** * Test for function getSalesTaxFinancialAccounts(). */ - public function testgetSalesTaxFinancialAccounts() { + public function testGetSalesTaxFinancialAccounts(): void { $this->enableTaxAndInvoicing(); $financialType = $this->createFinancialType(); $financialAccount = $this->addTaxAccountToFinancialType($financialType['id']); @@ -976,7 +976,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; $financialAccount = $this->addTaxAccountToFinancialType($financialType['id']); $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id; $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts(); - $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.'); + $this->assertEquals($salesTaxFinancialAccount, $expectedResult); } /** diff --git a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php index 72efd47637..1cf3deac08 100644 --- a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php +++ b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php @@ -141,9 +141,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { '***********1111', 'Expires: May 2025', ]); - - $mut->stop(); - $mut->clearMessages(); } /** diff --git a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php index 178a46dba5..8120f82773 100644 --- a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php +++ b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php @@ -95,7 +95,7 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testGetTotalPaymentsParticipantOrder() { + public function testGetTotalPaymentsParticipantOrder(): void { $orderID = $this->createPartiallyPaidParticipantOrder()['id']; $params = [ 'contribution_id' => $orderID, diff --git a/tests/phpunit/CiviTest/CiviMailUtils.php b/tests/phpunit/CiviTest/CiviMailUtils.php index 551f9dd225..9fe84fbc77 100644 --- a/tests/phpunit/CiviTest/CiviMailUtils.php +++ b/tests/phpunit/CiviTest/CiviMailUtils.php @@ -245,10 +245,8 @@ class CiviMailUtils extends PHPUnit\Framework\TestCase { * * @param int $limit * How many recent messages to remove, defaults to 0 (all). - * - * @throws \CRM_Core_Exception */ - public function clearMessages($limit = 0) { + public function clearMessages(int $limit = 0): void { $sql = 'DELETE FROM civicrm_mailing_spool ORDER BY id DESC'; if ($limit) { $sql .= ' LIMIT ' . $limit; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 07baf4ed80..b46a7d985d 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -30,6 +30,7 @@ use Civi\Api4\Address; use Civi\Api4\Contribution; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; +use Civi\Api4\FinancialAccount; use Civi\Api4\FinancialType; use Civi\Api4\LineItem; use Civi\Api4\MembershipType; @@ -1922,6 +1923,10 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 'Event Fee', ] )->execute(); + FinancialAccount::delete(FALSE)->addClause( + 'OR', + [['name', 'LIKE', 'Financial-Type -%'], ['name', 'LIKE', 'Sales tax %']] + )->execute(); } catch (API_Exception $e) { $this->fail('failed to cleanup financial types ' . $e->getMessage()); @@ -3016,31 +3021,32 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { /** * Add Sales Tax Account for the financial type. * - * @param int $financialTypeId + * @param int $financialTypeID * * @param array $accountParams * * @return CRM_Financial_DAO_EntityFinancialAccount - * @throws \CRM_Core_Exception + * + * @noinspection PhpDocMissingThrowsInspection */ - protected function addTaxAccountToFinancialType(int $financialTypeId, $accountParams = []) { + protected function addTaxAccountToFinancialType(int $financialTypeID, array $accountParams = []): CRM_Financial_DAO_EntityFinancialAccount { $params = array_merge([ - 'name' => 'Sales tax account ' . substr(sha1(rand()), 0, 4), + 'name' => 'Sales tax account - test - ' . $financialTypeID, 'financial_account_type_id' => key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")), 'is_deductible' => 1, 'is_tax' => 1, 'tax_rate' => 10, 'is_active' => 1, ], $accountParams); - $account = CRM_Financial_BAO_FinancialAccount::add($params); + $financialAccountID = FinancialAccount::create()->setValues($params)->execute()->first()['id']; $entityParams = [ 'entity_table' => 'civicrm_financial_type', - 'entity_id' => $financialTypeId, + 'entity_id' => $financialTypeID, 'account_relationship' => key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")), ]; // set tax rate (as 10) for provided financial type ID to static variable, later used to fetch tax rates of all financial types - \Civi::$statics['CRM_Core_PseudoConstant']['taxRates'][$financialTypeId] = $params['tax_rate']; + \Civi::$statics['CRM_Core_PseudoConstant']['taxRates'][$financialTypeID] = $params['tax_rate']; //CRM-20313: As per unique index added in civicrm_entity_financial_account table, // first check if there's any record on basis of unique key (entity_table, account_relationship, entity_id) @@ -3050,7 +3056,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { if ($dao->fetch()) { $entityParams['id'] = $dao->id; } - $entityParams['financial_account_id'] = $account->id; + $entityParams['financial_account_id'] = $financialAccountID; return CRM_Financial_BAO_FinancialTypeAccount::add($entityParams); } -- 2.25.1