diff --git a/modules/ppcp-api-client/src/Repository/PayPalRequestIdRepository.php b/modules/ppcp-api-client/src/Repository/PayPalRequestIdRepository.php index 01bf35a1d..de0f7e650 100644 --- a/modules/ppcp-api-client/src/Repository/PayPalRequestIdRepository.php +++ b/modules/ppcp-api-client/src/Repository/PayPalRequestIdRepository.php @@ -60,12 +60,13 @@ class PayPalRequestIdRepository { * @param string $request_id The ID. */ public function set( string $key, string $request_id ): void { - $all = $this->all(); - $all[ $key ] = array( + $all = $this->all(); + $day_in_seconds = 86400; + $all[ $key ] = array( 'id' => $request_id, - 'expiration' => time() + 10 * DAY_IN_SECONDS, + 'expiration' => time() + 10 * $day_in_seconds, ); - $all = $this->cleanup( $all ); + $all = $this->cleanup( $all ); update_option( self::KEY, $all ); } diff --git a/tests/PHPUnit/ApiClient/Repository/PayPalRequestIdRepositoryTest.php b/tests/PHPUnit/ApiClient/Repository/PayPalRequestIdRepositoryTest.php new file mode 100644 index 000000000..f27b0ed3e --- /dev/null +++ b/tests/PHPUnit/ApiClient/Repository/PayPalRequestIdRepositoryTest.php @@ -0,0 +1,58 @@ +testee = new PayPalRequestIdRepository(); + + when('get_option')->alias(function () { + return $this->data; + }); + when('update_option')->alias(function (string $key, array $data) { + $this->data = $data; + }); + } + + public function testForOrder() + { + $this->testee->set_for_order($this->createPaypalOrder('42'), 'request1'); + $this->testee->set_for_order($this->createPaypalOrder('43'), 'request2'); + + self::assertEquals('request1', $this->testee->get_for_order($this->createPaypalOrder('42'))); + self::assertEquals('request2', $this->testee->get_for_order($this->createPaypalOrder('43'))); + self::assertEquals('', $this->testee->get_for_order($this->createPaypalOrder('41'))); + } + + public function testExpiration() + { + $this->testee->set_for_order($this->createPaypalOrder('42'), 'request1'); + $this->data['42']['expiration'] = time() - 1; + $this->testee->set_for_order($this->createPaypalOrder('43'), 'request2'); + + self::assertEquals('', $this->testee->get_for_order($this->createPaypalOrder('42'))); + self::assertEquals('request2', $this->testee->get_for_order($this->createPaypalOrder('43'))); + } + + private function createPaypalOrder(string $id): Order { + $order = Mockery::mock(Order::class); + $order + ->shouldReceive('id') + ->andReturn($id); + return $order; + } +} diff --git a/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php index c324901a8..946ade989 100644 --- a/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php @@ -32,11 +32,12 @@ class AuthorizedPaymentsProcessorTest extends TestCase private $amount = 42.0; private $currency = 'EUR'; private $paypalOrder; + private $authorization; private $orderEndpoint; private $paymentsEndpoint; private $notice; private $config; - private $subscription_helper; + private $subscription_helperauthorization; private $testee; public function setUp(): void { @@ -44,7 +45,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase $this->wcOrder = $this->createWcOrder($this->paypalOrderId); - $this->paypalOrder = $this->createPaypalOrder([$this->createAuthorization($this->authorizationId, AuthorizationStatus::CREATED)]); + $this->authorization = $this->createAuthorization($this->authorizationId, AuthorizationStatus::CREATED); + $this->paypalOrder = $this->createPaypalOrder([$this->authorization]); $this->orderEndpoint = Mockery::mock(OrderEndpoint::class); $this->orderEndpoint @@ -176,6 +178,57 @@ class AuthorizedPaymentsProcessorTest extends TestCase ); } + public function testVoid() + { + $authorizations = [ + $this->createAuthorization('id1', AuthorizationStatus::CREATED), + $this->createAuthorization('id2', AuthorizationStatus::VOIDED), + $this->createAuthorization('id3', AuthorizationStatus::PENDING), + $this->createAuthorization('id4', AuthorizationStatus::CAPTURED), + $this->createAuthorization('id5', AuthorizationStatus::DENIED), + $this->createAuthorization('id6', AuthorizationStatus::EXPIRED), + $this->createAuthorization('id7', AuthorizationStatus::COMPLETED), + ]; + $this->paypalOrder = $this->createPaypalOrder($authorizations); + + $this->paymentsEndpoint + ->expects('void') + ->with($authorizations[0]); + $this->paymentsEndpoint + ->expects('void') + ->with($authorizations[2]); + + $this->testee->void_authorizations($this->paypalOrder); + + self::assertTrue(true); // fix no assertions warning + } + + public function testVoidWhenNoVoidable() + { + $exception = new RuntimeException('void error'); + $this->paymentsEndpoint + ->expects('void') + ->with($this->authorization) + ->andThrow($exception); + + $this->expectExceptionObject($exception); + + $this->testee->void_authorizations($this->paypalOrder); + } + + public function testVoidWhenNoError() + { + $authorizations = [ + $this->createAuthorization('id1', AuthorizationStatus::VOIDED), + $this->createAuthorization('id2', AuthorizationStatus::EXPIRED), + ]; + $this->paypalOrder = $this->createPaypalOrder($authorizations); + + $this->expectException(RuntimeException::class); + + $this->testee->void_authorizations($this->paypalOrder); + } + private function createWcOrder(string $paypalOrderId): WC_Order { $wcOrder = Mockery::mock(WC_Order::class); $wcOrder @@ -192,14 +245,7 @@ class AuthorizedPaymentsProcessorTest extends TestCase } private function createAuthorization(string $id, string $status): Authorization { - $authorization = Mockery::mock(Authorization::class); - $authorization - ->shouldReceive('id') - ->andReturn($id); - $authorization - ->shouldReceive('status') - ->andReturn(new AuthorizationStatus($status)); - return $authorization; + return new Authorization($id, new AuthorizationStatus($status)); } private function createCapture(string $status): Capture {