From dbb17d48689b6033d365d1b9297456a3e0668ab1 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 31 Aug 2020 11:21:51 +1200 Subject: [PATCH] Slight increase in test cover & make functions more re-usable Just preliminary to increasing cover in another test class --- .../CRM/Core/Payment/AuthorizeNetTrait.php | 28 +++++++++++++++++++ .../CRM/Financial/Form/PaymentFormsTest.php | 8 ++---- tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php index 87ff897d41..38871fa041 100644 --- a/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php +++ b/tests/phpunit/CRM/Core/Payment/AuthorizeNetTrait.php @@ -72,4 +72,32 @@ trait CRM_Core_Payment_AuthorizeNetTrait { return '8d468ca1b1dd5c2b56c7OkI00001Successful.663205215120232801512027350'; } + /** + * Create an AuthorizeNet processors with a configured mock handler. + * + * @throws \CiviCRM_API3_Exception + */ + protected function createAuthorizeNetProcessor() { + $processorID = $this->paymentProcessorAuthorizeNetCreate(['is_test' => FALSE]); + $this->setupMockHandler($processorID); + $this->ids['PaymentProcessor']['anet'] = $processorID; + } + + /** + * Assert the request sent to Authorize.net contains the expected values. + * + * @param array $expected + */ + protected function assertRequestValid($expected = []) { + $expected = array_merge([ + 'x_card_num' => '4111111111111111', + 'x_card_code' => 123, + ], $expected); + $request = explode('&', $this->getRequestBodies()[0]); + // This is stand in for now just to check a request happened. We can improve later. + foreach ($expected as $key => $value) { + $this->assertContains($key . '=' . $value, $request); + } + } + } diff --git a/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php b/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php index 29b271cadb..f012856aa8 100644 --- a/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php +++ b/tests/phpunit/CRM/Financial/Form/PaymentFormsTest.php @@ -38,8 +38,8 @@ class CRM_Financial_Form_PaymentFormsTest extends CiviUnitTestCase { * @throws \CiviCRM_API3_Exception */ public function testEventPaymentForms() { - $processors = [$this->paymentProcessorAuthorizeNetCreate(['is_test' => FALSE])]; - $this->setupMockHandler($processors[0]); + $this->createAuthorizeNetProcessor(); + $processors = [$this->ids['PaymentProcessor']['anet']]; $eventID = $this->eventCreatePaid([ 'end_date' => '+ 1 month', 'registration_end_date' => '+ 1 month', @@ -93,9 +93,7 @@ class CRM_Financial_Form_PaymentFormsTest extends CiviUnitTestCase { $qfKey = $form->controller->_key; } $this->callAPISuccessGetSingle('Participant', ['participant_status_id' => 'Registered']); - $request = explode('&', $this->getRequestBodies()[0]); - // This is stand in for now just to check a request happened. We can improve later. - $this->assertContains('x_card_num=4111111111111111', $request); + $this->assertRequestValid(['x_city' => 'The+Shire', 'x_state' => 'IL', 'x_amount' => 1.0]); } } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 8f4550ec44..229650a46b 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -749,7 +749,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { ], $params); $result = $this->callAPISuccess('PaymentProcessor', 'create', $params); - return $result['id']; + return (int) $result['id']; } /** -- 2.25.1